Skip to content

Commit 4cccc75

Browse files
committed
SLIDER
1 parent e301f63 commit 4cccc75

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

exercises/57 - Slider/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</div>
5353

5454
</div>
55-
<script src="./src/index.js"></script>
55+
<script src="./src/index-prototype.js"></script>
5656
</body>
5757

5858
</html>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function Slider(slider) {
2+
if (!(slider instanceof Element)) {
3+
throw new Error('No slider passed in');
4+
}
5+
6+
// select the elements needed for the slider
7+
this.slides = slider.querySelector('.slides');
8+
this.slider = slider;
9+
const prevButton = slider.querySelector('.goToPrev');
10+
const nextButton = slider.querySelector('.goToNext');
11+
12+
// when this slider is created, run the start slider function
13+
this.startSlider();
14+
this.applyClasses();
15+
16+
// Event listeners
17+
prevButton.addEventListener('click', () => this.move('back'));
18+
nextButton.addEventListener('click', () => this.move());
19+
}
20+
21+
Slider.prototype.startSlider = function() {
22+
this.current =
23+
this.slider.querySelector('.current') || this.slides.firstElementChild;
24+
this.prev =
25+
this.current.previousElementSibling || this.slides.lastElementChild;
26+
this.next = this.current.nextElementSibling || this.slides.firstElementChild;
27+
};
28+
29+
Slider.prototype.applyClasses = function() {
30+
this.current.classList.add('current');
31+
this.prev.classList.add('prev');
32+
this.next.classList.add('next');
33+
};
34+
35+
Slider.prototype.move = function(direction) {
36+
// first strip all the classes off the current slides
37+
const classesToRemove = ['prev', 'current', 'next'];
38+
this.prev.classList.remove(...classesToRemove);
39+
this.current.classList.remove(...classesToRemove);
40+
this.next.classList.remove(...classesToRemove);
41+
if (direction === 'back') {
42+
// make an new array of the new values, and destructure them over and into the prev, current and next variables
43+
[this.prev, this.current, this.next] = [
44+
// get the prev slide, if there is none, get the last slide from the entire slider for wrapping
45+
this.prev.previousElementSibling || this.slides.lastElementChild,
46+
this.prev,
47+
this.current,
48+
];
49+
} else {
50+
[this.prev, this.current, this.next] = [
51+
this.current,
52+
this.next,
53+
// get the next slide, or if it's at the end, loop around and grab the first slide
54+
this.next.nextElementSibling || this.slides.firstElementChild,
55+
];
56+
}
57+
58+
this.applyClasses();
59+
};
60+
61+
const mySlider = new Slider(document.querySelector('.slider'));
62+
const dogSlider = new Slider(document.querySelector('.dog-slider'));
63+
64+
console.log(mySlider, dogSlider);
65+
66+
window.dogSlider = dogSlider;
67+
68+
window.addEventListener('keyup', function(e) {
69+
if (e.key === 'ArrowRight') {
70+
dogSlider.move();
71+
}
72+
if (e.key === 'ArrowLeft') {
73+
dogSlider.move('back');
74+
}
75+
});

exercises/57 - Slider/src/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function Slider(slider) {
2+
if (!(slider instanceof Element)) {
3+
throw new Error('No slider passed in');
4+
}
5+
// create some variables for working iwth the slider
6+
let prev;
7+
let current;
8+
let next;
9+
// select the elements needed for the slider
10+
const slides = slider.querySelector('.slides');
11+
const prevButton = slider.querySelector('.goToPrev');
12+
const nextButton = slider.querySelector('.goToNext');
13+
14+
function startSlider() {
15+
current = slider.querySelector('.current') || slides.firstElementChild;
16+
prev = current.previousElementSibling || slides.lastElementChild;
17+
next = current.nextElementSibling || slides.firstElementChild;
18+
console.log({ current, prev, next });
19+
}
20+
21+
function applyClasses() {
22+
current.classList.add('current');
23+
prev.classList.add('prev');
24+
next.classList.add('next');
25+
}
26+
27+
function move(direction) {
28+
// first strip all the classes off the current slides
29+
const classesToRemove = ['prev', 'current', 'next'];
30+
prev.classList.remove(...classesToRemove);
31+
current.classList.remove(...classesToRemove);
32+
next.classList.remove(...classesToRemove);
33+
if (direction === 'back') {
34+
// make an new array of the new values, and destructure them over and into the prev, current and next variables
35+
[prev, current, next] = [
36+
// get the prev slide, if there is none, get the last slide from the entire slider for wrapping
37+
prev.previousElementSibling || slides.lastElementChild,
38+
prev,
39+
current,
40+
];
41+
} else {
42+
[prev, current, next] = [
43+
current,
44+
next,
45+
// get the next slide, or if it's at the end, loop around and grab the first slide
46+
next.nextElementSibling || slides.firstElementChild,
47+
];
48+
}
49+
50+
applyClasses();
51+
}
52+
53+
// when this slider is created, run the start slider function
54+
startSlider();
55+
applyClasses();
56+
57+
// Event listeners
58+
prevButton.addEventListener('click', () => move('back'));
59+
nextButton.addEventListener('click', move);
60+
}
61+
62+
const mySlider = Slider(document.querySelector('.slider'));
63+
const dogSlider = Slider(document.querySelector('.dog-slider'));

0 commit comments

Comments
 (0)