【解决方案】JavaScript 实现DOM、element-ui-table自动滚动

自动轮播

工具类源码

export default {
    /**
     *创建轮播实例
     * @param {Element} target 需要轮播的
     * @param {string} position 滚动方向:x,y
     * @param {number} step 移动步长:1
     * @param {number} interval 定时器间隔时间:50
     * @param {number} timeout 倒计时定时间隔时间:1000
     * @param {number} anotation 动画时长,最小50
     * @param {number} gatter 初始动画前和窗口大小改变时的间隔时间:1000
     * @returns
     */
    create({ target, position, step, interval, timeout, anotation, name, gatter }) {
        return {
            name,
            target,
            position,
            step,
            interval,
            timeout,
            anotation,
            gatter,
            intervalTimer: null,
            timeoutTimer: null,
            start() {
                if (!this.target) {
                    return;
                }

                this.target.style.overflow = "hidden";
                this.step = this.step || 1;
                this.position = this.position || "y";
                this.interval = this.interval || 50;
                this.timeout = this.timeout || 1000;
                this.anotation = this.anotation || 50;
                this.gatter = this.gatter || 1000;

                setTimeout(() => {
                    this.intervalScroll();
                }, this.gatter);
                this.watchWheelEvent();

                window.addEventListener("resize", () => {
                    this.stop();
                    setTimeout(() => {
                        this.intervalScroll();
                    }, this.gatter);
                });
            },
            /**
             * 监听滚轮事件
             */
            watchWheelEvent() {
                this.target.addEventListener(
                    "wheel",
                    (event) => {
                        this.stop();
                        if (this.position === "x") {
                            this.target.scrollLeft = this.target.scrollLeft + event.deltaY;
                        } else {
                            this.target.scrollTop = this.target.scrollTop + event.deltaY;
                        }
                        this.timeoutTimer = setTimeout(() => {
                            this.stop();
                            this.intervalScroll();
                        }, this.timeout);
                    },
                    { passive: false }
                );
            },
            /**
             * 滚动特效
             */
            scrollAnotation() {
                const timer = setInterval(() => {
                    if (this.position === "x") {
                        this.target.scrollLeft += this.step / (this.anotation / 50);
                    } else {
                        this.target.scrollTop += this.step / (this.anotation / 50);
                    }
                }, 50);
                const timer2 = setTimeout(() => {
                    clearInterval(timer);
                    clearTimeout(timer2);
                }, this.anotation);
            },
            /**
             * 重置滚动到起始位置
             */
            resetScrolltoZero() {
                if (!this.timeoutTimer) {
                    this.timeoutTimer = setTimeout(() => {
                        if (this.position === "x") {
                            this.target.scrollLeft = 0;
                        } else {
														this.target.scrollTop = 0;
                        }

                        this.timeoutTimer = clearTimeout(this.timeoutTimer);
                    }, this.timeout);
                }
            },
            /**
             * 设置定时滚动
             * @returns
             */
            intervalScroll() {
                if (this.intervalTimer) {
                    return;
                }
                this.intervalTimer = setInterval(() => {
                    if (
                        this.position === "x" &&
                        this.target.scrollWidth <= this.target.clientWidth
                    ) {
                        return clearInterval(this.intervalTimer);
                    }
                    if (
                        this.position === "y" &&
                        this.target.scrollHeight <= this.target.clientHeight
                    ) {
                        return clearInterval(this.intervalTimer);
                    }

                    if (
                        this.position === "x" &&
                        this.target.clientWidth + this.target.scrollLeft == this.target.scrollWidth
                    ) {
                        this.resetScrolltoZero();
                    } else if (
                        this.position === "y" &&
                        this.target.clientHeight + this.target.scrollTop == this.target.scrollHeight
                    ) {
                        this.resetScrolltoZero();
                    } else {
                        this.scrollAnotation();
                    }
                }, this.interval);
            },
            /**
             * 停止滚动
             */
            stop() {
                this.intervalTimer = clearInterval(this.intervalTimer);
                this.timeoutTimer = clearTimeout(this.timeoutTimer);
            }
        };
    }
};

示例

1. 实例:element-ui table 滚动

import AutoScroll from 'autoscroll';

const table = this.$refs.table,
			wrapper = table.bodyWrapper;

this.autoScrollInstance = AutoScroll.create({ target: wrapper });
this.autoScrollInstance.start();
```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Space Chars

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值