-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
73 lines (67 loc) · 2.97 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>IntersectionObserver - Lazy load</title>
<link rel="shortcut icon" href="http://bit.ly/ghfavicon" width=32px>
<title>IntersectionObserver: lazy load images | @rpsthecoder, Github Pages</title>
<style>
@import url('https://fonts.googleapis.com/css?family=Averia+Serif+Libre');
body{
font-family: 'Averia serif libre';
text-align: center;
color: aliceblue; background-color: crimson;
}
img{
width: 600px; height: 420px;
display: inline-block;
margin: 50px;
box-shadow: 0 0 20px #333;
}
h1{
margin-bottom: 0; text-transform: uppercase; font-size: 14pt;
}
#mark{
width: 100%;
position: fixed; bottom: 200px;
font-size: 16pt;
border-bottom: dashed 2px currentColor;
text-shadow: 0 0 10px black;
}
#warning{
padding: 10px;
color: beige; background-color: red;
}
a{ color: skyblue; }
@media(max-width:620px){
img{ width: 90vw; height: 55vw; margin: 50px 0; }
}
</style>
</head>
<body>
<strong id="warning" hidden>IntersectionObserver is not supported by your browser, so you can't see the effect. Check its browser support <a href="https://caniuse.com/#feat=intersectionobserver" target="_blank">here</a>.</strong>
<noscript>⚠️ JAVASCRIPT IS DIABLED IN YOUR BROWSER!</noscript>
<h1>Scroll down and see images lazy load at the mark</h1>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" data-src=https://images.unsplash.com/photo-1504272017915-32d1bd315a59?fit=crop&w=600&q=80><br>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" data-src=https://images.unsplash.com/photo-1502716643504-c4ea7b357d91?fit=crop&w=600&q=80><br>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" data-src=https://images.unsplash.com/photo-1502716716838-6ad177344906?fit=crop&w=600&q=80><br>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" data-src=https://images.unsplash.com/photo-1504271933050-2cf260bbec95?fit=crop&w=600&q=80><br>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/PlaceholderLC.png/600px-PlaceholderLC.png" data-src=https://images.unsplash.com/photo-1502716197620-bf14ce1651b3?fit=crop&w=600&q=80><br>
<div id='mark'>200px above viewport</div>
<script>
if(!!window.IntersectionObserver){
let observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if(entry.isIntersecting){
entry.target.src = entry.target.dataset.src;
observer.unobserve(entry.target);
}
});
}, {rootMargin: "0px 0px -200px 0px"});
document.querySelectorAll('img').forEach(img => { observer.observe(img) });
}
else document.querySelector('#warning').style.display = 'block';
</script>
</body>
</html>