溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Javascript實(shí)現(xiàn)圖片無縫滾動效果

發(fā)布時間:2020-05-18 16:29:29 來源:億速云 閱讀:239 作者:Leah 欄目:web開發(fā)

今天小編就為大家?guī)硪黄嘘P(guān)Javascript實(shí)現(xiàn)圖片無縫滾動效果的文章。小編覺得挺實(shí)用的,為此分享給大家做個參考。一起跟隨小編過來看看吧。

實(shí)現(xiàn)原理:

圖片滾動原理同圖片輪播原理,同樣也適用于文字滾動等一系列滾動,通過復(fù)制最后一張圖片或最后一堆文字插入第一行,或復(fù)制第一張圖片或一堆文字插入在結(jié)尾,來實(shí)現(xiàn)無縫拼接。

前提:1、必須是沒有設(shè)置過渡動畫的,2、重置為0的時候與當(dāng)前已經(jīng)滾動到的高度對于圖片的位置而言肉眼看上去沒變化。

實(shí)現(xiàn):

html主要包含三塊:

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

2、滾動的盒子,主要改變該盒子的定位值,來實(shí)現(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)
    }
}

看完上述內(nèi)容,你們掌握Javascript實(shí)現(xiàn)圖片無縫滾動效果的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI