div上下来回自动滚动,解决setInterval出现抖动

因使用setInterval定时改变scrollTop以达到自动滚动效果,列表会出现明显抖动,所以在这里使用requestAnimationFrame来实现滚动效果

使用:

id:当前滚动元素的id

interval:滚动时间

backtrack:触底返回方式(true---缓慢返回;false----1秒到顶)

mouse:鼠标悬停

//引入
import { autoScroll } from '@/utils/autoScroll'

//使用
onMounted(async () => {
	//开始滚动
	setTimeout(() => {
		const scroll = new autoScroll({
			id: '#scrollContainer',
			interval: 2000,
			backtrack: true,
			mouse: true
		})
		scroll.start()
	}, 3000)
})

新建:autoScroll.js 

export class autoScroll {
    #animate;
    #id;
    #interval;
    #backtrack;
    #direction = 'toBottom';
    #mouse;
    #timer = null;
    constructor({ id = '', interval = 2000, backtrack = false, mouse = true }) {
        this.#id = id
        this.#interval = interval
        this.#backtrack = backtrack
        this.#mouse = mouse
    }
    start() {
        const animateFn = () => {
            const dom = document.querySelector(this.#id)
            if (dom.scrollHeight <= dom.clientHeight) {
                return
            }
            if (dom) {
                this.#direction === 'toTop' && (dom.scrollTop -= 1)
                this.#direction === 'toBottom' && (dom.scrollTop += 1)
                if (dom.scrollTop === 0) {
                    this.clear()
                    this.#direction = 'toBottom'
                    this.move(animateFn, this.#interval)
                } else if (dom.scrollHeight - dom.clientHeight <= dom.scrollTop) {
                    this.clear()
                    if (this.#backtrack) {
                        this.#direction = 'toTop'
                        this.move(animateFn, this.#interval)
                    } else {
                        dom.scrollTo({
                            top: 0,
                            behavior: 'smooth'
                        })
                        this.move(animateFn, this.#interval)
                    }

                } else {
                    this.move(animateFn)
                }
            }
        }
        setTimeout(() => {
            animateFn()
            this.#mouse && this.mouseListen(animateFn)
        }, 0);
    }
    mouseListen(animateFn) {
        const dom = document.querySelector(this.#id)
        dom.addEventListener('mouseover', () => this.clear())
        dom.addEventListener('mouseleave', animateFn)
    }
    move(animateFn, time) {
        time && !this.#timer && (this.#timer = setTimeout(() => {
            this.#animate = requestAnimationFrame(animateFn);
            this.#timer = null
        }, time))
        !time && !this.#timer && (this.#animate = requestAnimationFrame(animateFn))
    }
    clear() {
        cancelAnimationFrame(this.#animate)
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现列表内容自动向上滚动的效果,可以使用原生的JavaScript来操作DOM元素和定时器。 首先,可以通过getElementById或querySelector等方法获取到包含列表内容的元素。 然后,通过获取元素的高度、内容等信息来判断是否需要滚动。 接着,可以利用定时器setInterval来不断改变元素的scrollTop属性值,实现滚动效果。 具体步骤如下: 1. 首先,获取到包含列表内容的元素: ```javascript var listContainer = document.getElementById("listContainer"); //或者使用querySelector根据CSS选择器选择元素: var listContainer = document.querySelector("#listContainer"); ``` 2. 判断是否需要滚动,比如当元素内容的高度大于元素本身的高度时才需要滚动: ```javascript if(listContainer.scrollHeight > listContainer.clientHeight){ // 需要滚动,执行后续操作 } ``` 3. 设置定时器,不断改变元素的scrollTop属性值,实现滚动效果: ```javascript var isScrolling = true; // 表示是否正在滚动 setInterval(function(){ if(isScrolling){ listContainer.scrollTop += 1; // 每次滚动1个像素,可根据需要调整滚动速度 if(listContainer.scrollTop === (listContainer.scrollHeight - listContainer.clientHeight)){ listContainer.scrollTop = 0; // 滚动到底部后,回到顶部 } } }, 50); // 每50毫秒滚动一次,可根据需要调整滚动速度 ``` 4. 可以添加一些事件来控制滚动的开始和停止,比如鼠标进入和离开时暂停和恢复滚动效果: ```javascript listContainer.onmouseover = function(){ isScrolling = false; // 鼠标进入时停止滚动 } listContainer.onmouseout = function(){ isScrolling = true; // 鼠标离开时恢复滚动 } ``` 以上就是用原生JavaScript实现列表内容自动向上滚动效果的基本步骤。根据具体需求,还可以进行一些定制和调整,比如添加缓动效果、点击按钮控制滚动等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值