Advertisement
ReutenkoIvan

Sample slider

Oct 2nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const sliderNode = document.createElement('div')
  2. const SLICE_CLASSES = ['first-slide', 'second-slide']
  3. const slider = {realIndex: 2}
  4.  
  5. const switchSlide = (e) => {
  6.    const currentIndex = slider.realIndex;
  7.    const displacement = e.id === 'next' ? 1 : -1;
  8.    const slidesCnt = SLICE_CLASSES.lemgth - 1;
  9.    const indexSum = currentIndex + displacement;
  10.  
  11.    slider.realIndex = indexSum < slidesCnt
  12.       ? (indexSum < 0 && slidesCnt) || indexSum
  13.       : 0
  14.  
  15.    sliderNode.classList.remove(SLICE_CLASSES[currentIndex])
  16.    sliderNode.classList.add(SLICE_CLASSES[slider.realIndex])
  17. }
  18.  
  19. // нужно навесить на кнопки переключения слайдов id=next и id=previous
  20. // next.addEventListener('click', switchSlide)
  21. // previous.addEventListener('click', switchSlide)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement