1. 程式人生 > >用CSS實現圖片幻燈片式的切換(所有圖片輪流顯示,每次顯示一張)

用CSS實現圖片幻燈片式的切換(所有圖片輪流顯示,每次顯示一張)

HTML 部分

<div id="img-wrap">
    <img src="img/1.jpg">
    <img src="img/2.jpg">
    <img src="img/3.jpg">
    <img src="img/4.jpg">
</div>

CSS部分

 #img-wrap{
            width:300px;
            height:190px;
            position: relative;
            overflow: hidden;
        }
       #img-wrap img{
           width:300px;
           height:auto;
           float:left;
           position:absolute;
           top:0;
           left:0;
           opacity: 0;// 完全透明
       }
        #img-wrap img:first-child{
            animation:h1 8s linear 0s infinite;
            -webkit-animation:h1 8s linear 0s infinite; // 自定義動畫名為h1,8s完成該動畫,勻速執行該動畫,立即執行,迴圈執行動畫 
        }
        #img-wrap img:nth-child(2){
            animation:h1 8s linear 2s infinite;
            -webkit-animation:h1 8s linear  2s infinite ;
        }
        #img-wrap img:nth-child(3){
            animation:h1 8s linear 4s infinite ;
            -webkit-animation:h1 8s linear  4s infinite  ;
        }
        #img-wrap img:nth-child(4){
            /*animation-name: h4;
            animation-delay: 0s;
            animation-duration: 8s;
            animation-iteration-count: infinite;
            animation-timing-function: linear;*/
            animation:h1 8s linear 6s infinite ;
            -webkit-animation:h1 8s linear  6s infinite  ;
        }

        @keyframes h1 {
                  0%{
                      opacity: 1;
                  }
                  25%{
                        opacity: 0;
                    }
            50%{
                opacity: 0;
            }
            75%{
                opacity: 0;
            }
            100%{
                opacity: 0;
            }
        }
        
[email protected]
h1 { 0%{ opacity: 1; } 25%{ opacity:0; } 50%{ opacity: 0; } 75%{ opacity: 0; } 100%{ opacity: 0; } }
實現思路

為每一張圖片新增一個動畫效果,動畫週期為8s,每張照片顯示的時間為2s。

依次使得第一到最後一張的照片的opacity為1(完全不透明,即顯示該張照片)其餘照片的opacity設為0(完全透明,即不顯示該照片)。

注意根據動畫的週期(總時長,和每張照片的顯示時間),給每張圖片設定適當的animation屬性。

一些點

CSS部分,也可以將opacity屬性替換為z-index屬性。

z-index值 大的 將放置在 值小的上層。

上面只是實現了 圖片依次顯示,像幻燈片的切換效果。但實際結果仍然較為生硬,整個過程不夠流暢。

如果想要實現圖片從左到右(從上到下、從下到上或者從右到左)的切換效果更為流暢,可以將使用left

(right/top/bottom)屬性 或者 transform:transform( a px) 屬性,適當調整 動畫執行效果,使圖片的切換更加流暢。

        完整程式碼如下

<div id="img-wrap">
    <img src="img/1.jpg">
    <img src="img/2.jpg">
    <img src="img/3.jpg">
    <img src="img/4.jpg">
</div>
 #img-wrap{
            width:300px;
            height:190px;
            position: relative;
            overflow: hidden;
        }
        #img-wrap img{
            width:300px;
            height:auto;
            float:left;
            position:absolute;
            top:0;
            left:0;
            /*opacity: 0;*/
            z-index:1;
        }
        #img-wrap img:first-child{
            animation:h1 8s linear 0s infinite;
            -webkit-animation:h1 8s linear 0s infinite;
        }
        #img-wrap img:nth-child(2){

            animation:h2 8s linear 0s infinite;
            -webkit-animation:h2 8s linear 0s infinite ;
        }
        #img-wrap img:nth-child(3){
            animation:h3 8s linear 0s infinite ;
            -webkit-animation:h3 8s linear  0s infinite  ;
        }
        #img-wrap img:nth-child(4){
            animation:h4 8s linear 0s infinite ;
            -webkit-animation:h4 8s linear  0s infinite  ;
        }

        @keyframes h1 {
            0%{
                left:-300px;
            }
            25%{
                left:0;
            }
            50%{
                left:0;
            }
            75%{
                left:0;
            }
            100%{
                left:0;
            }
        }

        @keyframes h2 {
            0%{
                left:-300px;

            }
            25%{
                left:-300px;

            }
            50%{
                left:0;

            }
            75%{
                left:0;

            }
            100%{
                left:0;

            }
        }
        @keyframes h3 {
            0%{
                left:-300px;

            }
            25%{
                left:-300px;

            }
            50%{
                left:-300px;

            }
            75%{
                left:0;

            }
            100%{
                left:0;

            }
        }
        @keyframes h4 {
            0%{
                left:-300px;

            }
            25%{
                left:-300px;

            }
            50%{
                left:-300px;

            }
            75%{
                left:-300px;

            }
            100%{
                left:0;

            }
        }


相關推薦

CSS實現圖片幻燈片式切換所有圖片輪流顯示每次顯示

HTML 部分<div id="img-wrap"> <img src="img/1.jpg"> <img src="img/2.jpg"> <img src="img/3.jpg"> <i

css實現文本不換行切超出限制時顯示省略號小tips

max 省略號 over 顯示 寬度 width code wid ellipsis div{ max-width: 500px; text-overflow: ellipsis; overflow: hidden; white-spac

JavaScript實現點選迴圈切換圖片

<!DOCTYPE html> <html><head><meta charset="UTF-8"><title>單擊迴圈切換圖片</title><script>var i = 1;func

js中onchange的使用實現功能選擇哪圖片顯示

pla option type oct 圖片 script www () height 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/x

h5,css實現下拉列表

gin clas isp pla mar ack 實現 寬高 doc <!DOCTYPE html><html><head><meta charset="utf-8"><style> ul li{ list-st

CSS 實現元素垂直居中

rst 實用 for 行內元素 bottom 浮動 100% scale 使用 作者:知乎用戶鏈接:https://www.zhihu.com/question/20543196/answer/275464838來源:知乎著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業

CSS 實現打印顯示底色

nbsp AS -a 用戶 方法 含義 技術分享 現實 ble 上一篇有講到如何在瀏覽器端實現打印功能。後面發現有個問題,就是表格表頭有背景顏色,但是實際打印出來無背景顏色。網上的方法主要有以下幾種實現方式: 1.把背景顏色寫成行內樣式,如下圖所示: 但是發現這樣子寫了

div css Firefox和IE瀏覽器相容問題 CSS實現cellSpacing屬性效果

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

pytorch實現多層感知機MLP)全連線神經網路FC分類MNIST手寫數字體的識別

1.匯入必備的包 1 import torch 2 import numpy as np 3 from torchvision.datasets import mnist 4 from torch import nn 5 from torch.autograd import Variable 6

實現的計算器最終版本可以實現括號以及浮點數的運算

1 #include <stdio.h> 2 #include <ctype.h> 3 #include <stdlib.h> 4 5 #define STACK_INIT_SIZE 20 6 #define STACKINCREMENT

css實現圓形波浪效果圖

在移動端經常看到一些圓形波浪圖來顯示金額,剛開始我認為這種效果只能用canvas寫的,後來發現用css也可以。 原理:我們都知道讓塊元素的border-radius:50%會變成圓形,如果少於50%呢,其實就變成不規則的圓形。我們可以利用這個特徵,用偽類加上transform動畫來實現波浪效

Fluentd實現收集日誌到HDFS

版權宣告:本文來自行者(http://blog.csdn.net/liuyuan185442111),轉載請註明出處。 https://blog.csdn.net/liuyuan185442111/article/details/47057571 Fluentd是一個實

CSS實現一個抽獎轉盤

效果 基本是用CSS實現的,沒有用圖片,加一丟丟JS。完全沒有考慮相容性。 首先畫一個轉盤, <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <ti

CSS實現下拉選單功能(過於冗餘此方法不建議)

<style> .dropdown{ position:relative; display:inline-block; } .dropbtn{ background-color:#4CAF50; color:white; padding:16

springmvc實現類似restful風格介面接收和返回為xml格式資料

1.配置檔案 在springMVC配置檔案中新增(以下兩句都為基本配置語句,基本一個普通的配置好的配置檔案都會有這兩個語句),不用配置那些奇奇怪怪的轉換器的原因是,裡面已經包含了很多轉換器的配置。 <!-- 還能實現同一資源 多種表述,按

ViewPager實現微信tab切換

佈局共三部分:top,bottom,中間。 top.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.and

java實現控制小球的移動監聽機制

效果圖如下:  通過上下左右來控制小球的移動 ,用到監聽機制,如果監聽機制不知道的話,可以看看這篇部落格https://blog.csdn.net/doubleguy/article/details/85198107 完整程式碼如下: package node; i

CSS實現表格背景顏色漸變效果

  用CSS實現表格背景顏色漸變效果 <style>td{text-align:center;color:white;"">宋體;font-size:14px;}table{table-layout:fixed;border:1 dashed blue;wi

java實現字母的大小寫轉換ASCII

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in);

css實現各種導航欄

基本的垂直導航條 通過HTML建立一個無序列表 <div> <ul class="nav"> <li><a href="#">home</a&g