-
Notifications
You must be signed in to change notification settings - Fork 2
/
Slide.js
175 lines (153 loc) · 4.26 KB
/
Slide.js
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
175
/*class Slide {
constructor(){
this.data = 0;
}
setData(data){
this.data = data;
this.setPercentage()
}
render(cb){
this.clickDot(cb);
}
clickDot(callback) {
document.querySelector(".sensitivity .dot").addEventListener("mousedown", (e) => {
document.onmousemove = (e) => {
this.setCoordinate(e.clientX);
callback()
};
document.onmouseup = () => {
document.onmousemove = null;
}
});
}
//得到当前的百分数
getPercentage() {
// let lineWidth = document.getElementById("sensitivity").offsetWidth;
// let progressWidth = document.querySelector(".progress").offsetWidth;
// return progressWidth / lineWidth
return this.data;
}
//设置百分数来改变进度
setPercentage() {
if (this.data < 0) {
this.data = 0;
}
if (this.data > 100) {
this.data = 100;
}
let sensitivityWidth = document.getElementById("sensitivity").offsetWidth;
Slide.setDot(this.data * sensitivityWidth * 0.01 - 3 + 'px');
Slide.setProgress(this.data * sensitivityWidth * 0.01 + "px");
}
//通过坐标来设置进度
setCoordinate(moveTo) {
let sensitivity = document.getElementById("sensitivity");
if (moveTo < sensitivity.offsetLeft) {
moveTo = sensitivity.offsetLeft;
}
if (moveTo > sensitivity.offsetLeft + sensitivity.offsetWidth) {
moveTo = sensitivity.offsetLeft + sensitivity.offsetWidth;
}
Slide.setDot(moveTo - sensitivity.offsetLeft - 3 + "px");
Slide.setProgress(moveTo - sensitivity.offsetLeft + 'px');
//设置data
this.data = (moveTo - sensitivity.offsetLeft)/document.querySelector(".sensitivity").offsetWidth*100
}
//设置点的位置
static setDot(position) {
document.querySelector(".dot").style.left = position;
}
//设置进度的位置
static setProgress(position) {
document.getElementById("progress").style.width = position;
}
}
//通过自定义事件来降低控件之间的依赖,只需要同时监听这个事件就好
//需要自定义一个进度变化的事件,然后其他的组件(小圆点,条形进度,数值显示)来监听这个事件,同时做出相应的反应
//
//在初始的实现中:如果输入框数值变了就通过setData函数来改变变量的值,同时调用setPercentage方法来渲染view。如果滑动条变了也是修改data的值然后在调用对应的方法来修改view,同时调用回调函数来修改输入框中的数值。
//
//
//
//58:34
*/
//这里是自定义事件
// let myEvent = new CustomEvent("userLogin", {
// detail: {
// username: "davidwalsh"
// }
// });
//
// document.addEventListener("userLogin",(e)=>{
// console.log(e)
// })
//
// // 触发它!
// document.dispatchEvent(myEvent);
class Slide {
constructor(dot, progress, father) {
this.data = 0;
//判断是否存在
this.dot = dot;
this.progress = progress;
//父组件
this.father = father;
}
setData(data) {
this.data = data;
this.setPercentage()
}
render() {
this.clickDot();
}
clickDot() {
let sensitivity = this.father.querySelector(".sensitivity");
let dot = this.father.querySelector(".sensitivity .dot");
//自定义事件,通过监听事件来重新渲染dom
const slideEvent = new CustomEvent("slide");
dot.addEventListener("mousedown", (e) => {
this.father.onmousemove = (e) => {
//设置data
this.setData((e.clientX - sensitivity.offsetLeft) / sensitivity.offsetWidth * 100);
document.dispatchEvent(slideEvent);
};
this.father.onmouseup = () => {
this.father.onmousemove = null;
}
});
}
//设置百分数来改变进度
setPercentage() {
if (this.data < 0) {
this.data = 0;
}
if (this.data > 100) {
this.data = 100;
}
let sensitivityWidth = this.father.querySelector(".sensitivity").offsetWidth;
if (this.dot) {
this.setDot(this.data * sensitivityWidth * 0.01 - 3 + 'px');
}
if (this.progress) {
this.setProgress(this.data * sensitivityWidth * 0.01 + "px");
}
}
//得到当前的百分数
getPercentage() {
return this.data;
}
//设置点的位置
setDot(position) {
let dot = this.father.querySelector(".sensitivity .dot");
if (dot) {
dot.style.left = position;
}
}
//设置进度的位置
setProgress(position) {
let progress = this.father.querySelector(".sensitivity .progress");
if (progress) {
progress.style.width = position;
}
}
}