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

摘要:
原理:图片滚动原理同图片轮播原理,同样也适用于文字滚动等一系列滚动,通过复制最后一张图片或最后一堆文字插入第一行,或复制第一张图片或一堆文字插入在结尾,来实现无缝拼接,前提:1、必须是没有设置过渡动画的,2、重置为0的时候与当前已经滚动到的高度对于图片的位置而言肉眼看上去没变化。

背景:

想要实现图片持续滚动,既然使用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

免责声明:文章转载自《js实现单张或多张图片持续无缝滚动》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇目标函数、损失函数、代价函数Netty源码分析之ChannelPipeline(二)—ChannelHandler的添加与删除下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CharacterController 中Move 和 SimpleMove的区别分析

CollisionFlagsMove(Vector3 motion); Description A more complex move function taking absolute movement deltas; Attempts to move the controller by mothon will only be constrained by...

Top应用选项及SQL执行顺序

写在前面的话 我们常常使用top来查询前几条语句,或使用嵌套的top的方式获取分页的数据,那么我们对top及SQL执行的顺序真正了解吗. 具体如下例. 实例 建表toptest,表结构如下: 列 类型 sid int sname nvarchar(50) 建表语句如下: create table toptest ( sid int...

性能优化工具---top

作用:   实时显示linux下各个进程的资源占用情况 参数:   -d :后面可以接秒数,就是整个程序画面更新的秒数。预设是 5 秒;   -p :指定某些个 PID 来进行观察监测而已。   -b :以批次的方式执行 top ,通常会搭配数据流重定向到指定输出。   -n :与 -b 搭配,意义是,需要进行几次 top 的输出结果。 在 top 执行过...

利用CSS实现带相同间隔地无缝滚动动画

说明:因为在移动上主要利用CSS来做动画,所以没有考虑其他浏览器的兼容性,只有-webkit这个前缀,如果需要其他浏览器,请自行补齐。 首先解释一下什么是无缝滚动动画, 例如下面的例子 See the Pen css实现上下循环滚动效果 by lilyH (@lilyH) on CodePen. ------------------------------...

linux top 命令

top 命令主要用于查看进程的相关信息,同时它也会提供系统平均负载,cpu 信息和内存信息。下面的截图展示了 top 命令默认提供的信息: 系统平均负载top 命令输出中的第一行是系统的平均负载,这和 uptime 命令的输出是一样的: 13:05:49      表示系统当前时间。up 7 days    表示系统最后一次启动后总的运行时间。1 us...

WPF 2D绘图(1)Shape

Shape画图形 特点: 使用XAML输出 继承自FrameworkElement的,因此拥有了众多的属性和事件 1.Ellipse <Canvas Height="200" Width="250" HorizontalAlignment="Left"> <Ellipse Wi...