index.vue 9.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
2 3 4
  <uni-slider
    ref="uni-slider"
    v-on="$listeners"
5
    @click="_onClick"
fxy060608's avatar
fxy060608 已提交
6
  >
fxy060608's avatar
fxy060608 已提交
7 8
    <div class="uni-slider-wrapper">
      <div class="uni-slider-tap-area">
9 10
        <div
          :style="setBgColor"
11
          class="uni-slider-handle-wrapper"
fxy060608's avatar
fxy060608 已提交
12
        >
13 14 15
          <div
            ref="uni-slider-handle"
            :style="setBlockBg"
16
            class="uni-slider-handle"
fxy060608's avatar
fxy060608 已提交
17
          />
18 19
          <div
            :style="setBlockStyle"
20
            class="uni-slider-thumb"
fxy060608's avatar
fxy060608 已提交
21
          />
22 23
          <div
            :style="setActiveColor"
24
            class="uni-slider-track"
fxy060608's avatar
fxy060608 已提交
25
          />
fxy060608's avatar
fxy060608 已提交
26 27
        </div>
      </div>
28
      <span
29
        v-show="showValue"
30
        ref="uni-slider-value"
31
        class="uni-slider-value"
fxy060608's avatar
fxy060608 已提交
32
      >{{ sliderValue }}</span>
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39 40 41 42
    </div>
    <slot />
  </uni-slider>
</template>
<script>
import {
  emitter,
  listeners
} from 'uni-mixins'
import touchtrack from 'uni-mixins/touchtrack'
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

// 处理js计算小数时的精度问题
var computeController = {
  add: function (arg) {
    var r1, r2, m
    try {
      // 获得小数位数
      r1 = this.toString().split('.')[1].length
    } catch (e) {
      r1 = 0
    }
    try {
      // 获得小数位数
      r2 = arg.toString().split('.')[1].length
    } catch (e) {
      r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2))
    return (this * m + arg * m) / m
  },
  sub: function (arg) {
    return this.add(-arg)
  },
  mul: function (arg) {
    var m = 0; var s1 = this.toString(); var s2 = arg.toString()
    try {
      // 获得小数位数
      m += s1.split('.')[1].length
    } catch (e) { }
    try {
      // 获得小数位数
      m += s2.split('.')[1].length
    } catch (e) { }
    // 转为十进制计算后,要除以两个数的共同小数位数
    return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
  },
  div: function (arg) {
    var t1 = 0; var t2 = 0; var r1; var r2
    try {
      // 获得小数位数
      t1 = this.toString().split('.')[1].length
    } catch (e) { }
    try {
      // 获得小数位数
      t2 = arg.toString().split('.')[1].length
    } catch (e) { }
    r1 = Number(this.toString().replace('.', ''))
    r2 = Number(arg.toString().replace('.', ''))
    // 转为十进制计算后,要乘以除数与被除数小数位数的差
    return (r1 / r2) * Math.pow(10, t2 - t1)
  },
  mod: function (arg) {
    var t1 = 0; var t2 = 0; var r1; var r2
    try {
      t1 = this.toString().split('.')[1].length
    } catch (e) { }
    try {
      t2 = arg.toString().split('.')[1].length
    } catch (e) { }
    // 小数位数
    var digit = Math.pow(10, Math.abs(t1 - t2))
    // eslint-disable-next-line eqeqeq
    if (digit == 1) { digit = Math.pow(10, t1) }
    // 计算余数
    r1 = (this * digit).toString().split('.')[0]
    r2 = arg * digit
    // 小数点后数字,直接拼接上即可
    var decimals = (this * digit).toString().split('.')[1] ? (this * digit).toString().split('.')[1] : ''
    return (r1 % r2 + decimals) / digit
  }
}

fxy060608's avatar
fxy060608 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
export default {
  name: 'Slider',
  mixins: [emitter, listeners, touchtrack],
  props: {
    name: {
      type: String,
      default: ''
    },
    min: {
      type: [Number, String],
      default: 0
    },
    max: {
      type: [Number, String],
      default: 100
    },
    value: {
      type: [Number, String],
      default: 0
    },
    step: {
      type: [Number, String],
      default: 1
    },
    disabled: {
      type: [Boolean, String],
      default: false
    },
    color: {
      type: String,
      default: '#e9e9e9'
    },
    backgroundColor: {
      type: String,
      default: '#e9e9e9'
    },
    activeColor: {
      type: String,
      default: '#007aff'
    },
    selectedColor: {
      type: String,
      default: '#007aff'
    },
    blockColor: {
      type: String,
      default: '#ffffff'
    },
    blockSize: {
      type: [Number, String],
      default: 28
    },
    showValue: {
      type: [Boolean, String],
      default: false
    }
  },
  data () {
    return {
      sliderValue: Number(this.value)
    }
  },
  computed: {
    setBlockStyle () {
      return {
        width: this.blockSize + 'px',
        height: this.blockSize + 'px',
        marginLeft: -this.blockSize / 2 + 'px',
        marginTop: -this.blockSize / 2 + 'px',
        left: this._getValueWidth(),
        backgroundColor: this.blockColor
      }
    },
    setBgColor () {
      return {
        backgroundColor: this._getBgColor()
      }
    },
    setBlockBg () {
      return {
        left: this._getValueWidth()
      }
    },
    setActiveColor () { // 有问题,设置最大值最小值是有问题
      return {
        backgroundColor: this._getActiveColor(),
        width: this._getValueWidth()
      }
    }
  },
  watch: {
    value (val) {
      this.sliderValue = Number(val)
    }
  },
  mounted () {
    this.touchtrack(this.$refs['uni-slider-handle'], '_onTrack')
  },
  created () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'add',
      vm: this
    })
  },
  beforeDestroy () {
    this.$dispatch('Form', 'uni-form-group-update', {
      type: 'remove',
      vm: this
    })
  },
  methods: {
226 227 228 229 230
    _onUserChangedValue (e) {
      const sliderRightBox = this.$refs['uni-slider-value']
      const sliderRightBoxLeft = getComputedStyle(sliderRightBox, null).marginLeft
      let sliderRightBoxWidth = sliderRightBox.offsetWidth
      sliderRightBoxWidth = sliderRightBoxWidth + parseInt(sliderRightBoxLeft)
fxy060608's avatar
fxy060608 已提交
231
      const slider = this.$refs['uni-slider']
232
      const offsetWidth = slider.offsetWidth - (this.showValue ? sliderRightBoxWidth : 0)
fxy060608's avatar
fxy060608 已提交
233 234
      const boxLeft = slider.getBoundingClientRect().left
      const value = (e.x - boxLeft) * (this.max - this.min) / offsetWidth + Number(this.min)
fxy060608's avatar
fxy060608 已提交
235 236
      this.sliderValue = this._filterValue(value)
    },
237 238
    _filterValue (e) {
      const max = Number(this.max)
239
      const min = Number(this.min)
240
      return e < min ? min : e > max ? max : computeController.mul.call(Math.round((e - min) / this.step), this.step) + min
fxy060608's avatar
fxy060608 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253
    },
    _getValueWidth () {
      return 100 * (this.sliderValue - this.min) / (this.max - this.min) + '%'
    },
    _getBgColor () {
      return this.backgroundColor !== '#e9e9e9' ? this.backgroundColor : (this.color !== '#007aff' ? this.color
        : '#007aff')
    },
    _getActiveColor () {
      return this.activeColor !== '#007aff' ? this.activeColor : (this.selectedColor !== '#e9e9e9' ? this.selectedColor
        : '#e9e9e9')
    },
    _onTrack: function (e) {
254
      if (!this.disabled) {
fxy060608's avatar
fxy060608 已提交
255 256 257 258
        return e.detail.state === 'move' ? (this._onUserChangedValue({
          x: e.detail.x0
        }), this.$trigger('changing', e, {
          value: this.sliderValue
fxy060608's avatar
fxy060608 已提交
259
        }), !1) : (e.detail.state === 'end' && this.$trigger('change', e, {
fxy060608's avatar
fxy060608 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
          value: this.sliderValue
        }))
      }
    },
    _onClick ($event) {
      if (this.disabled) {
        return
      }
      this._onUserChangedValue($event)
      this.$trigger('change', $event, {
        value: this.sliderValue
      })
    },
    _resetFormData () {
      this.sliderValue = this.min
    },
    _getFormData () {
fxy060608's avatar
fxy060608 已提交
277
      const data = {}
fxy060608's avatar
fxy060608 已提交
278
      if (this.name !== '') {
fxy060608's avatar
fxy060608 已提交
279 280
        data.value = this.sliderValue
        data.key = this.name
fxy060608's avatar
fxy060608 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
      }
      return data
    }
  }
}
</script>
<style>
	uni-slider {
		margin: 10px 18px;
		padding: 0;
		display: block;
	}

	uni-slider[hidden] {
		display: none;
	}

	uni-slider .uni-slider-wrapper {
		display: -webkit-flex;
		display: flex;
		-webkit-align-items: center;
		align-items: center;
		min-height: 16px;
	}

	uni-slider .uni-slider-tap-area {
		-webkit-flex: 1;
		flex: 1;
		padding: 8px 0;
	}

	uni-slider .uni-slider-handle-wrapper {
		position: relative;
		height: 2px;
		border-radius: 5px;
		background-color: #e9e9e9;
		cursor: pointer;
		transition: background-color 0.3s ease;
		-webkit-tap-highlight-color: transparent;
	}

	uni-slider .uni-slider-track {
		height: 100%;
		border-radius: 6px;
		background-color: #007aff;
		transition: background-color 0.3s ease;
	}

	uni-slider .uni-slider-handle,
	uni-slider .uni-slider-thumb {
		position: absolute;
		left: 50%;
		top: 50%;
		cursor: pointer;
		border-radius: 50%;
		transition: border-color 0.3s ease;
	}

	uni-slider .uni-slider-handle {
		width: 28px;
		height: 28px;
		margin-top: -14px;
		margin-left: -14px;
		background-color: transparent;
		z-index: 3;
346
		cursor: grab;
fxy060608's avatar
fxy060608 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
	}

	uni-slider .uni-slider-thumb {
		z-index: 2;
		box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
	}

	uni-slider .uni-slider-step {
		position: absolute;
		width: 100%;
		height: 2px;
		background: transparent;
		z-index: 1;
	}

362
	uni-slider .uni-slider-value {
363
    width: 3ch;
fxy060608's avatar
fxy060608 已提交
364 365
		color: #888;
		font-size: 14px;
366
		margin-left: 1em;
fxy060608's avatar
fxy060608 已提交
367 368 369 370 371 372 373 374 375 376
	}

	uni-slider .uni-slider-disabled .uni-slider-track {
		background-color: #ccc;
	}

	uni-slider .uni-slider-disabled .uni-slider-thumb {
		background-color: #FFF;
		border-color: #ccc;
	}
377
</style>