-
Notifications
You must be signed in to change notification settings - Fork 27
/
cell-swiper.html
252 lines (224 loc) · 7.6 KB
/
cell-swiper.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>cell swiper</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<style>
* {
margin: 0;
padding: 0;
}
.c-cell-swiper {
position: relative;
border-bottom: 1px solid #ccc;
}
.cell-content {
width: 100%;
overflow: hidden;
padding: 0 10px;
box-sizing: border-box;
background-color: #fff;
position: absolute;
left: 0;
top: 0;
z-index: 2;
}
.cell-content .your-code {
position: relative;
width: 100%;
height: 100%;
}
.cell-content .left {
float: left;
padding: 15px 0;
margin-left: 58px;
}
.cell-content .icon {
position: absolute;
top: 50%;
left: 0;
width: 48px;
height: 48px;
margin-top: -24px;
display: inline-block;
border-radius: 4px;
}
.cell-content .left .sub {
color: #ccc;
font-size: 12px;
margin-top: 4px;
}
.cell-content .right {
float: right;
padding: 15px 0;
font-size: 12px;
color: #666;
}
.cell-btn-group {
position: absolute;
right: 0;
top: 0;
bottom: 0;
z-index: 1;
display: flex;
}
.cell-btn-group .cell-btn {
flex-shrink: 0;
height: 100%;
min-width: 60px;
text-align: center;
padding: 0 10px;
color: #fff;
}
.cell-btn-group .cell-btn:first-child {
background-color: #999;
}
.cell-btn-group .cell-btn:last-child {
background-color: #f00;
}
</style>
</head>
<body>
<div class="c-cell-swiper" id="wrapper">
<div class="cell-content" id="content">
<div class="your-code">
<img class="icon" src="./images/t.jpg"></img>
<div class="left">
<span>萌萌的卡洛奇</span>
<p class="sub">我这个月要来看你啦</p>
</div>
<div class="right">now</div>
</div>
</div>
<div class="cell-btn-group" id="btnGroup">
<div class="cell-btn">标为未读</div>
<div class="cell-btn">删除</div>
</div>
</div>
<script>
var el = document.querySelector('#content');
var btn = document.querySelector('#btnGroup');
var wrapper = document.querySelector('#wrapper');
var swiping = false;
var opened = false;
var btnGroupWidth = 0;
var wrapperHeight = 48;
var dragState = {};
var prevent = true;
var stopPropagation = true;
var userScrolling = false;
var animating = false;
function getBtnGroupWidth() {
btnGroupWidth = btn.getBoundingClientRect().width;
wrapperHeight = el.getBoundingClientRect().height;
wrapper.style.height = wrapperHeight + 'px';
el.children[0].style.height = wrapperHeight + 'px';
btn.style.lineHeight = wrapperHeight + 'px';
}
function translate(element, offset, speed) {
if (speed) {
animating = true;
element.style.webkitTransition = `-webkit-transform ${speed}ms ease-in-out`;
setTimeout(function() {
element.style.webkitTransform = `translate(${offset}px, 0) translateZ(0)`;
}, 50);
} else {
element.style.webkitTransition = '';
element.style.webkitTransform = `translate(${offset}px, 0) translateZ(0)`;
}
}
function doOnTouchStart(event) {
var touch = event.touches[0];
dragState.startTime = new Date();
dragState.startLeft = touch.pageX;
dragState.startTop = touch.pageY;
swiping = true;
}
function doOnTouchMove(event) {
var touch = event.touches[0];
dragState.currentLeft = touch.pageX;
dragState.currentTop = touch.pageY;
var offsetLeft = dragState.currentLeft - dragState.startLeft;
var offsetTop = dragState.currentTop - dragState.startTop;
var distanceX = Math.abs(offsetLeft);
var distanceY = Math.abs(offsetTop);
if (distanceX < 5 || ( distanceY >= 5 && distanceY >= 1.73 * distanceX )) {
userScrolling = true;
return;
} else {
userScrolling = false;
event.preventDefault();
}
if (offsetLeft > 0) {
offsetLeft = offsetLeft - btnGroupWidth;
if (!opened) {
return;
}
} else {
if (opened) {
return;
}
}
offsetLeft = Math.min(Math.max(-btnGroupWidth, offsetLeft), 0);
translate(el, offsetLeft);
}
function doOnTouchEnd() {
var dragDuration = new Date() - dragState.startTime;
var offsetLeft = dragState.currentLeft - dragState.startLeft;
var offsetTop = dragState.currentTop - dragState.startTop;
if (dragDuration < 300) {
var fireTap = Math.abs(offsetLeft) < 5 && Math.abs(offsetTop < 5);
if (isNaN(offsetLeft) || isNaN(offsetTop)) {
fireTap = true;
}
if (fireTap) {
translate(el, 0, 150);
opened = false;
swiping = false;
return;
}
}
var distanceX = Math.abs(offsetLeft);
if (distanceX > btnGroupWidth * 0.4 && offsetLeft < 0) {
translate(el, -btnGroupWidth, 150);
opened = true;
} else {
translate(el, 0, 150);
opened = false;
}
dragState = {};
}
function mounted() {
getBtnGroupWidth();
el.addEventListener('touchstart', function(event) {
if (prevent) {
event.preventDefault();
}
if (stopPropagation) {
event.stopPropagation();
}
if (swiping) {
return;
}
doOnTouchStart(event);
});
el.addEventListener('touchmove', function(event) {
if (!swiping) {
return;
}
doOnTouchMove(event);
});
el.addEventListener('touchend', function(event) {
if (userScrolling) {
swiping = false;
dragState = {};
}
doOnTouchEnd();
swiping = false;
});
}
document.addEventListener('DOMContentLoaded', mounted);
</script>
</body>
</html>