-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
134 lines (116 loc) · 3.99 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
// User defined data
data = [
{
id: 1,
img: '1.png',
}
,
{
id: 2,
img: '2.jpg',
}
,
{
id: 3,
img: '3.png',
}
,
{
id: 4,
img: '4.jpg',
},
{
id: 5,
img: '5.jpg',
}
]
let sliderSettings = {
slidePosition: 0,
slideWidth: 800,
sliderHeight: 700,
slideAmount: 800
}
// Select elements
let slides = document.querySelector('.slides');
let slider = document.querySelector('#slider');
let sliderDots = document.querySelector('.slider-dots');
// Set styles
slides.style.width = `${data.length * sliderSettings.slideWidth}px`;
slider.style.width = `${sliderSettings.slideWidth}px`;
slider.style.height = `${sliderSettings.sliderHeight}px`;
// Generate slides
function generateSlides(data) {
slides.innerHTML = '';
sliderDots.innerHTML = '';
for (let i = 0; i < data.length; i++) {
let dot = `<span data="${i}"></span>`;
let template = `
<div class="slide" style='width:${sliderSettings.slideAmount}px;' data="${i + 1}">
<img src="${data[i].img}" alt="">
</div>
`
slides.innerHTML += template;
sliderDots.innerHTML += dot;
}
}
//Task 2 - control dots
document.addEventListener('DOMContentLoaded', function () {
console.log(this.scrollLeft)
let slidersDots = document.querySelectorAll('.slider-dots span');
slidersDots.forEach(sliderDot => {
let dotAccess = sliderDot.getAttribute('data')
sliderDot.addEventListener('click', function () {
// console.log('Element clicked!', dotAccess);
if (dotAccess) {
slides.style.transform = `translateX(-${dotAccess * sliderSettings.slideAmount}px)`;
// Reset background color for all dots
slidersDots.forEach(dot => {
dot.style.background = 'rgba(206, 204, 204, 0.144)';
});
// Set background color for the clicked dot
sliderDot.style.background = 'rgb(180, 179, 179)';
}
});
});
});
// Slide Left
function slideLeft(e) {
e.preventDefault();
if (sliderSettings.slidePosition <= 0) {
sliderSettings.slidePosition = data.length * sliderSettings.slideAmount;
}
sliderSettings.slidePosition -= sliderSettings.slideAmount;
slides.style.transform = `translateX(-${sliderSettings.slidePosition}px)`;
///console.log(sliderSettings.slidePosition/sliderSettings.slideAmount)
//Slide dots change during left click
let slidersDots = document.querySelectorAll('.slider-dots span');
slidersDots.forEach(sliderDot => {
let dotAccess = sliderDot.getAttribute('data')
sliderDot.style.background = 'rgba(206, 204, 204, 0.144)'
if (sliderSettings.slidePosition / sliderSettings.slideAmount == dotAccess) {
sliderDot.style.background = 'rgb(180, 179, 179)'
}
});
}
// Slide Right
function slideRight(e) {
e.preventDefault();
if (sliderSettings.slidePosition == data.length * sliderSettings.slideAmount) {
sliderSettings.slidePosition = 0;
}
slides.style.transform = `translateX(-${sliderSettings.slidePosition}px)`;
sliderSettings.slidePosition += sliderSettings.slideAmount;
// console.log(sliderSettings.slidePosition/sliderSettings.slideAmount)
//Slide dots change during right click
let slidersDots = document.querySelectorAll('.slider-dots span');
slidersDots.forEach(sliderDot => {
let dotAccess = sliderDot.getAttribute('data');
//sliderDot.style.background = 'blue'
sliderDot.style.background = 'rgba(206, 204, 204, 0.144)'
if (sliderSettings.slidePosition / sliderSettings.slideAmount - 1 == dotAccess) {
sliderDot.style.background = 'rgb(180, 179, 179)'
}
});
}
// Start app
generateSlides(data);