better-scroll使用总结
参考:https://zhuanlan.zhihu.com/p/27407024

better-scroll使用小结

核心就是这4个
<script>
import BScroll from 'better-scroll'
const ERR_OK=0;
export default{
props:['seller'],
data(){
return{
goods:[],
listHeight:[],
scrollY: 0
}
},
created(){
this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee'];
var _this=this;
this.$axios.get('/apis/goods').then(function(res){
var res=res.data;
if(res.errno===ERR_OK){
_this.goods=res.data;
console.log(_this.goods);
/*Vue实现响应式并不是数据发生变化之后DOM立即变化,而是按一定的策略进行DOM的更新*/
/*$nextTick是在下次DOM更新循环结束之后执行延迟回调,在修改数据之后使用 $nextTick,则可以在回调中获取更新后的 DOM*/
_this.$nextTick(() => {
_this._initScroll();
_this._calculateHeight();
});
}
})
},
computed:{
currentIndex(){
for(let i=0;i<this.listHeight.length;i++){
let height1=this.listHeight[i];
let height2=this.listHeight[i+1];
if(!height2 || (this.scrollY>=height1 && this.scrollY<height2)){
//
this._followScroll(i);
return i;
}
}
return 0;
}
},
methods:{
selectMenu(index,event){
/*为了兼容PC*/
if (!event._constructed) {
return;
}
/**/
let foodList=this.$refs.foodList;
let el=foodList[index];
this.foodsScroll.scrollToElement(el,300);
},
_initScroll(){
this.menuScroll = new BScroll(this.$refs.menuWrapper,{
click:true
}) this.foodsScroll = new BScroll(this.$refs.foodsWrapper,{
click:true,
probeType:3
});
/*是否派发滚动事件*/
this.foodsScroll.on('scroll',(pos)=>{
//Math.abs//取正数
this.scrollY=Math.abs(Math.round(pos.y));
})
/*probeType类型:Number
默认值:0
可选值:1、2、3
作用:有时候我们需要知道滚动的位置。当 probeType 为 1 的时候,会非实时(屏幕滑动超过一定时间后)派发scroll 事件;当 probeType 为 2 的时候,
会在屏幕滑动的过程中实时的派发 scroll 事件;当 probeType 为 3 的时候,不仅在屏幕滑动的过程中,
而且在 momentum 滚动动画运行过程中实时派发 scroll 事件。如果没有设置该值,其默认值为 0,即不派发 scroll 事件。*/
},
_calculateHeight(){
let foodList=this.$refs.foodList;
let height=0;
this.listHeight.push(height);
for(let i=0;i<foodList.length;i++){
let item = foodList[i];
height += item.clientHeight;
this.listHeight.push(height);
} },
_followScroll(index){
let menuList=this.$refs.menuList;
let el=menuList[index];
this.menuScroll.scrollToElement(el,300);
}
}
}
</script>
better-scroll使用总结的更多相关文章
- 【前端性能】高性能滚动 scroll 及页面渲染优化
最近在研究页面渲染及web动画的性能问题,以及拜读<CSS SECRET>(CSS揭秘)这本大作. 本文主要想谈谈页面优化之滚动优化. 主要内容包括了为何需要优化滚动事件,滚动与页面渲染的 ...
- MUI开发APP,scroll组件,运用到区域滚动
最近在开发APP的过程中,遇到一个问题,就是内容有一个固定的头部和底部. 头部就是我们常用的header了,底部的话,就放置一个button,用来提交页面数据或者进入下一个页面等,效果 ...
- 完美解决,浏览器下拉显示网址问题 | 完美解决,使用原生 scroll 写下拉刷新
在 web 开发过程中我们经常遇到,不想让用户下拉看到我的地址,也有时候在 div 中没有惯性滚动,就此也出了 iScroll 这种关于滚动条的框架,但是就为了一个体验去使用一个框架好像又不值得,今天 ...
- offset、client、scroll开头的属性归纳总结
HTML元素有几个offset.client.scroll开头的属性,总是让人摸不着头脑.在书中看到记下来,分享给需要的小伙伴.主要是以下几个属性: 第一组:offsetWidth,offsetHei ...
- mui scroll和上拉加载/下拉刷新
mui中 scroll和上拉加载/下拉刷新同时存在会出现两个滚动条 把/* */ /* //mui页面鼠标拖动代码: mui('.mui-scroll-wrapper').scroll({ dec ...
- JavaScript学习笔记5 之 计时器 & scroll、offset、client系列属性 & 图片无缝滚动
一.计时器 setInterval ( 函数/名称 , 毫秒数 )表示每经过一定的毫秒后,执行一次相应的函数(重复) setTimeout ( 函数/名称 , 毫秒数 ) 表示经过一定的毫秒后,只执行 ...
- JavaScript中的 offset, client,scroll
在js 中我们要用到的 offset, client, scroll 在这我把自己理解的给大家分享一下. offset div.offsetTop 指div距离上方或上层控件的距离,单位像素 div. ...
- 【前端性能】高性能滚动 scroll 及页面渲染优化--转发
本文主要想谈谈页面优化之滚动优化. 主要内容包括了为何需要优化滚动事件,滚动与页面渲染的关系,节流与防抖,pointer-events:none 优化滚动.因为本文涉及了很多很多基础,可以对照上面的知 ...
- 由overflow-x:scroll产生的收获
我们都知道float:left属性会让元素向左浮动,如果用一个div将几个左浮动的li包起来,是不是div的宽度被li撑得很长很长呢,代码: <!DOCTYPE html> <htm ...
- UGUI 之Scroll Rect 坑
由于UGUI没有提供类似Scroll View类似的控件,但提供了ScrollRect主机.可以自定义Scroll View 同时提供了Mask组件来遮罩超出Scroll Rect对象的范围, 之所以 ...
随机推荐
- sass实战演练01 - 外部文件引用和变量
SASS是什么? 目前前端开发中css已经是公认的”前端程序员必须掌握”的知识,最早的css编写都是手工一条条写出来的,工作量大.不利于维护. 而sass的存在使得css开发可以像写代码一样最终生成一 ...
- 如何使用JDBC查询指定的记录
//连接数据库 public class JdbcDao { private Connection conn=null; private String strSql=null; publi ...
- 注解(Annotation)是什么?
- angularjs 与 UEditor开发,添加directive,保证加载顺序正常
'use strict'; angular.module('app.core').directive('ueditor', [function () { return { restrict: 'A', ...
- 30.深入理解abstract class和interface
- week5 0.2 client
我们修改了下logo 自己找的图片 放在public文件下 页面如下我们准备做成这样 每一个component对应一个css样式 不需要统一的css 这样容易找到自己的css并修改 下面我们修改我们的 ...
- Arcgis map export or print Error: Cannot map metafile into memory. Not enough memory
Arcgis map export or print Error: Cannot map metafile into memory. Not enough memory Link: https:/ ...
- iOS Dev (25) 解决“The executable was signed with invalid entitlements.”问题
2014-01-10 10:34 5240人阅读 评论(1) 收藏 举报 目录(?)[+] iOS Dev (25) 解决“The executable was signed with inv ...
- SAP 数据类型
数据元素和基本类型对应关系 数据字典预置类型 ABAP类型 运行长度 说明 ACCP N(6) 6 会计计算周期 CHAR C(n) 1-255 字符 CLNT C(3) 3 集团,数据区域代码 CU ...
- c 中的单引号和双引号的使用
1. 在c中,'A' 表示的是一个 character constant ,表示的是字符集的数值:而 "A" 表示的是一个字符串常量,代表的是指向字符串的指针.