|
| 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 | +}); |
0 commit comments