-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharx_slider.js
More file actions
100 lines (87 loc) · 2.88 KB
/
arx_slider.js
File metadata and controls
100 lines (87 loc) · 2.88 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
if(document.querySelectorAll(".slider").length == 1) {
const timerlength = 7000 // Change it if you want to change the autoslide speed (in miliseconds, 10000 means 10 seconds)
const slider = document.querySelector(".slider");
const sliderImg = slider.querySelectorAll("img");
const sliderPreviusButton = slider.querySelector(".left")
const sliderNextButton = slider.querySelector(".right")
const slider_dots_holder = slider.querySelector(".slider_dots_holder")
const sliderCount = slider.querySelectorAll("img").length - 1
const imgHolder = slider.querySelector(".imgholder")
let currentSlider = 0;
let dotsnum = sliderCount + 1;
// CreateDots function creates clickable dots & caption based on the number of images and alt texts
for ( ;dotsnum != 0;dotsnum--) {
const realId = sliderCount-(dotsnum-1)
const newDot = document.createElement("div");
const newCaption = document.createElement("div");
newDot.classList.add("slider_dots");
newDot.setAttribute("id", realId);
newDot.setAttribute("onclick", ("sliderJump" + "(" + realId + ")") )
newCaption.classList.add("slider_alts");
newCaption.innerHTML = sliderImg[realId].alt;
slider_dots_holder.appendChild(newDot);
imgHolder.appendChild(newCaption);
}
// Below codes are for Next and previous butttons
const sliderdots = slider.querySelectorAll(".slider_dots")
const slideralts = slider.querySelectorAll(".slider_alts")
// Functions
// Auto Play Codes (autoplay is Optional. you can remove the below code if you do not want Autoplay)
function slideTimer() {
if (currentSlider < sliderCount) {
slidNext ()
window.setTimeout (slideTimer, timerlength)
} else {
sliderJump(0)
window.setTimeout (slideTimer, timerlength)
}
}
function sliderShow() {
sliderImg[currentSlider].style.opacity = "1";
sliderdots[currentSlider].style.opacity = "1";
slideralts[currentSlider].style.opacity = "0.8";
}
function sliderHide() {
sliderImg[currentSlider].style.opacity = "0";
sliderdots[currentSlider].style.opacity = "0.5";
slideralts[currentSlider].style.opacity = "0";
}
function slidNext() {
if (currentSlider < sliderCount) {
sliderHide();
currentSlider++;
sliderShow();
} else {
sliderHide();
currentSlider = 0;
sliderShow();
}
}
function slidePrevius() {
if (currentSlider > 0) {
sliderHide();
currentSlider --;
sliderShow();
} else {
sliderHide();
currentSlider = sliderCount;
sliderShow();
}
}
function sliderJump(number) {
sliderHide();
currentSlider = number;
sliderShow();
resetTimer();
}
function resetTimer() {
}
// Init
if(sliderCount !== -1) {
sliderPreviusButton.addEventListener("click", slidePrevius);
sliderNextButton.addEventListener("click", slidNext);
sliderShow()
// Events
window.setTimeout(slideTimer, timerlength);
}
}