Vue2自定义列表无缝滚动

使用vue-seamless-scroll插件时发现当一组列表滚动完之后会留有一些空白在页面上,如下图

 于是自己封装了一个组件,内包括鼠标移入、鼠标移出、首尾相应等事件,具体如下,可直接赋值运行

<template>
    <div class="scrollList">
        <h2>无缝滚动</h2>
        <div @mousemove="testMove" @mouseleave="testMend">
            <div ref="roll" style="height: 110px;overflow: hidden;margin:20px;">
                <slot>
                        <li>模拟标题1</li>
                        <li>模拟标题2</li>
                        <li>模拟标题3</li>
                        <li>模拟标题4</li>
                        <li>模拟标题5</li>
                </slot>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            timer: null,        //定时器
            roll: null,         //获取容器的值
        }
    },
    methods: {
        //鼠标移入事件 
        testMove() {
            clearTimeout(this.timer)
        },
        //鼠标移出事件  
        testMend() {
            this.start()
        },
        //开启定时器方法
        start() {
            //清除定时器
            clearTimeout(this.timer)
            //定时器触发周期,移动速度
            let speed = 50

            this.timer = setInterval(() => {
                //获取绑定的容器
                this.roll = this.$refs.roll
                let test1 = this.roll
                //判断组件是否渲染完成
                if (test1.offsetHeight == 0) {
                    test1 = this.roll
                } else {
                    //如果列表数量过少不进行滚动
                    if (test1.childNodes.length < 6) {
                        clearTimeout(this.timer)
                        return;
                    }
                    //组件进行滚动
                    test1.scrollTop += 1
                    //判断滚动条是否滚动到底部
                    if (test1.scrollTop == (test1.scrollHeight - test1.clientHeight)) {
                        //获取组件第一个节点
                        let a = test1.childNodes[0]
                        //删除节点
                        test1.removeChild(a)
                        //将该节点拼接到组件最后
                        test1.append(a)
                    }
                }
            }, speed)

        },

    },
    mounted() {
        this.$nextTick(() => {
            // 在挂载完成后执行回调函数
            this.start();
        });
    },
    //组件销毁前需清除定时器
    beforeDestroy() {
        //清除定时器
        clearTimeout(this.timer)
    },
    //组件销毁后需清除定时器
    destroyed() {
        //清除定时器
        clearTimeout(this.timer)
    },
}
</script>

<style lang="scss" scoped>
.scrollList {
    width: 100%;
    height: 100%;

    h2 {
        text-align: center;
    }
}
</style>

原理:

        主要就是通过通过计时器不断的给获取到的容器增加它的自身高度,在与可视高度为0时,将容器内的第一个子元素添加到容器底部,从而达到首尾呼应,Vue3版本的见下一篇文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现在 Vue 2 中当页面滚动到底部时显示整个组件的效果,你可以结合使用 Vue 的生命周期钩子和原生 JavaScript滚动事件。 首先,在你的组件中,添加一个容器元素,例如一个 `<div>`,用于包裹需要显示的整个组件。 然后,在组件的生命周期钩子函数 `mounted` 中,绑定一个滚动事件监听器,用于检测页面滚动。 ```html <template> <div> <!-- 其他内容 --> <div ref="bottomComponent" class="bottom-component"> <!-- 整个组件的内容 --> </div> </div> </template> <script> export default { mounted() { window.addEventListener('scroll', this.handleScroll); }, destroyed() { window.removeEventListener('scroll', this.handleScroll); }, methods: { handleScroll() { const bottomComponent = this.$refs.bottomComponent; const scrollPosition = window.scrollY + window.innerHeight; const componentPosition = bottomComponent.offsetTop + bottomComponent.offsetHeight; if (scrollPosition >= componentPosition) { // 当页面滚动到底部时,显示整个组件 // 可以在这里修改组件的显示状态或其他操作 } }, }, }; </script> <style> .bottom-component { display: none; /* 初始状态下隐藏组件 */ } </style> ``` 在 `handleScroll` 方法中,通过比较滚动位置和组件位置来判断是否滚动到底部。当滚动到底部时,你可以在对应的条件下修改组件的显示状态,例如通过操作组件的 CSS 类名或修改数据属性来控制组件的显示和隐藏。 希望这个解决方案对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值