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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>document</title>
<style>
.kuang {
width: 700px;
height: 7px;
/* background: #c03; */
border: 1px solid #000;
position: relative;
border-radius: 7px;
/* margin-left: 30%; */
/* margin-top: 20%; */
box-sizing: border-box;
/*overflow: hidden;*/
}
.overflow {
overflow: hidden;
width: 700px;
height: 7px;
border-radius: 7px;;
position: absolute;
left: -1px;
top: -1px;
}
.bg {
position: absolute;
background-color: pink;
/* border-radius: 30px; */
width: 700px;
height: 9px;
top: -1px;
left: -100%;
}
.dot {
position: absolute;
display: inline-block;
width: 16px;
height: 16px;
border-radius: 50%;
background: pink;
top: 50%;
left: 0;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="kuang">
<div class="overflow">
<div class="bg"></div>
</div>
<a class="dot" draggable="false">
<div class="line"></div>
</a>
</div>
<script type="text/javascript">
window.onload = function() {
var kuang = document.querySelector(".kuang");
var dot = document.querySelector('.dot');
var bg = document.querySelector('.bg');
var load1 = kuang.clientWidth * 0.1666;
var load2 = kuang.clientWidth * 0.3333;
var load3 = kuang.clientWidth * 0.5;
var load4 = kuang.clientWidth * 0.6666;
var load5 = kuang.clientWidth * 0.8333;
var load6 = kuang.clientWidth;
dot.onmousedown = function() {
document.onmousemove = function(e) {
var e = e || window.event;
var x = e.clientX;
var y = e.clientY;
var mx = x - kuang.offsetLeft;
var my = y - kuang.offsetTop;
if(mx < load1) {
dot.style.left = 0;
bg.style.left = -load6 + 'px';
} else if(mx > load1 && mx < load3) {
dot.style.left = load2 + 'px';
bg.style.left = -load4 + 'px';
} else if(mx > load3 && mx < load5) {
dot.style.left = load4 + 'px';
bg.style.left = -load2 + 'px';
} else if(mx > load5) {
dot.style.left = load6 + 'px';
bg.style.left = 0;
}
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
}
}
kuang.onclick = function(e) {
var e = e || window.event;
var x = e.clientX;
var y = e.clientY;
var mx = x - kuang.offsetLeft;
var my = y - kuang.offsetTop;
if(mx < load1) {
dot.style.left = 0;
bg.style.left = -load6 + 'px';
} else if(mx > load1 && mx < load3) {
dot.style.left = load2 + 'px';
bg.style.left = -load4 + 'px';
} else if(mx > load3 && mx < load5) {
dot.style.left = load4 + 'px';
bg.style.left = -load2 + 'px';
} else if(mx > load5) {
dot.style.left = load6 + 'px';
bg.style.left = 0;
}
}
document.onmouseup = function() {
document.onmousemove = null;
}
/*移动端try*/
function touchmove(event) {
var touch = event.targetTouches[0];
var mx = touch.pageX - kuang.offsetLeft;
if(event.targetTouches.length == 1) {    
event.preventDefault();
if(mx < load1) {
dot.style.left = 0;
bg.style.left = -load6 + 'px';
} else if(mx > load1 && mx < load3) {
dot.style.left = load2 + 'px';
bg.style.left = -load4 + 'px';
} else if(mx > load3 && mx < load5) {
dot.style.left = load4 + 'px';
bg.style.left = -load2 + 'px';
} else if(mx > load5) {
dot.style.left = load6 + 'px';
bg.style.left = 0;
}
}
}

function touchstart(e) {
kuang.addEventListener('touchmove', touchmove, false);
}
dot.addEventListener("touchstart", touchstart, false);
document.addEventListener("touchend", function() {
//dot.removeEventListener("touchstart",touchstart,false);
kuang.removeEventListener("touchmove", touchmove, false);
});
}
</script>
</body>
</html>

其中有注意的问题!!!

1
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();  //实现元素拖动时,出现禁止图标。