JavaScript-自制列表自动滚动[Vue][v-for]

JavaScript-自制列表自动滚动[Vue][v-for]

1. 实现思路

  • 假设有一个数据源列表:rows数组;
  • 再定义一个用于最终被v-for循环生成列表的数据源列表:toShowRows数组;
  • 现在只需让toShowRows首元素弹出,然后再从尾部加入一个新元素,此时由于Vue的响应式,就会更新列表;
  • 最后,设置自我递归调用的setTimeout定时器,对上述环节每隔一定时间执行一次,即可达到列表向上自动滚动的效果!

2.完整HTML文件代码

<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta http-equiv="Content-Type" charset=UTF-8">
        <style>
            .row{
                display: flex;
                flex-direction: row;
            }
            .order-div{
                width: 30%;
                height: 100%;
                background-color: aquamarine;
            }
            .item-div{
                width: 30%;
                height: 100%;
                background-color: blanchedalmond;
            }
            .message-div{
                width: 40%;
                height: 100%;
                background-color: tomato;
                color:white;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <div class="row" v-for="(item,index) in toShowRows" :style="`width:600px;margin:3px;border:${item} 3px solid;`">
                <div class="order-div">{{rows.indexOf(item)}}</div>
                <div class="item-div">{{item}}</div>
                <div class="message-div">{{`border:${item} 3px solid;`}}</div>
            </div>
        </div>
        <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
        <script>
            const {createApp} = Vue;
            createApp({
                data() {
                    return {
                        message: 'Hello Vue!',
                        rows:['skyblue','orange','black','red','green','blue','yellow','#bfa','purple','#bce'],
                        toShowRows:[],
                        record:0
                    }
                },
                mounted(){
                    console.log('ok')
                    const timego = ()=>{
                        setTimeout(()=>{
                                if(this.toShowRows.length < this.rows.length){
                                    this.toShowRows.push(this.rows[this.record++]);
                                }else{
                                    console.log(this.record,'<==========');
                                    this.toShowRows.shift();
                                    if(this.record == this.rows.length){
                                        this.record = 0;
                                        console.log(this.record,'!==========');
                                    }
                                    this.toShowRows.push(this.rows[this.record++]);
                                }
                                timego()
                            }
                            ,700
                        )
                    }
                    timego();
                }
            }).mount('#app')
        </script>
    </body>
</html>

3.自动滚动运行效果(gif)

在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
参数说明 listLoadingMore({id:"",id2:function(){},action:function(){},pageNum:10,getNew:undefined,funcArg:undefined,func:function(){},loadingMustTime:0,loadingDom:function(){},loadedDom:function(){},nullDataFunc:function(){},endFunc:function(){},errorFunc:function(){}}); id:滚动条id,不可以是body; id2:滚动列表的id,通过function自行根据情况返回; action:数据来源的ajax地址,插件会自动加上page和length(分别表示请求页数和当前已有记录数),返回格式统一是[{},{}...],此类jsonArray; ajaxType:ajax提交方式,将更改传递数据的方式,默认post; pageNum:每页加载数量,将会根据这个数量判断是否全部加载完成; getNew:自定义获取数据方法; funcArg:自定义获取数据时传递的参数,类型为函数,返回所需参数; childrenTag:列表子项的标签,默认LI; func:非自定义获取数据时,创建每行数据的方法; loadingMustTime:强制最小加载时间,默认0; loadingDom:加载中动画自定义,返回Dom或者HTML代码; loadedDom:完全加载完成动画自定义,返回Dom或者HTML代码; nullDataFunc:数据列表为空时执行的方法; endFunc:每页数据加载完成时执行的方法; errorFunc:ajax获取数据失败时调用的方法。 回调方法 调用本方法初始化后将会得到一个专属的方法集: Object {getNew:function,isFull:function,isRunning:function,isStop:function,loading:function,show:function}; getNew():忽略判断条件,强制执行加载操作。此时数据传递中,page值可能出错,但length值一定正确,请谨慎判断后在执行该方法; isFull(boolean):修改列表加载完成标志。true为全部加载完成,false则相反; isRunning(boolean):修改列表当前是否正在加载中标志。几乎没有使用的机会; isStop(boolean):设置是否停止加载,设置为true时将会阻止未来的所有加载操作; loading():根据当前滚动条的位置,自动判断是否需要加载新数据; show(boolean,boolean):强制设置加载中和加载完成dom的显示或隐藏,true表示显示,false相对。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值