| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import { use } from '../utils';
- import { TouchMixin } from '../mixins/touch';
- import { preventDefault } from '../utils/event';
- const [sfc, bem] = use('slider');
- export default sfc({
- mixins: [TouchMixin],
- props: {
- min: Number,
- value: Number,
- disabled: Boolean,
- vertical: Boolean,
- activeColor: String,
- inactiveColor: String,
- max: {
- type: Number,
- default: 100
- },
- step: {
- type: Number,
- default: 1
- },
- barHeight: {
- type: String,
- default: '2px'
- }
- },
- methods: {
- onTouchStart(event) {
- if (this.disabled) return;
- this.touchStart(event);
- this.startValue = this.format(this.value);
- },
- onTouchMove(event) {
- preventDefault(event, true);
- if (this.disabled) return;
- this.touchMove(event);
- const rect = this.$el.getBoundingClientRect();
- const delta = this.vertical ? this.deltaY : this.deltaX;
- const total = this.vertical ? rect.height : rect.width;
- const diff = (delta / total) * 100;
- this.newValue = this.startValue + diff;
- this.updateValue(this.newValue);
- },
- onTouchEnd() {
- if (this.disabled) return;
- this.updateValue(this.newValue, true);
- },
- onClick(event) {
- event.stopPropagation();
- if (this.disabled) return;
- const rect = this.$el.getBoundingClientRect();
- const delta = this.vertical ? event.clientY - rect.top : event.clientX - rect.left;
- const total = this.vertical ? rect.height : rect.width;
- const value = (delta / total) * 100;
- this.updateValue(value, true);
- },
- updateValue(value, end) {
- value = this.format(value);
- this.$emit('input', value);
- if (end) {
- this.$emit('change', value);
- }
- },
- format(value) {
- return (
- Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step
- );
- }
- },
- render(h) {
- const { vertical } = this;
- const style = {
- background: this.inactiveColor
- };
- const mainAxis = vertical ? 'height' : 'width';
- const crossAxis = vertical ? 'width' : 'height';
- const barStyle = {
- [mainAxis]: `${this.format(this.value)}%`,
- [crossAxis]: this.barHeight,
- background: this.activeColor
- };
- return (
- <div
- style={style}
- class={bem({ disabled: this.disabled, vertical })}
- onClick={this.onClick}
- >
- <div class={bem('bar')} style={barStyle}>
- <div
- class={bem('button-wrapper')}
- onTouchstart={this.onTouchStart}
- onTouchmove={this.onTouchMove}
- onTouchend={this.onTouchEnd}
- onTouchcancel={this.onTouchEnd}
- >
- {this.slots('button') || <div class={bem('button')} />}
- </div>
- </div>
- </div>
- );
- }
- });
|