-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (58 loc) · 2.19 KB
/
script.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
(() => {
"use strict";
const previous = document.querySelector(".previous");
const picture = document.querySelector("picture");
const next = document.querySelector(".next");
const body = document.querySelector("body");
const imgs = document.querySelectorAll("img");
const images = new Object(null);
images[Symbol.iterator] = function* () {
yield "https://www.kolpaper.com/wp-content/uploads/2020/03/doom-wallpaper.jpg",
yield "https://images3.alphacoders.com/686/686773.jpg";
yield "https://hdqwalls.com/wallpapers/doom-eternal-artwork-yd.jpg";
};
imgs.forEach((element, index) => {
element.src = [...images][index];
});
body.style.cssText = `
background-image: url(${[...images][1]})
`;
const duration = 500;
const amount = duration;
const delay = () => new Promise((resolve) => setTimeout(resolve, amount));
const addSlideAnimation = (element, from, to) => {
const keyframes = [
{ transform: `translateX(${from})` },
{ transform: `translateX(${to})` },
];
const options = {
duration,
};
element.animate(keyframes, options);
};
const insertAfter = (newNode, referenceNode) => {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
};
const previousImage = () => {
const imgs = document.querySelectorAll("img");
previous.removeEventListener("click", previousImage, false);
body.style.cssText = `
background-image: url(${imgs[0].src})
`;
imgs.forEach((element) => addSlideAnimation(element, "-680px", "10px"));
picture.insertBefore(imgs[imgs.length - 1], imgs[0]);
delay().then(() => previous.addEventListener("click", previousImage));
};
const nextImage = () => {
const imgs = document.querySelectorAll("img");
next.removeEventListener("click", nextImage, false);
body.style.cssText = `
background-image: url(${imgs[2].src})
`;
imgs.forEach((element) => addSlideAnimation(element, "680px", "-10px"));
insertAfter(imgs[0], imgs[imgs.length - 1]);
delay().then(() => next.addEventListener("click", nextImage));
};
previous.addEventListener("click", previousImage);
next.addEventListener("click", nextImage);
})();