-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom-stacked-images-with-web-animation-API-and-progressive-JS.html
100 lines (91 loc) · 2.51 KB
/
random-stacked-images-with-web-animation-API-and-progressive-JS.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Random Stacked Images With Web Animation API and Progressive JS</title>
<link rel="stylesheet" href="css/random-stacked-images-with-web-animation-API-and-progressive-JS.css">
</head>
<body>
<div class="shuffle reveal">
<img src="images/calgary-bridge-2013.jpg" alt="">
<img src="images/photoshop-face-before.jpg" alt="">
<img src="images/emma-bottom-flipped.jpg" alt="">
<img src="images/butterfly-on-petal.jpg" alt="">
<img src="images/lake-tekapo.jpg" alt="">
<img src="images/blue-butterfly.jpg" alt="">
</div>
</body>
<script>
var reveal = document.querySelector('.reveal');
reveal.classList.remove('reveal');
var revealedImages = document.querySelectorAll('img'),
i = 1,
zMax = 0;
function getRandom (min, max) {
return Math.random() * (max - min) + min;
}
Array.prototype.forEach.call(revealedImages, function (photo) {
setTimeout(function () {
photo.style.position = "absolute";
photo.style.width = getRandom(33,45)+"%";
photo.style.left = getRandom(-5,65)+"%";
photo.style.top = getRandom(-6,60)+"vh";
photo.classList.add("expose");
var animate = photo.animate(
[
{
opacity: '0',
transform: 'rotate('+getRandom(-12,12)+'deg) scale(1.2)',
boxShadow: '0 0 12px 12px rgba(0, 0, 0, .3)'
},
{
opacity: '1',
transform: 'rotate('+getRandom(-8,8)+'deg)',
boxShadow: '0 0 6px 6px rgba(0, 0, 0, .3)'
}
], {
duration: 2000,
fill: 'forwards'
}
);
}, 1800*i)
i++;
dragElement(photo);
});
function dragElement (el) {
var pos1 = 0,
pos2 = 0,
pos3 = 0,
pos4 = 0;
el.onmousedown = dragMouseDown;
function dragMouseDown (e) {
e = e || window.event;
e.preventDefault();
//get the mouse cursor position at startup
pos3 = e.clientX;
pos4 = e.clientY;
if(zMax == 0 || zMax > this.style.zIndex) this.style.zIndex = ++zMax;
document.onmouseup = closeDragElement;
document.onmousemove = elementDrag;
}
function elementDrag (e) {
e = e || window.event;
e.preventDefault();
//calc new cursor position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
//set new position
el.style.top = (el.offsetTop - pos2) + "px";
el.style.left = (el.offsetLeft - pos1) + "px";
}
function closeDragElement () {
//stop moving when mouse button is released
document.onmouseup = null;
document.onmousemove = null;
}
}
</script>
</html>