-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (88 loc) · 2.93 KB
/
index.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
var timer = window.setInterval(nextImg, 5000);
const nextButton = document.getElementById('next');
nextButton.addEventListener('click', () => {
nextImg();
restartTime();
});
const backButton = document.getElementById('back');
backButton.addEventListener('click', () => {
previousImg();
restartTime();
});
const controlPositions = document.getElementById('control-positions');
controlPositions.addEventListener('click', (event) => {
if (event.target.id != 'control-positions') {
const controlItems = Array.from(controlPositions.querySelectorAll('*'));
controlPosition(controlItems, event);
restartTime();
}
});
function nextImg() {
const img = getImage();
const nextImg = img.id + 1;
if (nextImg < 5) {
img.element.style.backgroundImage = `url(img/${nextImg}.jpg)`;
}
else {
img.element.style.backgroundImage = `url(img/0.jpg)`
}
changeControlPosition(nextImg, nextImg - 1);
}
function previousImg() {
const img = getImage();
const previousImg = img.id - 1;
if (previousImg >= 0) {
img.element.style.backgroundImage = `url(img/${previousImg}.jpg)`;
}
else {
img.element.style.backgroundImage = `url(img/4.jpg)`
}
changeControlPosition(previousImg, previousImg + 1);
}
function controlPosition(array, event) {
array.forEach(element => {
element.classList.remove('current')
});
const itemTarget = event.target.id;
const itemFound = array.find(item => item.id == itemTarget);
itemFound.classList.add('current');
const nextImg = array.indexOf(itemFound);
const img = getImage();
img.element.style.backgroundImage = `url(img/${nextImg}.jpg)`;
}
function getImage() {
const imgElement = document.getElementById("img");
const style = window.getComputedStyle(img, false);
const backgroundImg = style.backgroundImage;
const imgIndex = getImageIndex(backgroundImg);
return image = {
element: imgElement,
id: imgIndex
}
}
function getImageIndex(url) {
const imgPosition = url.search('[0-9].jpg') || url.search('[0-9].png');
const imgNumberString = url.charAt(imgPosition);
const imgIndex = parseInt(imgNumberString, 10);
return imgIndex;
}
function changeControlPosition(currentPosition, previousPosition) {
const controlPositionsElement = document.getElementById('control-positions');
const controlChildrenList = controlPositionsElement.querySelectorAll("*");
const lastPosition = 4;
const firstPosition = 0;
const controlPositions = {
current: controlChildrenList[lastPosition],
previous: controlChildrenList[firstPosition]
};
if (currentPosition >= firstPosition) {
controlPositions.current = controlChildrenList[currentPosition] || controlChildrenList[firstPosition];
controlPositions.previous = controlChildrenList[previousPosition]
}
controlPositions.current.classList.add('current');
controlPositions.previous.classList.remove('current');
}
function restartTime() {
clearInterval(timer);
timer = window.setInterval(nextImg, 5000);
}