本文主要為大家帶來一篇實作圖片首尾平滑輪播(JS原生方法—節流)。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。
先給HTML程式碼,要注意輪播圖片表(#list)結尾加上第一個圖片1.jpg,在首部加上最後一張圖片5.jpg。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首尾轮播</title> <link rel="stylesheet" href="首尾轮播.css" rel="external nofollow" > <script src="首尾轮播.js"></script> </head> <body> <p id="container"> <p id="list"> <p><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/5.jpg" alt=""></a></p> <p><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/1.jpg" alt=""></a></p> <p><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/2.jpg" alt=""></a></p> <p><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/3.jpg" alt=""></a></p> <p><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/4.jpg" alt=""></a></p> <p><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/5.jpg" alt=""></a></p> <p><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/1.jpg" alt=""></a></p> </p> <p class="arrow" id="prev"><</p> <p class="arrow" id="next">></p> </p> </body> </html>
接下來給出css和js程式碼,大家可以酌情根據圖片的大小、數量、寬度,調整container、list的參數,這也是封裝JS所要考慮的參數。
*{ margin: 0; padding: 0; } #container{ height: 400px; width: 500px; margin: 0 auto; position: relative; overflow: hidden; border: 1px solid black; } #list>p { float: left; } #list{ position: absolute; height: 400px; width: 3600px; } #list img{ height: 400px; width: 500px; } .arrow { user-select:none; position: absolute; top:150px; z-index: 2; background-color: #aaa; height: 100px; width: 80px; cursor: pointer; opacity: 0.5; display: none; line-height: 100px; text-align: center; color: #222; font-size: 3em; } #container:hover .arrow{ display: block; } #prev{ left:20px; } #next{ right: 20px; }
在JS中,我們可以透過改變speed變數來控制圖片切換的速度....這裡用的是element.style.transition來控制的過渡效果.
window.onload=function(){ var container = document.getElementById('container'); var list = document.getElementById('list');//获取图片容器 var prev = document.getElementById('prev');//向前按钮 var next = document.getElementById('next');//向后按钮 var nowP = 1; //显示位置 var judge = null; //执行权 var speed = 0.1; // 切换速度 秒 prev.onclick=function(){ if(!judge){ judge = setTimeout(function(){ if(nowP==1){setTimeout(function(){ list.style.transition="left 0s"; list.style.left = "-2500px"; nowP = 5;},speed*1000);} //当到达图片表左边缘时与动画同步切换 list.style.transition = "left "+speed+"s"; move(500); nowP--; judge = null; },speed*1000); } }; next.onclick=function(){ if(!judge){ judge = setTimeout(function(){ if(nowP==5){setTimeout(function(){ list.style.transition="left 0s"; list.style.left = "-500px"; nowP = 1;},speed*1000);} //当到达图片表右边缘时与动画同步切换 list.style.transition = "left "+speed+"s"; move(-500); nowP++; judge = null; },speed*1000); } }; function move(num){ var term = parseInt(list.style.left) + num ; list.style.left = term+"px"; } var roll= setInterval(function(){ next.onclick(); },2000); container.onmouseenter=function(){ clearInterval(roll); }; container.onmouseleave=function() { roll=setInterval(function(){ next.onclick(); },2000); }; };
下面是一個示範demo,簡單來說原理就是在切換到圖片表首和表尾的動畫開始時,設定一個延遲執行時間與動畫過渡時間相同的setTimeout函數來執行瞬間切換,再透過節流來確保最大的切換速度(speed)。
本身也是初學前端,如果有幫助的話,點一下推薦吧
#相關推薦:
以上是js實作圖片首尾平滑輪播的詳細內容。更多資訊請關注PHP中文網其他相關文章!