站長資訊網
        最全最豐富的資訊網站

        javascript實現單張或多張圖片持續無縫滾動

        javascript實現單張或多張圖片持續無縫滾動

        背景:

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

        推薦:《javascript高級教程

        原理:

        圖片滾動原理同圖片輪播原理,同樣也適用于文字滾動等一系列滾動,通過復制最后一張圖片或最后一堆文字插入第一行,或復制第一張圖片或一堆文字插入在結尾,來實現無縫拼接,前提: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值超出元素的高度,則置為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:                 // 默認向上滾動,如果滾動的盒子的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)     } }

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 伊人精品久久久久7777| 国产成人精品一区二区三区免费| 亚洲精品一级无码中文字幕| 九九在线精品视频专区| 亚洲国产精品VA在线观看麻豆| 97久久精品人人澡人人爽| 精品久久无码中文字幕| 亚洲AV日韩精品一区二区三区| 91亚洲精品麻豆| 国产一区二区精品久久| 精品不卡一区二区| 无码国内精品人妻少妇| 亚洲国产精品尤物yw在线| 久久97久久97精品免视看秋霞| 777久久精品一区二区三区无码| 99久久99这里只有免费的精品| 日韩精品无码一本二本三本| 亚洲?V无码乱码国产精品| 精品一区二区三区色花堂| 国产成人亚洲精品影院| 51久久夜色精品国产| 精品免费视在线观看| 99精品久久精品| 国精品无码一区二区三区在线| 四虎影视永久在线观看精品| 亚洲综合精品香蕉久久网| 亚洲午夜精品第一区二区8050| 无码8090精品久久一区| 思思久久99热免费精品6| 久久99精品九九九久久婷婷| 国产香蕉国产精品偷在线观看| 国产精品激情综合久久 | AAA级久久久精品无码区| 亚洲国产精品婷婷久久| 久久精品国产福利国产秒| 久久最新精品国产| 欧美精品一区二区精品久久| 久久国产精品久久久| 91精品国产91久久| 国产精品推荐天天看天天爽 | 91精品美女在线|