JS實(shí)現(xiàn)單張或多張圖片持續(xù)無(wú)縫滾動(dòng)的示例代碼
背景:
想要實(shí)現(xiàn)圖片持續(xù)滾動(dòng),既然使用js,就千萬(wàn)不要加css動(dòng)畫(huà)、過(guò)渡等相關(guān)樣式,如果想要滾動(dòng)的平滑一下,可以一像素一像素的感動(dòng),則很平滑,如果加了過(guò)渡動(dòng)畫(huà),當(dāng)圖片重置為0時(shí),會(huì)有往回倒的動(dòng)畫(huà)效果,跟預(yù)期不符。
原理:
圖片滾動(dòng)原理同圖片輪播原理,同樣也適用于文字滾動(dòng)等一系列滾動(dòng),通過(guò)復(fù)制最后一張圖片或最后一堆文字插入第一行,或復(fù)制第一張圖片或一堆文字插入在結(jié)尾,來(lái)實(shí)現(xiàn)無(wú)縫拼接,前提:1、必須是沒(méi)有設(shè)置過(guò)渡動(dòng)畫(huà)的,2、重置為0的時(shí)候與當(dāng)前已經(jīng)滾動(dòng)到的高度對(duì)于圖片的位置而言肉眼看上去沒(méi)變化。
實(shí)現(xiàn):
html主要包含三塊:
1、最外層盒子,用來(lái)展示滾動(dòng)圖的區(qū)域,overflow:hidden;
2、滾動(dòng)的盒子,主要改變?cè)摵凶拥亩ㄎ恢?,?lái)實(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)
}
}
參考鏈接:
https://www.teakki.com/p/590beb7be8136dfc5f21770d
總結(jié)
到此這篇關(guān)于JS實(shí)現(xiàn)單張或多張圖片持續(xù)無(wú)縫滾動(dòng)的文章就介紹到這了,更多相關(guān)js 圖片 無(wú)縫滾動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于javascript的無(wú)縫滾動(dòng)動(dòng)畫(huà)1
- javascript單張多張圖無(wú)縫滾動(dòng)實(shí)例代碼
- 純js實(shí)現(xiàn)無(wú)縫滾動(dòng)功能代碼實(shí)例
- JavaScript基于面向?qū)ο髮?shí)現(xiàn)的無(wú)縫滾動(dòng)輪播示例
- 原生JavaScript實(shí)現(xiàn)的無(wú)縫滾動(dòng)功能詳解
- js實(shí)現(xiàn)無(wú)縫滾動(dòng)雙圖切換效果
- js圖片無(wú)縫滾動(dòng)插件使用詳解
- JS實(shí)現(xiàn)簡(jiǎn)單的文字無(wú)縫上下滾動(dòng)功能示例
- JavaScript實(shí)現(xiàn)圖片無(wú)縫滾動(dòng)效果
- js實(shí)現(xiàn)文字列表無(wú)縫滾動(dòng)效果
- js輪播圖無(wú)縫滾動(dòng)效果
- 基于javascript的無(wú)縫滾動(dòng)動(dòng)畫(huà)實(shí)現(xiàn)2
相關(guān)文章
使用JavaScript實(shí)現(xiàn)一個(gè)炫酷的羅盤(pán)時(shí)鐘
在探究前端動(dòng)畫(huà)時(shí),想到之前在鎖屏壁紙看到的羅盤(pán)時(shí)鐘,看著很是炫酷,于是說(shuō)干就干,下面就跟隨小編一起來(lái)學(xué)習(xí)一下如何使用JS實(shí)現(xiàn)一個(gè)炫酷的羅盤(pán)時(shí)鐘效果吧2024-02-02
微信小程序開(kāi)發(fā)之toast等彈框提示使用教程
彈框提示是我們?cè)陂_(kāi)發(fā)中經(jīng)常用的一個(gè)效果,下面這篇文章主要給大家介紹了微信小程序開(kāi)發(fā)之toast等彈框提示實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
跨瀏覽器的 mouseenter mouseleave 以及 compareDocumentPosition的使用說(shuō)明
昨天去 大牛 司徒正美 的blog 看博文 突然看到 關(guān)于 onmouseenter 和onmouseleave 兩個(gè)ie專(zhuān)有事件..2010-05-05
layui數(shù)據(jù)表格 table.render 報(bào)錯(cuò)的解決方法
今天小編就為大家分享一篇layui數(shù)據(jù)表格 table.render 報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
BootstrapTable+KnockoutJS相結(jié)合實(shí)現(xiàn)增刪改查解決方案(三)兩個(gè)Viewmodel搞定增刪改查
這篇文章主要介紹了BootstrapTable+KnockoutJS相結(jié)合實(shí)現(xiàn)增刪改查解決方案(三)兩個(gè)Viewmodel搞定增刪改查 的相關(guān)資料,需要的朋友可以參考下2016-08-08

