国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

原生js和css實現圖片輪播效果

2019-11-19 17:41:50
字體:
來源:轉載
供稿:網友

本文實例為大家分享了javascript圖片輪播效果的具體代碼,供大家參考,具體內容如下

<!DOCTYPE HTML> <html lang="zh-CN">  <head>   <meta charset="utf-8">   <title>圖片輪播</title>      <style>     #box {       width:506px;       height:306px;       margin: 20px auto;       border:3px solid black;       position:relative;       background-color:orange;       overflow: hidden;       /*overflow: hidden;*/     }     .pic {       position: absolute;       width:500px;       height:300px;       line-height: 300px;       text-align: center;       font-size: 100px;       color:white;       bottom:0;     }     .red {       background-color:red;     }     .green {       background-color:green;     }     .blue {       background-color:blue;     }     .orange{       background-color: orange;     }     .move {       bottom:300px;       transition:bottom 3s; /* 設置圖片移動消耗的時間*/     }   </style> </head>  <body>   <div id="box">     <div id="pic1" class="pic red">1</div>     <div id="pic2" class="pic green">2</div>     <div id="pic3" class="pic blue">3</div>     <div id="pic3" class="pic orange">4</div>   </div>         <script>     window.addEventListener('load',function(){       var pics = document.getElementsByClassName('pic');              //為每個pic元素設置z-index的值       for(let i=0;i<pics.length;i++){         pics[i].style.zIndex = pics.length-i;       }              //循環播放圖片的函數       var loopPics = (function(){         var index=0;         return function(pics,delay){           var recall = function(pic){             //給圖片增加move類,調用css的transition屬性播放移動動畫             pic.className += ' move';             setTimeout(function(){               //取消圖片的move類,圖片返回原位               pic.className=pic.className.replace(' move','');               //改變圖片組的堆疊順序。最外的圖片放到最下面,其他圖片依次向外移動               for(let i=0;i<pics.length;i++){                 if(pics[i].style.zIndex==pics.length){                   pics[i].style.zIndex=1;                 } else {                   pics[i].style.zIndex=pics[i].style.zIndex*1+1;                 }               }               index++;               if(index==pics.length) index=0;               recall(pics[index]);             },delay);           };           recall(pics[index]);         };       })();       //調用函數,循環播放。delay的時間需要大于等于css動畫里設置的圖片移動時間       loopPics(pics,4000);     });    </script> </body>  </html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 裕民县| 尚义县| 青河县| 靖安县| 杭锦后旗| 建始县| 当阳市| 城市| 胶州市| 临猗县| 连州市| 镇原县| 古丈县| 巍山| 胶南市| 额尔古纳市| 宿州市| 绥宁县| 南漳县| 临猗县| 防城港市| 西华县| 延庆县| 临沭县| 石柱| 古蔺县| 汶上县| 临朐县| 威远县| 宁陕县| 宝兴县| 孟州市| 佛山市| 亳州市| 涿州市| 松阳县| 清原| 黄浦区| 隆回县| 赫章县| 耿马|