javascriptcssscrollfrontend

CSS Scroll Snap Points with navigation (next, previous) buttons


I am building a carousel, very minimalist, using CSS snap points. It is important for me to have CSS only options, but I'm fine with enhancing a bit with javascript (no framework).

I am trying to add previous and next buttons to scroll programmatically to the next or previous element. If javascript is disabled, buttons will be hidden and carousel still functionnal.

My issue is about how to trigger the scroll to the next snap point ?

All items have different size, and most solution I found require pixel value (like scrollBy used in the exemple). A scrollBy 40px works for page 2, but not for others since they are too big (size based on viewport).

function goPrecious() {
  document.getElementById('container').scrollBy({ 
    top: -40,
    behavior: 'smooth' 
  });
}

function goNext() {
  document.getElementById('container').scrollBy({ 
    top: 40,
    behavior: 'smooth' 
  });
}
#container {
  scroll-snap-type: y mandatory;
  overflow-y: scroll;

  border: 2px solid var(--gs0);
  border-radius: 8px;
  height: 60vh;
}

#container div {
  scroll-snap-align: start;

  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 4rem;
}
#container div:nth-child(1) {
  background: hotpink;
  color: white;
  height: 50vh;
}
#container div:nth-child(2) {
  background: azure;
  height: 40vh;
}
#container div:nth-child(3) {
  background: blanchedalmond;
  height: 60vh;
}
#container div:nth-child(4) {
  background: lightcoral;
  color: white;
  height: 40vh;
}
<div id="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>

<button onClick="goPrecious()">previous</button>
<button onClick="goNext()">next</button>


Solution

  • Nice question! I took this as a challenge.
    So, I increased JavaScript for it to work dynamically. Follow my detailed solution (in the end the complete code):

    First, add position: relative to the .container, because it need to be reference for scroll and height checkings inside .container.

    Then, let's create 3 global auxiliary variables:

    1) One to get items scroll positions (top and bottom) as arrays into an array. Example: [[0, 125], [125, 280], [280, 360]] (3 items in this case).
    3) One that stores half of .container height (it will be useful later).
    2) Another one to store the item index for scroll position

    var carouselPositions;
    var halfContainer;
    var currentItem;
    

    Now, a function called getCarouselPositions that creates the array with items positions (stored in carouselPositions) and calculates the half of .container (stored in halfContainer):

    function getCarouselPositions() {
      carouselPositions = [];
      document.querySelectorAll('#container div').forEach(function(div) {
        carouselPositions.push([div.offsetTop, div.offsetTop + div.offsetHeight]); // add to array the positions information
      })
      halfContainer = document.querySelector('#container').offsetHeight/2;
    }
    
    getCarouselPositions(); // call it once
    

    Let's replace the functions on buttons. Now, when you click on them, the same function will be called, but with "next" or "previous" argument:

    <button onClick="goCarousel('previous')">previous</button>
    <button onClick="goCarousel('next')">next</button>
    

    Here is about the goCarousel function itself:

    First, it creates 2 variables that store top scroll position and bottom scroll position of carousel.

    Then, there are 2 conditionals to see if the current carousel position is on most top or most bottom.
    If it's on top and clicked "next" button, it will go to the second item position. If it's on bottom and clicked "previous" button, it will go the previous one before the last item.

    If both conditionals failed, it means the current item is not the first or the last one. So, it checks to see what is the current position, calculating using the half of the container in a loop with the array of positions to see what item is showing. Then, it combines with "previous" or "next" checking to set the correct next position for currentItem variable.

    Finally, it goes to the correct position through scrollTo using currentItem new value.

    Below, the complete code:

    var carouselPositions;
    var halfContainer;
    var currentItem;
    
    function getCarouselPositions() {
      carouselPositions = [];
      document.querySelectorAll('#container div').forEach(function(div) {
        carouselPositions.push([div.offsetTop, div.offsetTop + div.offsetHeight]); // add to array the positions information
      })
      halfContainer = document.querySelector('#container').offsetHeight/2;
    }
    
    getCarouselPositions(); // call it once
    
    function goCarousel(direction) {
      
      var currentScrollTop = document.querySelector('#container').scrollTop;
      var currentScrollBottom = currentScrollTop + document.querySelector('#container').offsetHeight;
      
      if (currentScrollTop === 0 && direction === 'next') {
          currentItem = 1;
      } else if (currentScrollBottom === document.querySelector('#container').scrollHeight && direction === 'previous') {
          console.log('here')
          currentItem = carouselPositions.length - 2;
      } else {
          var currentMiddlePosition = currentScrollTop + halfContainer;
          for (var i = 0; i < carouselPositions.length; i++) {
            if (currentMiddlePosition > carouselPositions[i][0] && currentMiddlePosition < carouselPositions[i][1]) {
              currentItem = i;
              if (direction === 'next') {
                  currentItem++;
              } else if (direction === 'previous') {
                  currentItem--    
              }
            }
          }
      } 
      
      document.getElementById('container').scrollTo({
        top: carouselPositions[currentItem][0],
        behavior: 'smooth' 
      });
      
    }
    window.addEventListener('resize', getCarouselPositions);
    #container {
      scroll-snap-type: y mandatory;
      overflow-y: scroll;
      border: 2px solid var(--gs0);
      border-radius: 8px;
      height: 60vh;
      position: relative;
    }
    
    #container div {
      scroll-snap-align: start;
    
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 4rem;
    }
    #container div:nth-child(1) {
      background: hotpink;
      color: white;
      height: 50vh;
    }
    #container div:nth-child(2) {
      background: azure;
      height: 40vh;
    }
    #container div:nth-child(3) {
      background: blanchedalmond;
      height: 60vh;
    }
    #container div:nth-child(4) {
      background: lightcoral;
      color: white;
      height: 40vh;
    }
    <div id="container">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
    </div>
    
    <button onClick="goCarousel('previous')">previous</button>
    <button onClick="goCarousel('next')">next</button>

    Another good detail to add is to call getCarouselPositions function again if the window resizes:

    window.addEventListener('resize', getCarouselPositions);
    

    That's it.
    That was cool to do. I hope it can help somehow.