-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseSlider.vue
132 lines (126 loc) · 2.98 KB
/
BaseSlider.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
<template>
<div :style="{'height': vertical? sliderSize: ''}" class="slider__cont">
<div :class="[`${theme}`,'slider']"
:style="{'width': sliderSize, 'transform': vertical? `rotateZ(-90deg) translateX(${translateX})`:''}">
<input type="range" style="width:100%;"
:name="uuid" :ref="uuid" id="" :value="value" :min="min" :max="max"
@input="onInput($event)" @change="onChange($event)">
</div>
</div>
</template>
<style>
.slider__cont {
display: inline-flex;
justify-content: center;
}
.slider {
padding: 15px;
}
.dark {
background: #333;
}
input[type=range] {
margin-top: 8px;
outline: none;
-webkit-appearance: none;/*清除系统默认样式*/
background: -webkit-linear-gradient(#61bd12, #61bd12) no-repeat, #ddd;
background-size: 0% 100%;
border-radius: 2px;
height: 3px;/*横条的高度*/
}
input[type=range]:focus {
-webkit-appearance: none;
}
/*拖动块的样式*/
input[type=range]::-webkit-slider-thumb {
content: 'val:x';
-webkit-appearance: none;/*清除系统默认样式*/
-moz-appearance: none;
height: 16px;/*拖动块高度*/
width: 16px;/*拖动块宽度*/
background: #fff;/*拖动块背景*/
border-radius: 50%; /*外观设置为圆形*/
border: solid 1px #ddd; /*设置边框*/
transition: transform .1s;
}
input[type=range]::-webkit-slider-thumb::after {
content: '';
}
input[type=range]::-webkit-slider-thumb:active {
-webkit-appearance: none;
-moz-appearance: none;
transform: scale(1.1);
/* background: #eee; */
}
</style>
<script>
export default {
props: {
value: {
type: Number,
default: 1,
},
theme: {
type: String,
default: 'light',
},
vertical: {
type: Boolean,
default: false,
},
size: {
type: Number,
default: 300,
},
min: {
type: Number,
default: 1,
},
max: {
type: Number,
default: 100,
},
},
data () {
return {
sliderVal: this.value,
};
},
computed: {
uuid () {
return `slider + ${Math.round(Math.random()*1000000)}`;
},
isVertical() {
if (this.vertical) {
console.warn(`vertical slider...`);
return true;
}
else console.warn('horizontall slider..');
return false;
},
sliderSize() {
return `${this.size}px`;
},
translateX() {
return `${- this.size / 2}px`;
},
},
methods: {
onInput(evt) {
const newVal = this.changeStyle();
this.$emit('input', newVal);
},
onChange(evt) {
const newVal = this.changeStyle();
this.$emit('change');
},
changeStyle() {
const inputRange = this.$refs[this.uuid];
const newVal = Number(inputRange.value);
const ratio = (newVal - this.min) * 100 / (this.max - this.min);
inputRange.style.backgroundSize = `${ratio}% 100%`;
return newVal;
}
},
}
</script>