js实现图片无缝滚动

js实现图片无缝滚动

实现原理:

图片滚动原理同图片轮播原理,同样也适用于文字滚动等一系列滚动,通过复制最后一张图片或最后一堆文字插入第一行,或复制第一张图片或一堆文字插入在结尾,来实现无缝拼接。

前提: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)    }}

登录后复制

推荐教程:js入门教程

以上就是js实现图片无缝滚动的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2728612.html

(0)
上一篇 2025年3月7日 23:58:09
下一篇 2025年3月7日 23:58:16

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • jQuery 插件开发教程

    扩展jquery插件和方法的作用是非常强大的,它可以节省大量开发时间。这篇文章将概述jquery插件开发的基本知识,最佳做法和常见的陷阱。 一、入门 编写一个jQuery插件开始于给jQuery.fn加入新的功能属性,此处添加的对象属性的名…

    编程技术 2025年3月7日
    200
  • js对象的几种创建方式

    一、字面量方式 var obj = {    name: ‘mm’,    age: 18,    sayName: function() {            console.log(this.name);    }} 登录后复制 问…

    2025年3月7日 编程技术
    200
  • js中如何判断数据类型

    方法一、js内置方法typeof 检测基本数据类型的最佳选择是使用typeof typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object”,“func…

    2025年3月7日
    200
  • JavaScript 如何实现横向瀑布流

    最近在做一个小程序项目,在 UI 上借鉴了一下其他 App 设计,其中有一个图片横向布局铺满的 UI 感觉挺好看的,类似于传统的瀑布流布局横过来一样。于是就自己实现了一下,并且将原本的横向两张图的方法扩展了下,改成了可以自定义显示张数的方法…

    2025年3月7日
    200
  • js如何对乱序数组进行排序

    具体代码为: nbsp;html>            Document     var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; arr.sort(function (a, b) { retur…

    2025年3月7日
    200
  • 利用js实现表格隔行换色效果

    首先我们来看一下实现的效果,如下图所示: 思路: 确定事件: 文档加载完成 onload 2. 事件要触发函数: init() 3. 函数:操作页面的元素 要操作表格中每一行 动态的修改行的背景颜色 具体代码: nbsp;html> …

    2025年3月7日
    200
  • JS 中 9 个强大主流写法(各种 Hack 写法)

    1. 全局替换 我们知道,字符串函数 replace () 仅替换第一次出现的情况。 您可以通过在正则表达式的末尾添加 /g 来替换所有出现的内容。 var example = “potato potato”;console.log(exa…

    2025年3月7日
    200
  • js中!与!!的用法介绍

    js中!的用法是比较灵活的,它除了做逻辑运算常常会用!做类型判断,可以用!与上对象来求得一个布尔值, 1、!可将变量转换成boolean类型,null、undefined和空字符串取反都为false,其余都为true。 !null=true…

    2025年3月7日
    200
  • js实现图片预加载

    什么是预加载: 当页面打开图片提前加载,并且缓存在用户本地,需要用到时直接进行渲染;在浏览图片较多的网页(百度图库,淘宝京东等),可以有更好的用户体验; 一张图片的预加载 var img=new Image();    img.addEve…

    2025年3月7日
    200
  • js如何实现下拉控制列表

    重要属性介绍: ondblclick=”selectOne()”:双击事件 select标签的属性: multiple=”multiple”: 看一下实现效果: 具体实现代码: nbsp;ht…

    2025年3月7日
    200

发表回复

登录后才能评论