国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

JS實現單張或多張圖片持續無縫滾動的示例代碼

瀏覽:101日期:2024-05-09 08:33:57

背景:

想要實現圖片持續滾動,既然使用js,就千萬不要加css動畫、過渡等相關樣式,如果想要滾動的平滑一下,可以一像素一像素的感動,則很平滑,如果加了過渡動畫,當圖片重置為0時,會有往回倒的動畫效果,跟預期不符。

原理:

圖片滾動原理同圖片輪播原理,同樣也適用于文字滾動等一系列滾動,通過復制最后一張圖片或最后一堆文字插入第一行,或復制第一張圖片或一堆文字插入在結尾,來實現無縫拼接,前提:1、必須是沒有設置過渡動畫的,2、重置為0的時候與當前已經滾動到的高度對于圖片的位置而言肉眼看上去沒變化。

實現:

html主要包含三塊:

1、最外層盒子,用來展示滾動圖的區域,overflow:hidden;

2、滾動的盒子,主要改變該盒子的定位值,來實現滾動,里面包含所有要滾動的圖片或文字

3、包含圖片或文字的盒子。

代碼:

class Roll { constructor(opts) { this.elem = opts.elem; // 圖片包含滾動長度的元素的 this.elemBox = opts.elemBox; //圖片展示區域元素,為了獲取展示區域的高度 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值超出元素的高度,則置為0if(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值超出元素的高度,則置為0if(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超出元素的高度,則置為0if(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超出元素的高度,則置為0if(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:// 默認向上滾動,如果滾動的盒子的top超出元素的高度,則置為0if(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

總結

到此這篇關于JS實現單張或多張圖片持續無縫滾動的文章就介紹到這了,更多相關js 圖片 無縫滾動內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 日韩a毛片免费全部播放完整 | 性精品 | 午夜影院a级片 | 久久精品亚洲乱码伦伦中文 | 91黑丝国产线观看免费 | 99re国产视频| 成年性午夜免费视频网站不卡 | 一个色综合久久 | 欧美性猛交xxxxxxxx软件 | 一级黄网站| 中文字幕精品在线 | 美女美女大片黄a大片 | 亚洲综合第一欧美日韩中文 | 免费又黄又爽视频 | 欧美在线观看不卡 | 亚洲a级在线观看 | 一区二区三区在线播放视频 | 颜值超高的女神啪啪 | 亚洲高清无在码在线无弹窗 | 欧美人成在线观看网站高清 | 久久免费精品视频在线观看 | 国产久草在线 | 精品成人免费一区二区在线播放 | 欧美深夜在线 | 久久爽久久爽久久免费观看 | 国产91精品一区二区 | 加勒比在线免费视频 | 亚洲第一狼人区 | 中文乱码字幕午夜无线观看 | 欧美怡红院高清在线 | 久久精品国产一区 | 亚洲成在线观看 | 天天躁日日躁狠狠躁一级毛片 | 精品国产看高清国产毛片 | 草久网 | 国产成年网站v片在线观看 国产成人aa在线视频 | 亚洲一区二区三区精品视频 | 小毛片在线观看 | 色香欲综合成人免费视频 | 国产欧美另类性视频 | 成熟女人免费一级毛片 |