JavaScript輪播停留效果的實(shí)現(xiàn)思路
一、思路
1.輪播停留與無線滾動(dòng)十分類似,都是利用屬性及變量控制移動(dòng)實(shí)現(xiàn)輪播;
2.不同的是輪播停留需要添加過渡屬性搭配定時(shí)器即可實(shí)現(xiàn)輪播停留效果;
二、步驟
1.寫基本結(jié)構(gòu)樣式
需在末尾多添加一張與第一張相同的圖片,消除切換時(shí)的抖動(dòng);
2.添加輪播停留事件 有了之前的基礎(chǔ),直接添加索引圈默認(rèn)事件到輪播停留事件內(nèi);
注意:當(dāng)輪播到最后一張時(shí),需要消除掉過渡,這里使用setTimeout定時(shí)器,卡最后一張圖片輪播完不延時(shí),直接跳轉(zhuǎn)到第一張,由于第一張和最后一張一樣,所以會(huì)形成視覺盲區(qū),看起來是連續(xù)輪播效果;
//輪播停留方法 function move() { box.className = "box anmint"; circle[count].style.backgroundColor = ""; count++; box.style.marginLeft = (-800 * count) + "px"; //最后一張走完之后,執(zhí)行一次定時(shí)器不循環(huán),卡過渡時(shí)間,消除切換 setTimeout(function () { if (count >= 6) { count = 0; box.className = "box"; //marginLeft=0之前去除過渡屬性 box.style.marginLeft = "0px"; } circle[count].style.backgroundColor = "red"; }, 500); }
3.添加進(jìn)入索引圈事件
這和淡入淡出進(jìn)入索引圈事件基本一致,不同的是這里不用調(diào)用輪播停留事件,直接利用當(dāng)前index來索引使圖片跟隨變換;注意最后要標(biāo)記count=this.index
值,令再次執(zhí)行默認(rèn)行為時(shí)是緊跟著當(dāng)前顯示圖片向后執(zhí)行默認(rèn)行為;
//進(jìn)入索引圈事件 for(var j=0;j<circle.length;j++){ circle[j].index=j; circle[j].onmouseenter=function(){ for(var k=0;k<circle.length;k++){ circle[k].style.backgroundColor=""; } this.style.backgroundColor="red"; //圖片跟隨移動(dòng) box.className="box anmint"; box.style.marginLeft=(-800*this.index)+"px"; count=this.index; } }
4.完善鼠標(biāo)進(jìn)入離開代碼
效果圖:
完整代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS輪播停留效果</title> <style> *{margin: 0;padding: 0;} html,body{width: 100%;height: 100%;} .block{ width: 800px; height: 400px; margin: 80px auto; position: relative; border: 1px solid red; overflow: hidden; } .box{ width: 5600px; height: 400px; float: left; } .anmint{ transition: all 0.5s ease-in-out; } img{ width: 800px; height: 400px; float: left; } .cir{ width: 150px; height: 20px; z-index: 7; position: absolute; bottom: 10px; left: 320px; } .circle{ width: 10px; height: 10px; border: 2px solid grey; border-radius: 50%; float: left; margin: 0 5px; } </style> <script> window.onload=function(){ var box=document.getElementsByClassName("box")[0]; var count=0; //索引圈事件 var circle=document.getElementsByClassName("circle"); circle[0].style.backgroundColor="red"; var time=setInterval(function(){ move(); },2000); //鼠標(biāo)進(jìn)入事件 var block=document.getElementsByClassName("block")[0]; block.onmouseenter=function(){ clearInterval(time); }; //鼠標(biāo)離開事件 block.onmouseleave=function(){ time=setInterval(function(){ move(); },2000); }; //進(jìn)入索引圈事件 for(var j=0;j<circle.length;j++){ circle[j].index=j; circle[j].onmouseenter=function(){ for(var k=0;k<circle.length;k++){ circle[k].style.backgroundColor=""; } this.style.backgroundColor="red"; //圖片跟隨移動(dòng) box.className="box anmint"; box.style.marginLeft=(-800*this.index)+"px"; count=this.index; } } //輪播停留方法 function move() { box.className = "box anmint"; circle[count].style.backgroundColor = ""; count++; box.style.marginLeft = (-800 * count) + "px"; //最后一張走完之后,執(zhí)行一次定時(shí)器不循環(huán),卡過渡時(shí)間,消除切換 setTimeout(function () { if (count >= 6) { count = 0; box.className = "box"; //marginLeft=0之前去除過渡屬性 box.style.marginLeft = "0px"; } circle[count].style.backgroundColor = "red"; }, 500); } } </script> </head> <body> <div class="block"> <div class="box"> <img class="imgg" src="./image/box1.jpg"> <img class="imgg" src="./image/box2.jpg"> <img class="imgg" src="./image/box3.jpg"> <img class="imgg" src="./image/box4.jpg"> <img class="imgg" src="./image/box5.jpg"> <img class="imgg" src="./image/box6.jpg"> <img class="imgg" src="./image/box1.jpg"> </div> <div class="cir"> <div class="circle"></div> <div class="circle"></div> <div class="circle"></div> <div class="circle"></div> <div class="circle"></div> <div class="circle"></div> </div> </div> </body> </html>
總結(jié)
以上所述是小編給大家介紹的JavaScript輪播停留效果的思路詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
前端JS,刪除JSON數(shù)據(jù)(JSON數(shù)組)中的指定元素方式
這篇文章主要介紹了前端JS,刪除JSON數(shù)據(jù)(JSON數(shù)組)中的指定元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Bootstrap實(shí)現(xiàn)默認(rèn)導(dǎo)航欄效果
這篇文章主要介紹了Bootstrap實(shí)現(xiàn)默認(rèn)導(dǎo)航欄效果,導(dǎo)航欄是一個(gè)很好的功能,是Bootstrap網(wǎng)站的一個(gè)突出特點(diǎn),本文帶領(lǐng)大家學(xué)習(xí)實(shí)現(xiàn)Bootstrap導(dǎo)航欄,需要的朋友可以參考下2015-12-12js對(duì)象合并的4種方式與數(shù)組合并的4種方式
這篇文章主要介紹了js對(duì)象合并的4種方式與數(shù)組合并的4種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10關(guān)于在Typescript中做錯(cuò)誤處理的方式詳解
錯(cuò)誤處理是軟件工程重要的一部分,如果處理得當(dāng),它可以為你節(jié)省數(shù)小時(shí)的調(diào)試和故障排除時(shí)間,我發(fā)現(xiàn)了與錯(cuò)誤處理相關(guān)的三大疑難雜癥:TypeScript的錯(cuò)誤類型,變量范圍和嵌套,讓我們逐一深入了解它們帶來的撓頭問題,感興趣的朋友可以參考下2023-09-09微信小程序利用swiper+css實(shí)現(xiàn)購(gòu)物車商品刪除功能
這篇文章主要介紹了微信小程序利用swiper+css實(shí)現(xiàn)購(gòu)物車商品刪除功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03微信小程序用戶登錄和登錄態(tài)維護(hù)的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序用戶登錄和登錄態(tài)維護(hù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12js console.log打印對(duì)象時(shí)屬性缺失的解決方法
在編寫代碼時(shí),我們常常用 console.log() 的方式將信息在控制臺(tái)中打印出來以幫助我們進(jìn)行前端調(diào)試,那么console.log打印對(duì)象時(shí)屬性缺失怎么辦?下面我們就一起來了解一下解決方法2019-05-05JavaScript實(shí)現(xiàn)瀏覽器內(nèi)多個(gè)標(biāo)簽頁(yè)之間通信
這篇文章主要為大家詳細(xì)介紹了JavaScript如何實(shí)現(xiàn)瀏覽器內(nèi)多個(gè)標(biāo)簽頁(yè)之間通信,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04