站長(zhǎng)資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        javascript實(shí)現(xiàn)單張或多張圖片持續(xù)無(wú)縫滾動(dòng)

        javascript實(shí)現(xiàn)單張或多張圖片持續(xù)無(wú)縫滾動(dòng)

        背景:

        想要實(shí)現(xiàn)圖片持續(xù)滾動(dòng),既然使用js,就千萬(wàn)不要加css動(dòng)畫、過渡等相關(guān)樣式,如果想要滾動(dòng)的平滑一下,可以一像素一像素的感動(dòng),則很平滑,如果加了過渡動(dòng)畫,當(dāng)圖片重置為0時(shí),會(huì)有往回倒的動(dòng)畫效果,跟預(yù)期不符。

        推薦:《javascript高級(jí)教程

        原理:

        圖片滾動(dòng)原理同圖片輪播原理,同樣也適用于文字滾動(dòng)等一系列滾動(dòng),通過復(fù)制最后一張圖片或最后一堆文字插入第一行,或復(fù)制第一張圖片或一堆文字插入在結(jié)尾,來(lái)實(shí)現(xiàn)無(wú)縫拼接,前提:1、必須是沒有設(shè)置過渡動(dòng)畫的,2、重置為0的時(shí)候與當(dāng)前已經(jīng)滾動(dòng)到的高度對(duì)于圖片的位置而言肉眼看上去沒變化。

        實(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)     } }

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 久久精品国产亚洲AV嫖农村妇女| 精品无码AV一区二区三区不卡 | 999精品在线| 精品视频久久久久| 亚洲精品线在线观看| 国产精品无码AV一区二区三区| 日本精品在线视频| 国产精品久久久久一区二区三区 | 亚洲日韩国产AV无码无码精品| 亚洲国产精品专区在线观看| 亚洲精品国产精品国自产观看| 亚洲国产综合精品中文第一区| 久久精品黄AA片一区二区三区| 亚洲精品人成无码中文毛片| 精品国产三级a乌鸦在线观看| 亚洲国产精品婷婷久久| 99久久精品国产高清一区二区| 欧美精品v欧洲精品| 亚洲韩国精品无码一区二区三区 | 亚洲精品午夜国产VA久久成人| 久久精品国产99国产精品| 国产成人综合久久精品尤物| 久久www免费人成精品香蕉| 一区二区三区日韩精品| 国产精品久久久久影视不卡| 国产午夜精品一区二区三区不卡| 欧美一卡2卡3卡四卡海外精品| 99re这里只有精品6| 国产成人精品视频一区二区不卡| 欧美日韩人妻精品一区二区在线| 宅男在线国产精品无码| 国产精品视频一区国模私拍| 精品一区二区三区在线成人| 日韩精品乱码AV一区二区| 久久这里只有精品18| 久久香蕉国产线看观看精品yw| 午夜欧美精品久久久久久久| 亚洲国产精品一区第二页| 亚洲AV无码精品无码麻豆| 亚洲精品~无码抽插| 精品无码AV无码免费专区|