javascript單張多張圖無縫滾動(dòng)實(shí)例代碼
我們會(huì)看到很多的網(wǎng)站上會(huì)使用多張圖片無縫滾動(dòng)的效果。
下面我就介紹幾種純JS實(shí)現(xiàn)多張圖片的無縫滾動(dòng),并實(shí)現(xiàn)鼠標(biāo)移到圖片上運(yùn)動(dòng)停止的效果,可以控制圖片左右滾動(dòng)。
1.效果展示:
<!DOCTYPE html> <html> <head> <title>無縫滾動(dòng)</title> </head> <style type="text/css"> *{margin: 0;padding: 0;} #div1{position: relative;border:1px solid #0ff;width:1100px; height: 180px;margin:50px auto 0;overflow: hidden;} #div1 ul{position: absolute;left: 0;} #div1 ul li{list-style: none;width:200px;float: left;padding: 10px;height: 160px;} #div1 ul li img{width:100%;} </style> <script type="text/javascript"> window.onload=function(){ var oDiv=document.getElementById('div1'); var oUl=oDiv.getElementsByTagName('ul')[0]; var aLi=oUl.getElementsByTagName('li'); var aA=document.getElementsByTagName('a');//獲取向右向左的箭頭 var timer=null; var iSpeed=10; oUl.innerHTML+=oUl.innerHTML;//定義圖片可以循環(huán)播放 oUl.style.width=aLi.length*aLi[0].offsetWidth+'px';//定義外層ul的寬度,根據(jù)圖片的個(gè)數(shù)和每個(gè)圖片的寬度計(jì)算,保證總寬度是可調(diào)整的 function fnMove(){ if(oUl.offsetLeft<-oUl.offsetWidth/2){ oUl.style.left=0; }else if(oUl.offsetLeft>0){ oUl.style.left=-oUl.offsetWidth/2+'px'; }//定義到邊界的時(shí)候,實(shí)現(xiàn)無縫銜接 oUl.style.left=oUl.offsetLeft+iSpeed+'px'; //定義圖片的右邊距隨著速度不斷不斷增加,或減小,實(shí)現(xiàn)運(yùn)動(dòng)的效果 } timer=setInterval(fnMove,30); aA[0].onclick=function(){ iSpeed=-10; //按下左箭頭,定義向左運(yùn)動(dòng) } aA[1].onclick=function(){ iSpeed=10; //按下右箭頭,定義向右運(yùn)動(dòng) } oDiv.onmouseover=function(){ clearInterval(timer); //鼠標(biāo)移動(dòng)到圖片上,清除定時(shí)器,停止運(yùn)動(dòng) } oDiv.onmouseout=function(){ timer=setInterval(fnMove,30); //鼠標(biāo)移出,重新開啟定時(shí)器,重新運(yùn)動(dòng) } }; </script> <body> <a href="javascript:;" rel="external nofollow" rel="external nofollow" >←</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" >→</a> <div id="div1"> <ul> <li><img src="miaoflash/images/1.jpg"></li> <li><img src="miaoflash/images/2.jpg"></li> <li><img src="miaoflash/images/3.jpg"></li> <li><img src="miaoflash/images/4.jpg"></li> <li><img src="miaoflash/images/5.jpg"></li> <div style="clear: none;"></div> </ul> </div> </body> </html>
內(nèi)容補(bǔ)充:
背景:
想要實(shí)現(xiàn)圖片持續(xù)滾動(dòng),既然使用js,就千萬不要加css動(dòng)畫、過渡等相關(guān)樣式,如果想要滾動(dòng)的平滑一下,可以一像素一像素的感動(dòng),則很平滑,如果加了過渡動(dòng)畫,當(dāng)圖片重置為0時(shí),會(huì)有往回倒的動(dòng)畫效果,跟預(yù)期不符。
原理:
圖片滾動(dòng)原理同圖片輪播原理,同樣也適用于文字滾動(dòng)等一系列滾動(dòng),通過復(fù)制最后一張圖片或最后一堆文字插入第一行,或復(fù)制第一張圖片或一堆文字插入在結(jié)尾,來實(shí)現(xiàn)無縫拼接,前提:1、必須是沒有設(shè)置過渡動(dòng)畫的,2、重置為0的時(shí)候與當(dāng)前已經(jīng)滾動(dòng)到的高度對(duì)于圖片的位置而言肉眼看上去沒變化。
實(shí)現(xiàn):
html主要包含三塊:
1、最外層盒子,用來展示滾動(dòng)圖的區(qū)域,overflow:hidden;
2、滾動(dòng)的盒子,主要改變?cè)摵凶拥亩ㄎ恢担瑏韺?shí)現(xiàn)滾動(dòng),里面包含所有要滾動(dòng)的圖片或文字
3、包含圖片或文字的盒子。
代碼:
class Roll { constructor(opts) { this.elem = opts.elem; // 圖片包含滾動(dòng)長(zhǎng)度的元素的 this.elemBox = opts.elemBox; //圖片展示區(qū)域元素,為了獲取展示區(qū)域的高度 this.direction = opts.direction; this.time = opts.time; this.init(); this.roll = this.roll.bind(this) this.startRoll = this.startRoll.bind(this) this.stopRoll = this.stopRoll.bind(this) } init(){ this.elemHeight = this.elem.offsetHeight; this.elemHtml = this.elem.innerHTML; this.elem.innerHTML = this.elem.innerHTML + this.elemHtml+ this.elemHtml; this.speed; // 如果向上滾或者向左滾動(dòng)每次減1,向下滾或者向右滾動(dòng)每次加1 if(this.direction === 'top' || this.direction === 'left'){ this.speed = -1; }else{ this.speed = 1; } } roll(){ switch (this.direction) { case "top": // 如果滾動(dòng)的盒子的top值超出元素的高度,則置為0 if(Math.abs(this.elemBox.offsetTop) >= this.elemHeight){ this.elemBox.style.top = 0; }else{ this.elemBox.style.top = this.elemBox.offsetTop + this.speed + 'px'; } break; case "bottom": // 如果滾動(dòng)的盒子的bottom值超出元素的高度,則置為0 if(Math.abs(this.elemBox.offsetBottom) >= this.elemHeight){ this.elemBox.style.bottom = 0; }else{ this.elemBox.style.bottom = this.elemBox.offsetBottom + this.speed + 'px'; } break; case "left": // 如果滾動(dòng)的盒子的left超出元素的高度,則置為0 if(Math.abs(this.elemBox.offsetLeft) >= this.elemHeight){ this.elemBox.style.left = 0; }else{ this.elemBox.style.left = this.elemBox.offsetLeft + this.speed + 'px'; } break; case "right": // 如果滾動(dòng)的盒子的right超出元素的高度,則置為0 if(Math.abs(this.elemBox.offsetRight) >= this.elemHeight){ this.elemBox.style.right = 0; }else{ this.elemBox.style.right = this.elemBox.offsetRight + this.speed + 'px'; } break; default: // 默認(rèn)向上滾動(dòng),如果滾動(dòng)的盒子的top超出元素的高度,則置為0 if(Math.abs(this.elemBox.offsetTop) >= this.elemHeight){ this.elemBox.style.top = 0; }else{ this.elemBox.style.top = this.elemBox.offsetTop + speed + 'px'; } } } stopRoll(){ clearInterval(this.scrollTimer) } startRoll(){ this.scrollTimer = setInterval(this.roll,this.time) } }
以上就是javascript單張多張圖無縫滾動(dòng)實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于javascript圖片無縫滾動(dòng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 基于javascript的無縫滾動(dòng)動(dòng)畫1
- JS實(shí)現(xiàn)單張或多張圖片持續(xù)無縫滾動(dòng)的示例代碼
- 純js實(shí)現(xiàn)無縫滾動(dòng)功能代碼實(shí)例
- JavaScript基于面向?qū)ο髮?shí)現(xiàn)的無縫滾動(dòng)輪播示例
- 原生JavaScript實(shí)現(xiàn)的無縫滾動(dòng)功能詳解
- js實(shí)現(xiàn)無縫滾動(dòng)雙圖切換效果
- js圖片無縫滾動(dòng)插件使用詳解
- JS實(shí)現(xiàn)簡(jiǎn)單的文字無縫上下滾動(dòng)功能示例
- JavaScript實(shí)現(xiàn)圖片無縫滾動(dòng)效果
- js實(shí)現(xiàn)文字列表無縫滾動(dòng)效果
- js輪播圖無縫滾動(dòng)效果
- 基于javascript的無縫滾動(dòng)動(dòng)畫實(shí)現(xiàn)2
相關(guān)文章
BootStrap selectpicker后臺(tái)動(dòng)態(tài)綁定數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了BootStrap selectpicker后臺(tái)動(dòng)態(tài)綁定數(shù)據(jù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Vant+postcss-pxtorem 實(shí)現(xiàn)瀏覽器適配功能
這篇文章主要介紹了Vant+postcss-pxtorem 實(shí)現(xiàn)瀏覽器適配,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02JavaScript面向?qū)ο缶幊虒?shí)現(xiàn)模擬
面向?qū)ο缶幊?Object Oriented Programming)將現(xiàn)實(shí)世界中的復(fù)雜關(guān)系抽象成一個(gè)個(gè)對(duì)象,通過對(duì)象之間的分工合作對(duì)現(xiàn)實(shí)世界進(jìn)行模擬2022-10-10微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07postMessage消息通信Promise化的方法實(shí)現(xiàn)
postMessage Api 想必大家都不陌生,WebWorker 通信會(huì)用到,iframe 窗口之間通信也會(huì)用到,那么我們能不能將 postMessage 進(jìn)行一次轉(zhuǎn)化,把他變成類似 Promise 的使用方式,所以本文給大家介紹了postMessage消息通信Promise化的方法實(shí)現(xiàn),需要的朋友可以參考下2024-03-03JS+CSS實(shí)現(xiàn)簡(jiǎn)易的滑動(dòng)門效果代碼
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)簡(jiǎn)易的滑動(dòng)門效果代碼,涉及JavaScript動(dòng)態(tài)遍歷及修改頁面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09微信小程序左滑刪除實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了微信小程序左滑刪除實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09JS動(dòng)態(tài)調(diào)用方法名示例介紹
在JS中如何動(dòng)態(tài)調(diào)用方法名,想必很多的朋友們都不會(huì)吧,下面為大家舉例介紹下具體的調(diào)用方法2013-12-12javascript面向?qū)ο笕筇卣髦^承實(shí)例詳解
這篇文章主要介紹了javascript面向?qū)ο笕筇卣髦^承,簡(jiǎn)單描述了繼承的概念、原理,并結(jié)合實(shí)例形式詳細(xì)分析了繼承的常見實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2019-07-07