forked from bendc/gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
143 lines (117 loc) · 3.55 KB
/
app.js
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
import sources from "./images.js";
const {body} = document;
const toggle = element => {
element.classList.toggle("hidden");
return element;
};
const list = document.createElement("ul");
const scrollerID = "scroller";
const thumbnailsID = "thumbnails";
list.id = thumbnailsID;
const template = document.querySelector("template");
const images = sources.map((source, index) => {
const item = template.content.cloneNode(true);
const image = item.querySelector("img");
image.addEventListener("load", () => fade(image));
image.addEventListener("click", () => animate(index));
toggle(image).src = `images/${source}`;
list.append(item);
return image;
});
const timing = {
duration: 500,
easing: ".23, 1, .32, 1"
};
const fade = image => {
toggle(image).removeAttribute("loading");
image.animate({
transform: ["scale(.9)", "none"],
opacity: [0, 1],
},{
duration: timing.duration,
easing: `cubic-bezier(${timing.easing})`
});
};
const animate = async index => {
const {innerWidth, innerHeight} = window;
const direction = list.id == scrollerID ? -1 : 1;
const thumbnail = images[direction > 0
? index
: Math.round(list.scrollLeft / innerWidth)
];
const duplicate = thumbnail.cloneNode();
if (direction > 0)
toggle(duplicate);
await body.appendChild(duplicate).decode();
const {width, height} = duplicate.getBoundingClientRect();
const imageRatio = width / height;
const viewportRatio = innerWidth / innerHeight;
const limit = imageRatio > viewportRatio ? "width" : "height";
const heroSize = {
width: limit == "width" ? innerWidth : innerHeight * imageRatio,
height: limit == "height" ? innerHeight : innerWidth / imageRatio
};
Object.assign(duplicate.style, {
position: "fixed",
width: `${heroSize.width}px`,
height: `${heroSize.height}px`,
left: `${innerWidth / 2 - heroSize.width / 2}px`,
top: `${innerHeight / 2 - heroSize.height / 2}px`
});
if (direction < 0) {
toggle(list).id = thumbnailsID;
thumbnail.scrollIntoView({block: "center"});
if (list.scrollLeft) list.scrollLeft = 0;
}
const thumbnailBox = thumbnail.getBoundingClientRect();
const max = Math.max(...Object.values(heroSize));
const scale = thumbnailBox.width / max;
const middle = thumbnailBox.width / 2;
const thumbnailCenter = {
x: middle + thumbnailBox.left,
y: middle + thumbnailBox.top
};
const translate = {
x: thumbnailCenter.x - innerWidth / 2,
y: thumbnailCenter.y - innerHeight / 2
};
const animations = {
transform: [
`translate(${translate.x}px, ${translate.y}px) scale(${scale})`,
"none"
],
opacity: [1, 0]
};
if (direction < 0)
Object.values(animations).forEach(keyframes => keyframes.reverse());
const bounce = innerWidth < 450 ? ".08" : ".02";
requestAnimationFrame(() => {
toggle(thumbnail);
toggle(direction < 0 ? list : duplicate);
duplicate.animate({
transform: animations.transform
},{
duration: timing.duration,
easing: `cubic-bezier(${timing.easing}${bounce})`
});
list.animate({
opacity: animations.opacity
},{
duration: timing.duration,
easing: `cubic-bezier(${timing.easing})`
})
.addEventListener("finish", () => {
if (direction > 0) {
list.id = scrollerID;
list.scrollLeft = index * innerWidth;
}
toggle(thumbnail);
duplicate.remove();
},{once: true});
});
};
window.addEventListener("keyup", ({key}) => {
if (key != "Escape" || list.id != scrollerID) return;
animate();
});
body.append(list);