index.vue 7.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
2 3 4
  <uni-slider
    ref="uni-slider"
    v-on="$listeners"
fxy060608's avatar
fxy060608 已提交
5 6
    @click="_onClick"
  >
fxy060608's avatar
fxy060608 已提交
7 8
    <div class="uni-slider-wrapper">
      <div class="uni-slider-tap-area">
9 10
        <div
          :style="setBgColor"
fxy060608's avatar
fxy060608 已提交
11 12
          class="uni-slider-handle-wrapper"
        >
13 14 15
          <div
            ref="uni-slider-handle"
            :style="setBlockBg"
fxy060608's avatar
fxy060608 已提交
16 17
            class="uni-slider-handle"
          />
18 19
          <div
            :style="setBlockStyle"
fxy060608's avatar
fxy060608 已提交
20 21
            class="uni-slider-thumb"
          />
22 23
          <div
            :style="setActiveColor"
fxy060608's avatar
fxy060608 已提交
24 25
            class="uni-slider-track"
          />
fxy060608's avatar
fxy060608 已提交
26 27
        </div>
      </div>
28
      <span
29 30
        v-show="showValue"
        ref="uni-slider-value"
fxy060608's avatar
fxy060608 已提交
31 32
        class="uni-slider-value"
      >{{ sliderValue }}</span>
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39 40 41 42 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 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
    </div>
    <slot />
  </uni-slider>
</template>
<script>
import {
  emitter,
  listeners
} from 'uni-mixins'
import touchtrack from 'uni-mixins/touchtrack'
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: {
154 155 156 157 158
    _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 已提交
159
      const slider = this.$refs['uni-slider']
160
      const offsetWidth = slider.offsetWidth - (this.showValue ? sliderRightBoxWidth : 0)
fxy060608's avatar
fxy060608 已提交
161 162
      const boxLeft = slider.getBoundingClientRect().left
      const value = (e.x - boxLeft) * (this.max - this.min) / offsetWidth + Number(this.min)
fxy060608's avatar
fxy060608 已提交
163 164
      this.sliderValue = this._filterValue(value)
    },
165 166 167 168 169
    _filterValue (e) {
      const max = Number(this.max)
      const min = Number(this.min)
      return e < min ? min : e > max ? max : Math.round((e - min) / this
        .step) * this.step + min
fxy060608's avatar
fxy060608 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182
    },
    _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) {
fxy060608's avatar
fxy060608 已提交
183
      if (!this.disabled) {
fxy060608's avatar
fxy060608 已提交
184 185 186 187
        return e.detail.state === 'move' ? (this._onUserChangedValue({
          x: e.detail.x0
        }), this.$trigger('changing', e, {
          value: this.sliderValue
fxy060608's avatar
fxy060608 已提交
188
        }), !1) : (e.detail.state === 'end' && this.$trigger('change', e, {
fxy060608's avatar
fxy060608 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
          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 已提交
206
      const data = {}
fxy060608's avatar
fxy060608 已提交
207
      if (this.name !== '') {
fxy060608's avatar
fxy060608 已提交
208 209
        data.value = this.sliderValue
        data.key = this.name
fxy060608's avatar
fxy060608 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
      }
      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;
275
		cursor: grab;
fxy060608's avatar
fxy060608 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	}

	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;
	}

291 292
	uni-slider .uni-slider-value {
    width: 3ch;
fxy060608's avatar
fxy060608 已提交
293 294
		color: #888;
		font-size: 14px;
295
		margin-left: 1em;
fxy060608's avatar
fxy060608 已提交
296 297 298 299 300 301 302 303 304 305
	}

	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;
	}
306
</style>