vue爬坑之vux-scroller实现上拉加载下拉刷新

虽然vux官方已经说明scroller不再维护,但是基本上能满足我们的使用场景。

1.我装了mock.js来模拟加载数据,所以没有装mock的同学可以先装一下mock很方便的数据模拟工具:http://mockjs.com/

2.我装了stylus 语法相当自由,同僚们可以装一下,记得装stylus-loader;

3.这个组件是居于vux的,没有用vux框架的可以去了解一下

集成了一下代码,话不多说接下来我就直接贴代码了:

<template>
    <div>
        <scroller class="scroller" use-pullup :pullup-config="pullupDefaultConfig" @on-pullup-loading="loadMore"
                    use-pulldown :pulldown-config="pulldownDefaultConfig" @on-pulldown-loading="refresh"
                    lock-x ref="scrollerBottom" height="-48" v-model="scrollerStatus" >
            <div class="scroll-content">
                <div class="per-content"  v-for="(item, index) in loadList">
                    <div>测试图片:{{item.id}}</div>
                    <img :src="item.img" alt="">
                </div>
            </div>

        </scroller>
    </div>
</template>

<script type="text/ecmascript-6">
    import { Scroller } from 'vux'
    const pulldownDefaultConfig = {
        content: '下拉刷新',
        height: 40,
        autoRefresh: true,
        downContent: '下拉刷新',
        upContent: '释放后刷新',
        loadingContent: '正在刷新...',
        clsPrefix: 'xs-plugin-pulldown-'
    };
    const pullupDefaultConfig = {
        content: '上拉加载更多',
        pullUpHeight: 60,
        height: 40,
        autoRefresh: true,
        downContent: '释放后加载',
        upContent: '上拉加载更多',
        loadingContent: '加载中...',
        clsPrefix: 'xs-plugin-pullup-'
    };
    export default {
        components: {
            Scroller
        },
        mounted() {
//            this.$refs.scrollerBottom.reset()
            this.$nextTick(() => {
                this.$refs.scrollerBottom.reset()//{top: 0}
            })
            let firstLoad = this.$mock.mock({
                // 属性 list 的值是一个数组,其中含有 1 到 10 个元素
                'list|10': [{
                    // 属性 id 是一个自增数,起始值为 1,每次增 1
                    'id|+1': 1,
                    'img':this.$mock.Random.image('720x300')+'.png'
                }]
            });
            this.loadList=firstLoad.list
            console.log('第一次加载', this.loadList)
        },
        name: 'HelloWorld',
        data() {
            return {
                loadList:[] ,
                pullupDefaultConfig: pullupDefaultConfig,
                pulldownDefaultConfig: pulldownDefaultConfig
            }
        },

        methods: {
            refresh() {


            },
            loadMore() {
                //mock数据
                let loadMoreData= this.$mock.mock({
                    // 属性 list 的值是一个数组,其中含有 1 到 10 个元素
                    'list|5': [{
                        // 属性 id 是一个自增数,起始值为 1,每次增 1
                        'id|+1': 1,
                        'img':this.$mock.Random.image('720x300')+'.png'
                    }]
                });
                console.log('more',loadMoreData.list)
                console.log('concat前', this.loadList.length)
                this.loadList=this.loadList.concat(loadMoreData.list);
                console.log('concat后', this.loadList.length)
                console.log('加载更多后拼接', this.loadList)
                this.$refs.scrollerBottom.reset()
                this.scrollerStatus.pullupStatus = 'default'
            }
        },


    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    .scroller{
        /*overflow scroll!important*/
        /*-webkit-user-drag overflow !important*/
    }
    .scroll-content
        height 100%
        overflow scroll!important
    .per-content
        padding 10px
        img
            width: 100%

</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
vue-virtual-scroller是一个用于Vue.js的虚拟滚动组件,它可以实现高性能的长列表渲染。下面是使用vue-virtual-scroller实现下拉加载的步骤: 1. 首先,安装vue-virtual-scroller依赖: ```shell npm install vue-virtual-scroller -d ``` 2. 在你的Vue组件中引入vue-virtual-scroller: ```javascript import { RecycleScroller } from 'vue-virtual-scroller'; import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'; export default { components: { RecycleScroller }, // ... } ``` 3. 在模板中使用vue-virtual-scroller组件,并设置相应的属性和事件: ```html <template> <div class="wrapper"> <recycle-scroller :items="items" :item-size="50" :min-item-size="50" :max-item-size="50" :buffer="10" :page-mode="true" @load="loadMore" > <template slot-scope="props"> <!-- 渲染每个列表项的内容 --> <div class="item">{{ props.item }}</div> </template> </recycle-scroller> </div> </template> ``` 4. 在Vue组件的data中定义items数组,并在created钩子函数中初始化items: ```javascript export default { data() { return { items: [] }; }, created() { this.initItems(); }, methods: { initItems() { // 初始化items数组,可以从后端获取数据并赋值给items // 示例:假设从后端获取了10条数据 this.items = Array.from({ length: 10 }, (_, index) => `Item ${index + 1}`); }, loadMore() { // 加载更多数据的逻辑 // 示例:假设每次加载5条数据 const startIndex = this.items.length; const endIndex = startIndex + 5; const newItems = Array.from({ length: 5 }, (_, index) => `Item ${index + startIndex + 1}`); this.items = [...this.items, ...newItems]; } } } ``` 5. 根据你的需求,可以自定义样式来美化滚动区域。 以上是使用vue-virtual-scroller实现下拉加载的基本步骤。你可以根据自己的具体需求进行进一步的定制和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黒客与画家

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

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

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

打赏作者

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

抵扣说明:

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

余额充值