文章主要介绍了vue2中引用better-scroll和使用 better-scroll的方法,使用时有三个要点及注意事项在文中给你们详细介绍 ,须要的朋友能够参考下css
使用时有三个要点: 一:html部分html
<div class="example" ref="divScroll"> <div> <p>内容1</p> <p>内容2</p> <ul> <li>list1</li> <li>list2</li> <ul> </div> </div>
1.最外层加ref,让better-scroll经过ref来获取整个div; 2.紧跟一个div,不用加任何样式或class, 最终能够滑动的部分就是这个div,这个div必须是 加了ref 的div 的 直接子元素。 在这个div里面就能够放置但愿滑动的内容了。前端
二: css部分vue
.example width: 100% position: absolute top: 174px bottom: 48px left: 0 overflow: hidden
import BScroll from 'better-scroll';
1: 使用 mounted() 函数npm
mounted() { this.scroll = new BScroll(this.$refs.divScroll, { click: true, }); },
2.使用 created() 函数app
created() { this.$nextTick(() => { this.scroll = new BScroll(this.$refs.divScroll, { click: true, }); }); },
1.使用created 函数 要异步执行(此时html 还没有渲染完成)。 2. mounted函数 无需异步执行(mounted 函数在html渲染完成后触发)。异步
下面看下Vue中引入better-scroll的方法 1.用npm 安装好 better-scroll函数
npm install--save better-scroll
2.在须要的页面引入学习
import BScroll from 'better-scroll'
3.在data中定义 better-scroll的参数this
options: { pullDownRefresh: { threshold: 50, // 当下拉到超过顶部 50px 时,触发 pullingDown 事件 stop: 20 // 刷新数据的过程当中,回弹停留在距离顶部还有 20px 的位置 }, pullUpLoad: { threshold: -20 // 在上拉到超过底部 20px 时,触发 pullingUp 事件 }, // pullDownRefresh: false, //关闭下拉 // pullUpLoad: false, // 关闭上拉 click: true, probeType: 3, startY: 0,//欢迎加入全栈开发交流圈一块儿学习交流:864305860 scrollbar: true//面向1-3年前端人员 }//帮助突破技术瓶颈,提高思惟能力
4.在template中写入
<div class="wrapper" ref="wrapper" :scrollbar="options.scrollbar" :startY="options.startY">
5.在methods中写入方法,我自定义的
load() { if (!this.scroll) { this.scroll = new BScroll(this.$refs.wrapper, this.options); // 上拉 this.scroll.on('pullingUp', () => { // 刷新数据的过程当中,回弹停留在距离顶部还有20px的位置 this.setData(); }) } else {//欢迎加入全栈开发交流圈一块儿学习交流:864305860 this.scroll.refresh()//面向1-3年前端人员 }//帮助突破技术瓶颈,提高思惟能力 }, setData() { this.$nextTick(() => { let arr = [1, 2, 3, 'james']; this.data = this.data.concat(arr)// 添加数据 this.scroll.finishPullUp(); this.pullingDownUp() }) }, pullingDownUp() { this.scroll.refresh() //从新计算元素高度 },
6.在created中加载
this.$nextTick(() => { this.load() this.setData() })
结语
感谢您的观看,若有不足之处,欢迎批评指正。