-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
371 lines (339 loc) · 10.7 KB
/
index.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<html>
<head>
<title>秀就完事</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<style>
@keyframes fade-small-larger {
from{
opacity: 0;
transform: scale(1, 1);
}
to{
opacity: 1;
transform: scale(1.5, 1.5);
}
}
@keyframes fade-zero-origin {
from{
opacity: 0;
transform: scale(0, 0);
}
to{
opacity: 1;
transform: scale(1, 1);
}
}
* {
box-sizing: border-box;
}
body, h1, h2, h3, h4, h5, h6 {
margin: 0;
padding: 0;
}
img {
width: 100%;
border-radius: 15px;
}
img:not(:first-child):not(:last-child) {
margin: 25px 0;
}
img[loaded=true] {
cursor: pointer;
cursor: zoom-in;
}
article>h2 {
margin: 10px;
text-align: center;
background: transparent;
}
.lazy-Load {
height: 210px;
background-color: #f0f2f4;
}
.dropDown {
margin-top: -100px;
}
.refresh-box {
height: 100px;
line-height: 100px;
}
.overlay {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: #292929;
z-index: 1000;
opacity: 1;
/**
* 通知浏览器优化渲染
*/
will-change: opacity;
}
.overlay>img {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: auto;
animation: fade-zero-origin .3s both;
-webkit-animation: fade-zero-origin .3s both;
}
.slide-box {
display: none;
}
.slide-box h2 {
height: 100%;
/**
* 文字垂直显示
*/
-webkit-writing-mode: vertical-rl;
-ms-writing-mode: bt-rl;
writing-mode: vertical-rl;
text-align: center;
}
.slide-box--left {
position: fixed;
left: 30px;
top: 0;
}
.slide-box--right {
position: fixed;
right: 30px;
top: 0;
}
@media screen and (min-width: 760px) {
.content-box {
width: 640px;
margin: 0 auto;
}
.slide-box {
display: block;
}
.lazy-Load {
height: 320px;
transition: transform 0.3s;
}
.overlay>img {
animation: fade-small-larger .3s both;
-webkit-animation: fade-small-larger .3s both;
}
.content-box .lazy-Load:hover {
transform: scale(1.05, 1.05);
}
}
</style>
</head>
<body>
<section class="dropDown">
<div class="refresh-box">
<h4 style="text-align: center;">下拉刷新</h4>
</div>
<div class="content-box">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000000" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000001" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000002" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000003" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000004" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000005" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000006" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000007" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000008" class="lazy-Load">
<img data-src="https://api.ixiaowai.cn/api/api.php?1000009" class="lazy-Load">
</div>
</section>
<section class="slide-box">
<h2 class="slide-box--left">图片懒加载</h2>
<h2 class="slide-box--right">移动端下拉刷新</h2>
</section>
<script>
/**
* 获取元素距离页面可视区域的距离
* @param {[Element]} obj [元素节点对象]
*/
function getPos(obj){
var l = 0
, t = 0;
while(obj) {
l += obj.offsetLeft;
t += obj.offsetTop;
obj = obj.offsetParent;
}
return {
left: l,
top: t
}
}
/**
* 图片懒加载
*/
(function() {
// 所有需要懒加载的图片
var lazyImgs = document.querySelectorAll('img.lazy-Load');
window.onload = function() {
window.onscroll = _throttle(lazyLoad, 50);
}
function _throttle(fn, wait) {
var time;
// 立即执行一次(此处写的太随意,可改进)
cb();
return cb;
function cb() {
var self = this;
var args = arguments;
if(!time) {
time = setTimeout(function() {
time = null;
fn.apply(self, args);
}, wait || 50);
}
}
}
function lazyLoad() {
// 当前滚动条的偏移量
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
// 视口高度
var viewportSize = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var w = document.querySelector('.content-box').clientWidth;
Array.prototype.forEach.call(lazyImgs, function(img) {
var offset = scrollTop + viewportSize - getPos(img).top;
if(offset > 0 && !img.loaded) {
// 资源预加载,这样显示的时候就不会从上到下绘制而是整个绘制出来
var source = new Image();
source.src = img.getAttribute('data-src');
source.onload = function() {
var ratio = w / source.width;
img.style.height = source.height * ratio;
img.src = source.src;
img.setAttribute('loaded', true);
}
}
});
}
})();
/**
* 下拉刷新
*/
(function() {
// 获取需要下拉刷新的容器对象
var dropEle = document.querySelector('.dropDown');
var content = document.querySelector('.dropDown .content-box');
var fresh = document.querySelector('.dropDown .refresh-box h4');
var y = null;
var w = content.clientWidth;
var maxOffset = 100;
// 视口高度
var viewportSize = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
window.addEventListener('resize', function() {
w = content.clientWidth;
viewportSize = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
});
// 绑定事件
dropEle.addEventListener('touchstart', _start);
dropEle.addEventListener('touchmove', _move);
dropEle.addEventListener('touchend', _end);
function _start(e) {
// 记录最开始点击的点坐标,如果没处于首屏就不记录
y = e.changedTouches[0].pageY <= viewportSize ? e.changedTouches[0].pageY : null;
}
function _move(e) {
if(!y && typeof y === 'object') return;
var touch = e.changedTouches[0];
var move = touch.pageY - y;
if(move > 0) {
if(move > maxOffset) {
fresh.innerHTML = '松手刷新';
} else {
dropEle.style.marginTop = move - maxOffset;
}
}
}
function _end(e) {
if(!y && typeof y === 'object') return;
var touch = e.changedTouches[0];
var move = touch.pageY - y;
if(move > maxOffset) {
fresh.innerHTML = '正在刷新...';
/**
* 刷新回调
*/
setTimeout(function() {
var source = new Image();
source.src = 'https://api.ixiaowai.cn/api/api.php?' + +new Date();
content.insertBefore(source, content.firstChild);
source.onload = function() {
// 防止内存泄露
source.onerror = source.onload = null;
var ratio = w / source.width;
source.style.height = source.height * ratio;
_reset();
}
source.onerror = function() {
// 防止内存泄露
source.onerror = source.onload = null;
_reset();
}
}.bind(this), 0);
} else {
_reset();
}
y = null;
}
/**
* 将 refresh-box 还原到最开始的位置
*/
function _reset() {
window.requestAnimationFrame(step);
function step() {
var move = dropEle.style.marginTop.split('px')[0];
dropEle.style.marginTop = move - 3;
if(move > maxOffset * -1) {
window.requestAnimationFrame(step);
} else {
dropEle.style.marginTop = maxOffset * -1;
}
}
}
})();
// 点击放大(消除点击穿透)
// todo: 点击超出可视范围的不会居中
(function() {
var translateEle = document.querySelector('.content-box');
// 创建遮罩层节点
var mask = document.createElement('div');
mask.classList.add('overlay');
// 委托点击事件
translateEle.addEventListener('click', function(e) {
var target = e.target;
var parent = target.parentNode;
var next = parent.children[Array.prototype.indexOf.call(parent.children, target) + 1];
// 适口宽度
var width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
// 视口高度
var height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
if(target.nodeName === 'IMG') {
mask.appendChild(target);
mask.style = 'width:' + width + 'px; height: ' + height + 'px; left: 0; top: 0;';
function clear() {
parent.insertBefore(target, next);
target.removeEventListener('click', clear, true);
mask.removeEventListener('click', clear, true);
setTimeout(function() {
mask.parentNode && mask.parentNode.removeChild(mask);
}, 0);
};
document.body.appendChild(mask);
target.addEventListener('click', clear, true);
// 绑定遮罩层点击事件从而点击还原
mask.addEventListener('click', clear, true);
}
}, false);
})()
</script>
</body>
</html>