本文實例為大家分享了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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
< template > < div > < div class = "slider" ref = "slider" > < div class = "process" :style = "{ width }" ></ div > < div class = "thunk" ref = "trunk" :style = "{ left }" > < div class = "block" ></ div > < div class = "tips" > <!-- <span>{{scale*100}}</span> --> < i class = "fas fa-caret-down" ></ i > </ div > </ div > </ div > < div > < button @click=" () => { this.per++; } " > +</ button >{{ per }}%< button @click=" () => { if (this.per > 0) { this.per--; } } " > - </ button > </ div > </ div > </ template > < script > /* * min 進度條最小值 * max 進度條最大值 * v-model 對當前值進行雙向綁定實時顯示拖拽進度 * */ export default { props: ["min", "max", "value"], data() { return { slider: null, //滾動條DOM元素 thunk: null, //拖拽DOM元素 per: this.value, //當前值 }; }, //渲染到頁面的時候 mounted() { this.slider = this.$refs.slider; this.thunk = this.$refs.trunk; var _this = this; this.thunk.onmousedown = function (e) { var width = parseInt(_this.width); var disX = e.clientX; document.onmousemove = function (e) { // value, left, width // 當value變化的時候,會通過計算屬性修改left,width // 拖拽的時候獲取的新width var newWidth = e.clientX - disX + width; // 拖拽的時候得到新的百分比 var scale = newWidth / _this.slider.offsetWidth; _this.per = Math.ceil((_this.max - _this.min) * scale + _this.min); _this.per = Math.max(_this.per, _this.min); _this.per = Math.min(_this.per, _this.max); _this.$emit("input", _this.per); }; document.onmouseup = function () { document.onmousemove = document.onmouseup = null; }; return false; }; }, computed: { // 設置一個百分比,提供計算slider進度寬度和trunk的left值 // 對應公式為 當前值-最小值/最大值-最小值 = slider進度width / slider總width // trunk left = slider進度width + trunk寬度/2 scale() { return (this.per - this.min) / (this.max - this.min); }, width() { if (this.slider) { return this.slider.offsetWidth * this.scale + "px"; } else { return 0 + "px"; } }, left() { if (this.slider) { return ( this.slider.offsetWidth * this.scale - this.thunk.offsetWidth / 2 + "px" ); } else { return 0 + "px"; } }, }, }; </ script > < style > .box { margin: 100px auto 0; width: 80%; } .clear:after { content: ""; display: block; clear: both; } .slider { user-select: none; position: relative; margin: 20px 0; width: 400px; height: 10px; background: #e4e7ed; border-radius: 5px; cursor: pointer; } .slider .process { position: absolute; left: 0; top: 0; width: 112px; height: 10px; border-radius: 5px; background: #81b159; } .slider .thunk { position: absolute; left: 100px; top: -7px; width: 20px; height: 20px; } .slider .block { width: 20px; height: 20px; border-radius: 50%; border: 2px solid #409eff; background: rgba(255, 255, 255, 1); transition: 0.2s all; } .slider .tips { position: absolute; left: -7px; bottom: 30px; min-width: 15px; text-align: center; padding: 4px 8px; /* background: #000; */ border-radius: 5px; height: 24px; color: #fff; } .slider .tips i { position: absolute; margin-left: -5px; left: 50%; bottom: -9px; font-size: 16px; color: #000; } .slider .block:hover { transform: scale(1.1); opacity: 0.6; } </ style > |
調用:
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
|
< template > < slider :min = "0" :max = "100" v-model = "per" ></ slider > </ template > < script > import slider from "@/components/slider"; export default { data() { return {}; }, computed: { per: { get() { return 0; }, set(val) { console.log(val); }, }, }, components: { slider }, mounted() {}, methods: {}, }; </ script > < style > </ style > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_46476460/article/details/114139706