mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 10:20:19 +08:00
[improvement] Slider: jsx (#2622)
This commit is contained in:
parent
0f1cb12211
commit
8354c6a8d6
@ -1,35 +1,9 @@
|
|||||||
<template>
|
import { use } from '../utils';
|
||||||
<div
|
|
||||||
:class="b({ disabled })"
|
|
||||||
:style="style"
|
|
||||||
@click.stop="onClick"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
:class="b('bar')"
|
|
||||||
:style="barStyle"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
:class="b('button-wrapper')"
|
|
||||||
@touchstart="onTouchStart"
|
|
||||||
@touchmove.prevent.stop="onTouchMove"
|
|
||||||
@touchend="onTouchEnd"
|
|
||||||
@touchcancel="onTouchEnd"
|
|
||||||
>
|
|
||||||
<slot name="button">
|
|
||||||
<div :class="b('button')" />
|
|
||||||
</slot>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import create from '../utils/create';
|
|
||||||
import Touch from '../mixins/touch';
|
import Touch from '../mixins/touch';
|
||||||
|
|
||||||
export default create({
|
const [sfc, bem] = use('slider');
|
||||||
name: 'slider',
|
|
||||||
|
|
||||||
|
export default sfc({
|
||||||
mixins: [Touch],
|
mixins: [Touch],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
@ -52,22 +26,6 @@ export default create({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
computed: {
|
|
||||||
style() {
|
|
||||||
return {
|
|
||||||
background: this.inactiveColor
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
barStyle() {
|
|
||||||
return {
|
|
||||||
width: this.format(this.value) + '%',
|
|
||||||
height: this.barHeight,
|
|
||||||
background: this.activeColor
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onTouchStart(event) {
|
onTouchStart(event) {
|
||||||
if (this.disabled) return;
|
if (this.disabled) return;
|
||||||
@ -77,11 +35,14 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onTouchMove(event) {
|
onTouchMove(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
|
||||||
if (this.disabled) return;
|
if (this.disabled) return;
|
||||||
|
|
||||||
this.touchMove(event);
|
this.touchMove(event);
|
||||||
const rect = this.$el.getBoundingClientRect();
|
const rect = this.$el.getBoundingClientRect();
|
||||||
const diff = this.deltaX / rect.width * 100;
|
const diff = (this.deltaX / rect.width) * 100;
|
||||||
this.updateValue(this.startValue + diff);
|
this.updateValue(this.startValue + diff);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -91,10 +52,12 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onClick(event) {
|
onClick(event) {
|
||||||
|
event.stopPropagation();
|
||||||
|
|
||||||
if (this.disabled) return;
|
if (this.disabled) return;
|
||||||
|
|
||||||
const rect = this.$el.getBoundingClientRect();
|
const rect = this.$el.getBoundingClientRect();
|
||||||
const value = (event.clientX - rect.left) / rect.width * 100;
|
const value = ((event.clientX - rect.left) / rect.width) * 100;
|
||||||
this.updateValue(value, true);
|
this.updateValue(value, true);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -108,8 +71,35 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
format(value) {
|
format(value) {
|
||||||
return (Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step);
|
return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
render(h) {
|
||||||
|
const style = {
|
||||||
|
background: this.inactiveColor
|
||||||
|
};
|
||||||
|
|
||||||
|
const barStyle = {
|
||||||
|
width: `${this.format(this.value)}%`,
|
||||||
|
height: this.barHeight,
|
||||||
|
background: this.activeColor
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={style} class={bem({ disabled: this.disabled })} 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>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
|
Loading…
x
Reference in New Issue
Block a user