猿记录

一个记录、分享的博客

您的位置:主页 > 技术专栏 > javascript > vue >

vue+better-scroll 下拉刷新,上拉加载更多

2018-08-09 14:42:30 作者:yxl 次阅读 vue

<template>
    <div class="rules">
        <p class="drop-down" v-if="dropDown">松手刷新数据...</p>
        <div class="bscroll" ref="bscroll">
            <div class="bscroll-container">
                <ul>
                   <li>1</li><li>1</li><li>1</li>
                   <li>1</li><li>1</li><li>1</li>
                   <li>1</li><li>1</li><li>1</li>
                   <li>1</li><li>1</li><li>1</li>
                   <li>1</li><li>1</li><li>1</li>
                   <li>1</li><li>1</li><li>1</li>
                   <li>1</li><li>1</li><li>1</li>
                </ul>
            </div>
        </div>
        <!-- <p class="x">加载更多...</p> -->
    </div>
</template>
 
<script>
import BScroll from 'better-scroll'
export default {
    data(){
        return{
            dropDown:false
        }
    },
    mounted(){
        this.scrollFn()
    },
    methods:{
        scrollFn(){
            this.$nextTick(() => {
                if (!this.scroll) {
                    this.scroll = new BScroll(this.$refs.bscroll, {
                        click: true,
                        scrollY: true,
                        probeType: 3
                    });
                }else{
                    this.scroll.refresh();
                }
                this.scroll.on('scroll', (pos) => {
                    console.log(pos.y,this.dropDown)
                    //如果下拉超过50px 就显示下拉刷新的文字
                    if(pos.y>50){
                        this.dropDown = true
                    }else{
                        this.dropDown = false
                    }
                })
                //touchEnd(手指离开以后触发) 通过这个方法来监听下拉刷新
                this.scroll.on('touchEnd', (pos) => {
                    // 下拉动作
                    if(pos.y > 50){
                        console.log("下拉刷新成功")
                        this.dropDown = false
                    }
                    //上拉加载 总高度>下拉的高度+10 触发加载更多
                    if(this.scroll.maxScrollY>pos.y+10){
                        console.log("加载更多")
                        //使用refresh 方法 来更新scroll  解决无法滚动的问题
                        this.scroll.refresh()
                    }
                    console.log(this.scroll.maxScrollY+"总距离----下拉的距离"+pos.y)
                })
                console.log(this.scroll.maxScrollY)
            });
        }
    }
}
</script>
 
 
<style scoped>
.bscroll{
    width: 100%;
    height:500px;
    overflow: hidden;
}
.bscroll-container{
    background: #ff0000;
}
.drop-down{
    position: absolute;
    top:0px;
    lefT:0px;
    width: 100%;
    height: 50px;
    line-height:50px;
    text-align: center;
    font-size:0.8rem;
    color:#CCC;
}
</style>

1、如果bscroll 你不想设定高度,那你就把bscroll 定位absolute height:100%;前提示html,body{height:100%};这样才有效果,如果你bscoll 不设定高度,触发不了

2、还有异步加载的数据需要使用

    this.$nextTick(() => {})


凡本站注明“本站”或“投稿”的所有文章,版权均属于本站或投稿人,未经本站授权不得转载、摘编或利用其它方式使用上述作品。

编辑:yxl 关键词: better-scrol
0

网友评论