JS實現(xiàn)單張或多張圖片持續(xù)無縫滾動的示例代碼
背景:
想要實現(xiàn)圖片持續(xù)滾動,既然使用js,就千萬不要加css動畫、過渡等相關(guān)樣式,如果想要滾動的平滑一下,可以一像素一像素的感動,則很平滑,如果加了過渡動畫,當(dāng)圖片重置為0時,會有往回倒的動畫效果,跟預(yù)期不符。
原理:
圖片滾動原理同圖片輪播原理,同樣也適用于文字滾動等一系列滾動,通過復(fù)制最后一張圖片或最后一堆文字插入第一行,或復(fù)制第一張圖片或一堆文字插入在結(jié)尾,來實現(xiàn)無縫拼接,前提:1、必須是沒有設(shè)置過渡動畫的,2、重置為0的時候與當(dāng)前已經(jīng)滾動到的高度對于圖片的位置而言肉眼看上去沒變化。
實現(xiàn):
html主要包含三塊:
1、最外層盒子,用來展示滾動圖的區(qū)域,overflow:hidden;
2、滾動的盒子,主要改變該盒子的定位值,來實現(xiàn)滾動,里面包含所有要滾動的圖片或文字
3、包含圖片或文字的盒子。
代碼:
class Roll { constructor(opts) { this.elem = opts.elem; // 圖片包含滾動長度的元素的 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; // 如果向上滾或者向左滾動每次減1,向下滾或者向右滾動每次加1 if(this.direction === 'top' || this.direction === 'left'){ this.speed = -1; }else{ this.speed = 1; } } roll(){ switch (this.direction) { case "top": // 如果滾動的盒子的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": // 如果滾動的盒子的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": // 如果滾動的盒子的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": // 如果滾動的盒子的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)向上滾動,如果滾動的盒子的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) } }
參考鏈接:
https://www.teakki.com/p/590beb7be8136dfc5f21770d
總結(jié)
到此這篇關(guān)于JS實現(xiàn)單張或多張圖片持續(xù)無縫滾動的文章就介紹到這了,更多相關(guān)js 圖片 無縫滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解JavaScript實現(xiàn)動態(tài)的輪播圖效果
這篇文章主要介紹了JavaScript實現(xiàn)動態(tài)的輪播圖效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04TypeScript中使用getElementXXX()的示例代碼
這篇文章主要介紹了TypeScript中使用getElementXXX()的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09javascript實現(xiàn)類似java中g(shù)etClass()得到對象類名的方法
這篇文章主要介紹了javascript實現(xiàn)類似java中g(shù)etClass()得到對象類名的方法,實例分析了javascript實現(xiàn)java中g(shù)etClass方法的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07超詳細(xì)小程序定位地圖模塊全系列開發(fā)教學(xué)
這篇文章主要介紹了超詳細(xì)小程序定位地圖模塊全系列開發(fā)教學(xué),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11js+html5實現(xiàn)canvas繪制鏤空字體文本的方法
這篇文章主要介紹了js+html5實現(xiàn)canvas繪制鏤空字體文本的方法,涉及html5文字效果的相關(guān)技巧,需要的朋友可以參考下2015-06-06JavaScript設(shè)計模式之工廠模式和抽象工廠模式定義與用法分析
這篇文章主要介紹了JavaScript設(shè)計模式之工廠模式和抽象工廠模式,結(jié)合實例形式分析了工廠模式的功能、定義、相關(guān)問題解決方法,并分析抽象工廠模式與工廠模式的不同之處,需要的朋友可以參考下2018-07-07