better-scroll

来源:互联网 发布:JAVA中 异常类型 编辑:程序博客网 时间:2024/05/16 13:47

better-scroll:

npm 命令安装 npm install better-scroll –save

官网:https://ustbhuangyi.github.io/better-scroll/doc/installation.html#npm

github地址: https://github.com/ustbhuangyi/better-scroll

运行命令行之前先在package.json中配置版本

"dependencies": {    "babel-runtime": "^6.26.0",    "better-scroll": "^1.4.2",    "vue": "^2.5.2",    "vue-resource": "^1.3.4",    "vue-router": "^3.0.1"  }

这里写图片描述

在哪里用在哪里引入

import Bscroll from 'better-scroll'
 let ERROK = 0  export default{    name: 'goods',    data () {      return {        goods: {},        // addSelect: 0, 用不到了        listHeight: [], // 每个标题的高度        scrollY: 0 // 左边点击 右边要滚动的距离,右边滚动的距离选中左边的块      }    },    computed: {      currentIndex () {        let scrollY = Math.abs(Math.round(this.scrollY))       // console.log(scrollY)        for (var i = 0, len = this.listHeight.length; i < len; i++) {          let height1 = this.listHeight[i]          let height2 = this.listHeight[i + 1]          if (!height2 || (scrollY >= height1 && scrollY < height2)) {            return i          }        }        return 0      }    },    methods: {      changeTab (index, event) { // 点击事件操作滚动 间接触发currentIndex()函数 使index动态改变 左边颜色改变        // this.addSelect = index        let foodList = this.$refs.foods.getElementsByClassName('food-cont')        let el = foodList[index]        // 设置滚动的距离(点击滚动到指定的位置)        this.foodWrapperScroll.scrollToElement(el, 300)      },      // 在这里初始化 better-scroll  注意Vue中数据更新是异步的,在数据还没有加载完之前,BScroll是无法获取目标内容容器的高度的,就会出现无法滚动的现象。这里可以用$nextTick()解决。      _initScroll () {        // console.log(this.$refs.foodWrapper)        // console.log(this.$refs.menuWrapper)        this.menuWrapperScroll = new Bscroll(this.$refs.menuWrapper, {          click: true // 点击事件许可  让子元素可以点击        })        this.foodWrapperScroll = new Bscroll(this.$refs.foodWrapper, {          click: true,          probeType: 3 // 实时监控滚动        })        // 监听实时的 scrollY        this.foodWrapperScroll.on('scroll', (pos) => {          // 四舍五入后求绝对值(滚动的距离)          this.scrollY = Math.abs(Math.round(pos.y))        })      },      __calculateHeight () { // 获取食物的li Dom节点列表        let height = 0        let heightList = []        heightList.push(height)        let foodList = this.$refs.foods.getElementsByClassName('food-cont')       /*  console.log(foodList) */        for (let i = 0, len = foodList.length; i < len; i++) {          height += foodList[i].clientHeight          // console.log(foodList[i].clientHeight)          // console.log(window.getComputedStyle(foodList[i]).height)          heightList.push(height)          this.listHeight = heightList        }      }    },    created () {      this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']      this.$http.get('./api/goods').then((result) => {        result = result.body        if (result.errno === ERROK) {          this.goods = result.data          /* console.log(this.goods) */          // 数据加载完毕让 better-scroll 起作用  因为异步要放在$nextTick()中          this.$nextTick(() => {            this._initScroll()            this.__calculateHeight()          })        }      })    }/* ,    mounted () {      console.log(this.$refs.foods.getElementsByClassName('food-cont'))    },    updated () {      console.log(this.$refs.foods.getElementsByClassName('food-cont'))    } */  }

2、实现的效果:点击右侧选中的高亮 左侧滚动到对应的地方 滑动左侧商品 右边对应的分类变高亮
主要是判断 左侧内用的Y值在哪个区间 根据左侧的区间显示右侧的区间
首先要知道每一个区间的高度

better-scroll的属性与方法

格式:var obj = new BScroll(object,{[option1,],.,.});注意:1、要确保object元素的高度比其父元素高2、使用时,一定要确保object所在的dom渲染后再用上面的语句,或者fresh()

Options 参数

  • 列表内容startX: 0 开始的X轴位置
  • startY: 0 开始的Y轴位置
  • scrollY: true 滚动方向为 Y 轴
  • scrollX: true 滚动方向为 X 轴
  • click: true 是否派发click事件,通常判断浏览器派发的click还是betterscroll派 发的click,可以用event._constructed,若是bs派发的则为true
  • directionLockThreshold: 5
  • momentum: true 当快速滑动时是否开启滑动惯性
  • bounce: true 是否启用回弹动画效果
  • selectedIndex: 0 wheel 为 true 时有效,表示被选中的 wheel 索引
  • rotate: 25 wheel 为 true 时有效,表示被选中的 wheel 每一层的旋转角度
  • wheel: false 该属性是给 picker 组件使用的,普通的列表滚动不需要配置
  • snap: false 该属性是给 slider 组件使用的,普通的列表滚动不需要配置
  • snapLoop: false 是否可以无缝循环轮播
  • snapThreshold: 0.1 用手指滑动时页面可切换的阈值,大于这个阈值可以滑动的下一页
  • snapSpeed: 400, 轮播图切换的动画时间
  • swipeTime: 2500 swipe 持续时间
  • bounceTime: 700 弹力动画持续的毫秒数
  • adjustTime: 400 wheel 为 true 有用,调整停留位置的时间
  • swipeBounceTime: 1200 swipe 回弹 时间
  • deceleration: 0.001 滚动动量减速越大越快,建议不大于0.01
  • momentumLimitTime: 300 符合惯性拖动的最大时间
  • momentumLimitDistance: 15 符合惯性拖动的最小拖动距离
  • resizePolling: 60 重新调整窗口大小时,重新计算better-scroll的时间间隔
  • preventDefault: true 是否阻止默认事件
  • preventDefaultException: { tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ } 阻止默认事件
  • HWCompositing: true 是否启用硬件加速
  • useTransition: true 是否使用CSS3的Transition属性
  • useTransform: true 是否使用CSS3的Transform属性
  • probeType: 1 滚动的时候会派发scroll事件,会截流。2滚动的时候实时派发 scroll事件,不会截流。 3除了实时派发scroll事件,在swipe的情况下仍然能实时派发scroll事件

Events 事件

  • beforeScrollStart - 滚动开始之前触发
  • scrollStart - 滚动开始时触发
  • scroll - 滚动时触发
  • scrollCancel - 取消滚动时触发
  • scrollEnd - 滚动结束时触发
  • touchend - 手指移开屏幕时触发
  • flick - 触发了 fastclick 时的回调函数
  • refresh - 当 better-scroll 刷新时触发
  • destroy - 销毁 better-scroll 实例时触发
  • Example:

    let scroll = new BScroll(document.getElementById('wrapper'),{   probeType: 3})scroll.on('scroll', (pos) => {  console.log(pos.x + '~' + pos.y)})

函数列表

  • scrollTo(x, y, time, easing)
    滚动到某个位置,x,y 代表坐标,time 表示动画时间,easing 表示缓动函数
    scroll.scrollTo(0, 500)

  • scrollToElement(el, time, offsetX, offsetY, easing)
    滚动到某个元素,el(必填)表示 dom 元素,time 表示动画时间,offsetX 和 offsetY 表示坐标偏移量,easing 表示缓动函数

  • refresh()
    强制 scroll 重新计算,当 better-scroll 中的元素发生变化的时候调用此方法

  • getCurrentPage()
    snap 为 true 时,获取滚动的当前页,返回的对象结构为 {x, y, pageX, pageY},其中 x,y 代表滚动横向和纵向的位置;pageX,pageY 表示横向和纵向的页面索引。用法如:getCurrentPage().pageX

  • goToPage(x, y, time, easing)
    snap 为 true,滚动到对应的页面,x 表示横向页面索引,y 表示纵向页面索引, time 表示动画,easing 表示缓动函数(可省略不写)

  • enable()启用 better-scroll,默认开启

  • disable() 禁用 better-scroll

  • destroy() 销毁 better-scroll,解绑事件

原创粉丝点击