-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
85 lines (75 loc) · 1.95 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
83
84
const slidesContainer = document.querySelector('.slides');
const btnRight = document.querySelector('.btn-right');
const btnLeft = document.querySelector('.btn-left');
let slides = [];
let currentPosition = 0;
const DATA = [
{
name: 'deer',
image: './assets/images/latest-articles/deer.jpg',
text: 'Naturalist investigation'
},
{
name: 'squirrel',
image: './assets/images/latest-articles/squirrel.jpg',
text: 'Kamikaze squirrels'
},
{
name: 'bird',
image: './assets/images/latest-articles/bird.jpg',
text: 'Birds Fight club'
},
{
name: 'ladybug',
image: './assets/images/latest-articles/ladybug.jpg',
text: 'Nice red white dotted Bug'
},
{
name: 'mushroom',
image: './assets/images/latest-articles/mushroom.jpg',
text: 'Not little mushroom'
},
{
name: 'wolf',
image: './assets/images/latest-articles/wolf.jpg',
text: 'Woooooooooooolf'
}
]
const createSlide = (parent, content, data, index) => {
const el = document.createElement('div');
el.className = 'slide';
el.style.backgroundImage = `url(${data[index].image})`;
el.innerHTML = content;
parent.appendChild(el);
return el;
}
const createHTML = (data, index) => {
return (
` <div class="slide__inner">
<h4 class="slide__inner__caption">${data[index].name}</h4>
<p class="slide__inner__text">${data[index].text}</p>
</div>
`
);
}
for (let i = 0; i < 6; i++) {
slides.push(createSlide(slidesContainer, createHTML(DATA, i), DATA, i));
}
btnLeft.addEventListener('click', () => {
setPosition(currentPosition + 100);
});
btnRight.addEventListener('click', () => {
setPosition(currentPosition - 100);
});
const setPosition = (position) => {
if (position > 0) {
return false;
}
if (position < - (slides.length - 3) * 100) {
return false;
}
currentPosition = position;
slides.forEach((slide) => {
slide.style.transform = `translateX(${currentPosition}%)`;
});
};