|
| 1 | +<template lang="pug"> |
| 2 | +.multiRangeSlider |
| 3 | + input.multiRange.multiRange--min(name="min", type="range", :min="min", :max="max", :value="value[0]", @input="onChange") |
| 4 | + input.multiRange.multiRange--max(name="max", type="range", :min="min", :max="max", :value="value[1]", @input="onChange") |
| 5 | + .rangeBar(:style="barLenStyle") |
| 6 | + .rangeLabel |
| 7 | + label.label.label--min {{ toNumberAbbr(value[0]) }} |
| 8 | + label.label.label--max {{ toNumberAbbr(value[1]) }} |
| 9 | +</template> |
| 10 | + |
| 11 | +<script> |
| 12 | +const rangeColor = '#15a4fa' |
| 13 | +
|
| 14 | +export default { |
| 15 | + props: { |
| 16 | + min: Number, |
| 17 | + max: Number, |
| 18 | + value: Array |
| 19 | + }, |
| 20 | + computed: { |
| 21 | + rangePercentage () { |
| 22 | + return { |
| 23 | + start: `${this.value[0] / (this.max - this.min) * 100}%`, |
| 24 | + end: `${this.value[1] / (this.max - this.min) * 100}%` |
| 25 | + } |
| 26 | + }, |
| 27 | + barLenStyle () { |
| 28 | + return {background: `linear-gradient(to right, transparent ${this.rangePercentage.start}, ${rangeColor} ${this.rangePercentage.start}, ${rangeColor} ${this.rangePercentage.end}, transparent ${this.rangePercentage.end}) no-repeat`} |
| 29 | + } |
| 30 | + }, |
| 31 | + methods: { |
| 32 | + onChange (event) { |
| 33 | + let result = event.target.name === 'min' |
| 34 | + ? [Math.min(event.target.value, this.value[1] - 1), this.value[1]] |
| 35 | + : [this.value[0], Math.max(this.value[0] - 1, event.target.value)] |
| 36 | + this.$emit('input', result) |
| 37 | + }, |
| 38 | + toNumberAbbr (num) { |
| 39 | + return num < 1000 ? num : `${Math.floor(num / 1000)}k` |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +
|
| 44 | +</script> |
| 45 | + |
| 46 | +<style lang="sass" scoped> |
| 47 | +@import '../sass/variables' |
| 48 | +@import '../sass/utils' |
| 49 | +
|
| 50 | +$track-color: $bg-color |
| 51 | +$range-color: $primary-color |
| 52 | +$knob-color: #fff |
| 53 | +$knob-size: 16px |
| 54 | +
|
| 55 | +.multiRangeSlider |
| 56 | + position: relative |
| 57 | + width: 100% |
| 58 | +
|
| 59 | +=bar |
| 60 | + position: absolute |
| 61 | + top: $knob-size / 4 |
| 62 | + width: 100% |
| 63 | + height: $knob-size / 2 |
| 64 | + border-radius: $knob-size / 2 |
| 65 | + pointer-events: none |
| 66 | +
|
| 67 | +=knob |
| 68 | + width: $knob-size |
| 69 | + height: $knob-size |
| 70 | + border-radius: 50% |
| 71 | + background: $knob-color |
| 72 | + cursor: pointer |
| 73 | + z-index: 3 |
| 74 | +
|
| 75 | +
|
| 76 | +.multiRange |
| 77 | + display: inline-block |
| 78 | + vertical-align: top |
| 79 | + width: 100% |
| 80 | + padding: 0 |
| 81 | + margin: 0 |
| 82 | + outline: none |
| 83 | + -webkit-appearance: none |
| 84 | + -moz-appearance: none |
| 85 | + background: none |
| 86 | +
|
| 87 | + &::-webkit-slider-thumb |
| 88 | + position: relative |
| 89 | + -webkit-appearance: none |
| 90 | + -moz-appearance: none |
| 91 | + +knob |
| 92 | +
|
| 93 | + &::-moz-range-thumb |
| 94 | + transform: scale(1) |
| 95 | + +knob |
| 96 | +
|
| 97 | + &::before |
| 98 | + content: '' |
| 99 | + +bar |
| 100 | + background: $track-color |
| 101 | +
|
| 102 | +.multiRange--min |
| 103 | + position: absolute |
| 104 | +
|
| 105 | +.multiRange--max |
| 106 | + position: relative |
| 107 | +
|
| 108 | +.rangeBar |
| 109 | + +bar |
| 110 | + z-index: 2 |
| 111 | +
|
| 112 | +.rangeLabel |
| 113 | + @extend %clearfix |
| 114 | +
|
| 115 | +.label |
| 116 | + padding: 0.5em 0 0 |
| 117 | +
|
| 118 | +.label--min |
| 119 | + float: left |
| 120 | +
|
| 121 | +.label--max |
| 122 | + float: right |
| 123 | +</style> |
0 commit comments