您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關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> <div id="container"> <div id="list"> <div><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></div> <div><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></div> <div><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></div> <div><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></div> <div><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></div> <div><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></div> <div><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></div> </div> <div class="arrow" id="prev"><</div> <div class="arrow" id="next">></div> </div> </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>div { 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如何實現圖片首尾平滑輪播”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。