-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
79 lines (73 loc) · 2.52 KB
/
Copy pathmain.js
File metadata and controls
79 lines (73 loc) · 2.52 KB
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
const mySlides = document.querySelectorAll('.banner-container');
const sliderContainer = document.querySelector('.slider-container');
const slideTimeout = 3000;
let sliderInterval;
let currentIndex = 0;
let isRunning = true;
let tabButtons;
// let changedTime = 0 -> zastanowić się nad wdrożeniem mechanizmu, czekającego taką ilość czasu na zmienienie slide-u, jaka pozostała od momentu jego dezaktywacji
(() => {
mySlides.forEach((slide, index) => {
if(index != currentIndex)
{
slide.style.opacity = '0';
slide.style.visibility = 'hidden';
}
});
initializeSlider();
sliderContainer.addEventListener('mouseenter', () => disableSlider());
sliderContainer.addEventListener('mouseleave', () => enableSlider());
window.addEventListener('blur', () => disableSlider());
window.addEventListener('focus', () => enableSlider());
initializeButtons();
changeButtonFocus(0);
})();
function initializeSlider(){
if(!sliderInterval)
{
sliderInterval = setInterval(() => {
if(isRunning)
{
let previousIndex = currentIndex;
currentIndex = (currentIndex == mySlides.length - 1) ?
0 :
currentIndex + 1;
changeSlide(previousIndex);
}
}, slideTimeout);
}
}
function enableSlider(){
isRunning = true;
initializeSlider();
// return console.log('Aktywowano slider');
}
function disableSlider(){
isRunning = false;
clearInterval(sliderInterval);
sliderInterval = null;
// return console.log('Dezaktywowano slider');
}
function changeTab(tabNumber){
if(!Number.isInteger(tabNumber) || tabNumber < 0 || tabNumber > (mySlides.length - 1))
throw new Error('Wprowadzono nieprawidłowy index slide-u!');
disableSlider();
changeSlide(currentIndex, tabNumber);
setTimeout(() => {
enableSlider()
}, slideTimeout / 6);
}
function changeSlide(previousIndex, actualIndex){
if(previousIndex != actualIndex)
{
currentIndex = Number.isInteger(actualIndex) ?
actualIndex :
currentIndex;
mySlides[previousIndex].style.opacity = '0';
mySlides[currentIndex].style.visibility = 'visible';
mySlides[currentIndex].style.opacity = '1';
setTimeout(() => {
mySlides[previousIndex].style.visibility = 'hidden';
}, slideTimeout / 6);
}
}