-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path滑动条.html
96 lines (79 loc) · 2.68 KB
/
滑动条.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
background: yellowgreen;
}
.container {
width: 300px;
height:30px;
display: flex;
flex-flow: row nowrap;
}
.left,
.right {
height: 100%;
}
.left {
background: red;
}
.right {
background: blue;
}
.drag {
background: #eee;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="drag">Drag</div>
<div class="right">right</div>
</div>
<script>
var dragDom = document.querySelector('.drag'),
left = document.querySelector('.left'),
right = document.querySelector('.right'),
container = document.querySelector('.container'),
mousedownX = 0,
lW = left.offsetWidth,
num1 = 4000,
num2 = 4000;
right.style.width = right.offsetWidth;
left.style.width = num1 / (num2 + num1)*100 + '%';
right.style.width = num2 / (num2 + num1)*100 + '%';
console.log(container.offsetWidth)
var mouseMoveHandler = function (e) {
var width = e.changedTouches[0].clientX - mousedownX;
var calcLW = lW + width;
calcLW < 0 ? calcLW = 0 : 1;
calcLW > container.offsetWidth ? calcLW = container.offsetWidth : 0;
left.style.width = calcLW + 'px'
right.style.width = container.offsetWidth - calcLW + 'px'
console.log(left.style.width,right.style.width)
// 不需要判断滑动方向 只需要绑定width就行了
// moveEndX = e.changedTouches[0].pageX;
// var isMoveRight = moveEndX - startX > 0 ? console.log("右边") : console.log("左边");
}
window.onload = function () {
dragDom.addEventListener('touchstart', function (e) {
mousedownX = e.changedTouches[0].clientX;
lW = left.offsetWidth
// startX = e.changedTouches[0].pageX;
})
dragDom.addEventListener('touchmove', mouseMoveHandler)
dragDom.addEventListener('touchend', function () {
dragDom.removeEventListener('mousemove', mouseMoveHandler)
})
}
</script>
</body>
</html>