zy-slider icon indicating copy to clipboard operation
zy-slider copied to clipboard

如何设置步长

Open liyaxin58 opened this issue 5 years ago • 2 comments

有没有一个参数是设置步长的,类似于小程序里边的slider中step参数

liyaxin58 avatar Apr 16 '19 08:04 liyaxin58

没有

weixianlove avatar Apr 16 '19 09:04 weixianlove

有没有一个参数是设置步长的,类似于小程序里边的slider中step参数

// components/zyslider/zyslider.js 1.组件的初始数据data:{}中新增以下参数

data: {
    step: 1,
    stepEnable: false,//步进参数是否生效,默认不生效
    ......
}

2.组件的方法列表methods: {},新增函数

/**
* 设置步进参数,初始化调用
*/
_propertyStepChange: function () {

      let step = this.data.step;
      let min = this.data.min;
      let max = this.data.max;
      let minValue = this.data.minValue;
      let maxValue = this.data.maxValue;
      //如果步进小于0或等于1,或者最小值/最大值,或者差值不能整除步进,则不处理步进参数
      if (step <= 1 || min % step != 0 || max % step != 0 || (max - min) % step != 0)
        return;

      this.setData({

        stepEnable: true,
        min: min / step,
        max: max / step,
        minValue: minValue / step,
        maxValue: maxValue / step,
      })
},

3.在初始化ready: function () {}里新增函数_propertyStepChange()的调用

//在初始化滑块位置之前调用
that._propertyStepChange();
that._propertyLeftValueChange()
that._propertyRightValueChange()

4.左边滑块滑动回调 _minMove: function (e) {}中以下位置新增以下代码

let lowValue = parseInt(pagex / this.data.bigLength * parseInt(this.data.max - this.data.min) +             this.data.min)
//-------↓--加在这个位置--↓-----
if (this.data.stepEnable)
   lowValue *= this.data.step;
//-------↑--加在这个位置--↑-----
var myEventDetail = { lowValue: lowValue }

5.同理右边滑块滑动 _maxMove: function (e) {}中以下位置新增以下代码

let heighValue = parseInt(pagex / this.data.bigLength * (this.data.max - this.data.min) + this.data.min)
//-------↓--加在这个位置--↓-----
if (this.data.stepEnable)
    heighValue *= this.data.step;
//-------↑--加在这个位置--↑-----
var myEventDetail = { heighValue: heighValue }

6.使用组件时,添加step参数即可 <zy-slider step="5" />

PS:如果有什么不知名的bug- -! 自己改改就好了

xWolfSrt avatar May 11 '19 06:05 xWolfSrt