参考:https://zhuanlan.zhihu.com/p/27407024

 

better-scroll使用小结

核心就是这4个

 1 <script>
 2 import BScroll from 'better-scroll'
 3 const ERR_OK=0;
 4 export default{
 5     props:['seller'],
 6     data(){
 7         return{
 8             goods:[],
 9             listHeight:[],
10             scrollY: 0
11         }
12     },
13     created(){
14         this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
15         var _this=this;
16         this.$axios.get('/apis/goods').then(function(res){
17             var res=res.data;
18             if(res.errno===ERR_OK){
19                 _this.goods=res.data;
20                 console.log(_this.goods);
21                 /*Vue实现响应式并不是数据发生变化之后DOM立即变化,而是按一定的策略进行DOM的更新*/
22                 /*$nextTick是在下次DOM更新循环结束之后执行延迟回调,在修改数据之后使用 $nextTick,则可以在回调中获取更新后的 DOM*/
23                 _this.$nextTick(() => {
24           _this._initScroll();
25           _this._calculateHeight();
26         });
27             }
28         })
29     },
30     computed:{
31         currentIndex(){
32             for(let i=0;i<this.listHeight.length;i++){
33                 let height1=this.listHeight[i];
34                 let height2=this.listHeight[i+1];
35                 if(!height2 || (this.scrollY>=height1 && this.scrollY<height2)){
36                     //
37                     this._followScroll(i);
38                     return i;
39                 }
40             }
41             return 0;
42         }
43     },
44     methods:{
45         selectMenu(index,event){
46             /*为了兼容PC*/
47             if (!event._constructed) {
48         return;
49       }
50       /**/
51             let foodList=this.$refs.foodList;
52             let el=foodList[index];
53             this.foodsScroll.scrollToElement(el,300);
54         },
55         _initScroll(){
56             this.menuScroll = new BScroll(this.$refs.menuWrapper,{
57                 click:true
58             })
59 
60             this.foodsScroll = new BScroll(this.$refs.foodsWrapper,{
61                 click:true,
62                 probeType:3
63             });
64             /*是否派发滚动事件*/
65             this.foodsScroll.on('scroll',(pos)=>{
66                 //Math.abs//取正数
67                 this.scrollY=Math.abs(Math.round(pos.y));
68             })
69             /*probeType类型:Number
70         默认值:0
71         可选值:1、2、3
72         作用:有时候我们需要知道滚动的位置。当 probeType 为 1 的时候,会非实时(屏幕滑动超过一定时间后)派发scroll 事件;当 probeType 为 2 的时候,
      会在屏幕滑动的过程中实时的派发 scroll 事件;当 probeType 为 3 的时候,不仅在屏幕滑动的过程中,
      而且在 momentum 滚动动画运行过程中实时派发 scroll 事件。如果没有设置该值,其默认值为 0,即不派发 scroll 事件。
*/ 73 }, 74 _calculateHeight(){ 75 let foodList=this.$refs.foodList; 76 let height=0; 77 this.listHeight.push(height); 78 for(let i=0;i<foodList.length;i++){ 79 let item = foodList[i]; 80 height += item.clientHeight; 81 this.listHeight.push(height); 82 } 83 84 }, 85 _followScroll(index){ 86 let menuList=this.$refs.menuList; 87 let el=menuList[index]; 88 this.menuScroll.scrollToElement(el,300); 89 } 90 } 91 } 92 </script>

 

posted on 2018-01-12 11:10  执候  阅读(323)  评论(0编辑  收藏  举报