无穷滚动加载(v-infinite-scroll)

无穷滚动加载(v-infinite-scroll)

适用场景

  1. 如淘宝商品页面底部加载更多商品信息

知识点

  1. v-infinite-scroll指向一个加载函数,当滚动条到当前标签底部时(本例的标签为ul),自动触发加载函数,另外未铺满标签,加载函数会一直执行
  2. infinite-scroll-disabled指向truefalse,当为true时,加载函数不在执行,当为false时加载函数继续执行
  3. 此例中ul标签一定要设置style="overflow:auto",加载函数才能生效
  4. v-infinite-scroll对应的标签一定要设置一个固定高度,若不设置就是自定义撑开,则任何时刻都处于未铺满的状态,加载函数会一直执行下去

效果图

  1. 未铺满时,加载函数一直执行
    在这里插入图片描述
  2. 铺满且滚动条不在底部时停止加载
    在这里插入图片描述
  3. 滚动条到达底部是执行加载
    在这里插入图片描述
  4. :infinite-scroll-disabled = 'true'时,滚动条到达底部不在执行
    在这里插入图片描述

代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div class="container">
            <div class="header"></div>
            <div>
                <ul class="content" v-infinite-scroll="load" :infinite-scroll-disabled="stop">
                    <li v-for="i in count">{{i}}</li>
                </ul>
                <div>
                    <span v-if="loading">加载中</span>
                    <span v-if="count>=20">无法加载更多</span>
                </div>
            </div>
            <div class="bottom"></div>
        </div>

    </div>
</body>

</html>

<style scoped>
    .container {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    html body {
        width: 100%;
        height: 100%;
    }

    .header {
        width: 100%;
        height: 100px;
        background-color: royalblue;
    }

    .content {
        width: 100%;
        height: 200px;
        overflow: auto;
        flex: 1;
    }

    .bottom {
        width: 100%;
        height: 100px;
        background-color: sandybrown;
    }
</style>

<script>
    new Vue({
        el: "#app",
        data() {
            return {
                count: 0,
                loading: false
            }
        },
        computed: {
            stop() {
                return this.loading || this.count >= 20
            }
        },
        methods: {
            load() {
                this.loading = true;
                setTimeout(() => {
                    this.loading = false;
                    this.count++;
                }, 1000)
            }
        }
    })
</script>

官网

无限滚动条

补充案例(el-table结合infiniteScroll)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <title>表格结合无限滚动</title>
</head>

<body>
    <div id="app">
        <div v-infinite-scroll="loadClothes" style="overflow:auto;height: 300px;" :infinite-scroll-disabled="stop">
            <el-table :data="tableData">
                <el-table-column type="index" width="50" label="序号"></el-table-column>
                <el-table-column prop="name" label="商品名称"></el-table-column>
            </el-table>
        </div>
        <el-tag v-if="loading">加载中</el-tag>
        <el-tag v-if="tableData.length >= 50">无更多商品</el-tag>
    </div>


</body>

</html>

<script>
    new Vue({
        el: "#app",
        data() {
            return {
                tableData: [
                    { name: '白色纯棉T恤' },
                    { name: '白色亚麻T恤' },
                    { name: '白色聚脂钎维T恤' },
                    { name: '黑色纯棉T恤' },
                    { name: '黑色亚麻T恤' },
                    { name: '黑色聚脂钎维T恤' },
                    { name: '蓝色纯棉T恤' },
                    { name: '蓝色亚麻T恤' },
                    { name: '蓝色聚脂钎维T恤' },
                    { name: '白色聚脂钎维T恤' },
                ],
                loading: false
            }
        },
        computed: {
            stop() {
                return this.tableData.length >= 50 || this.loading;
            }
        },
        methods: {
            loadClothes() {
                this.loading = true;
                setTimeout(() => {
                    this.tableData.push(this.tableData[1])
                    this.tableData.push(this.tableData[2])
                    this.tableData.push(this.tableData[3])
                    this.tableData.push(this.tableData[4])
                    this.tableData.push(this.tableData[5])
                    this.loading = false;

                }, 2000)
            }
        }
    })
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

自律最差的编程狗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值