当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js实现单张或多张图片持续无缝滚动

js实现单张或多张图片持续无缝滚动

2020年05月09日  | 移动技术网IT编程  | 我要评论

背景:

想要实现图片持续滚动,既然使用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值超出元素的高度,则置为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)
    }
}

 

参考链接:https://www.teakki.com/p/590beb7be8136dfc5f21770d

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网