Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="carousel" duration="7000">
  <ul>
    <li id="c1_slide1"><div>Nice cat number 1<br /><a href="https://www.google.com">adopt me</a></div></li>  
    <li id="c1_slide2"><div>Nice cat number 2<br /><a href="https://www.google.com">adopt me</a></div></li>  
    <li id="c1_slide3"><div>Nice cat number 3<br /><a href="https://www.google.com">adopt me</a></div></li>  
    <li id="c1_slide4"><div>Nice cat number 4<br /><a href="https://www.google.com">adopt me</a></div></li>  
  </ul>
  <ol>
    <li><a href="#c1_slide1"></a></li>
    <li><a href="#c1_slide2"></a></li>
    <li><a href="#c1_slide3"></a></li>
    <li><a href="#c1_slide4"></a></li>
  </ol>
  <div class="prev">&lsaquo;</div>
  <div class="next">&rsaquo;</div>
</div>
<h2>Technique</h2>
<p>The above is an unordened list with horizontal overflow set to auto, snap scrolling and hidden scrollbars. You can swipe the carousel with your finger on touch screens through native touch scroll. The bullets are actually anchor links, which trigger a 'scroll-behaviour: smooth' scroll. The script uses a simple setInterval for autoplay which does not fire during hover and can be set on the carousel div in a custom attribute in milliseconds.</p>

<h2>Browser compatibility</h2>
<p>The code is tested on the newest versions of Chrome, Firefox, Chrome mobile and Firefox mobile in april 2021. This code uses fairly new techniques, like scroll snapping. However, I expect this solution to degrade quite graceful. It even looks and works 'okay-ish' without javascript.</p>

<h2>About the author</h2>
<p>I am a <a href="https://www.usecue.com/">website performance expert</a> and for <a href="https://www.usecue.com/portfolio/petervermaat/">a project</a> I was looking for a <a href="https://www.usecue.com/blog/carousel-with-scroll-snap/">touch enabled carousel</a>. I could not find one that was minimal, so I created a basic responsive and touch-enabled carousel that weighs only 3kb (uncompressed and including CSS). This carousel is based on modern web technology and will not slow down (the rendering of) your page... and yes, that <a href="https://www.usecue.com/portfolio/petervermaat/">project link</a> is a link to a production website.</p>

<h2>Credits</h2>
<p>The approach is (partly) based on the 2019 scroll snap carousel from <a href="https://nolanlawson.com">Nolan Lawson</a>. The cat images are borrowed from <a href="https://www.pexels.com/">Pexels</a>.</p>

<h2>License</h2>
<p>WTFPL - This is a GPL-compatible permissive license most commonly used as a free software license. As a public domain like license, the WTFPL is essentially the same as dedication to the public domain. It allows redistribution and modification of the work under any terms. The title is an abbreviation of "Do What The Fuck You Want To Public License".</p>

<p><br />Enjoy the code!<br /><br /><a href="https://www.usecue.com/">Joost van der Schee</a></p>

<br /><br /><br /><br />
              
            
!

CSS

              
                /* RESET CSS */
* {margin: 0; padding: 0;}

/* CAROUSEL CSS */
.carousel {position: relative;}
.carousel::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  box-shadow: inset 0px 0px 120px 30px rgba(0,0,0,0.35);
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.carousel ul {
  overflow: auto; 
  display: flex; 
  height: 40vw;
  max-height: 500px;
  scroll-snap-type: x mandatory;
  scroll-snap-points-y: repeat(100%);
  scroll-behavior: smooth;
  background: gray;
  -ms-overflow-style: none; /* IE and Edge */
  scrollbar-width: none; /* Firefox */
}
.carousel ul::-webkit-scrollbar {display: none; /* Hide scrollbar for Chrome, Safari and Opera */} 
.carousel ul li {
  min-width: 100%; 
  list-style: none; 
  background: url(https://images.pexels.com/photos/730896/pexels-photo-730896.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=1500) center center / cover no-repeat; 
  scroll-snap-align: start;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-weight: bold;
  font-size: 30px;
  text-align: center;
  text-shadow: 0px 0px 10px rgba(0,0,0,0.75);
}
.carousel ul li div {z-index: 9;}
.carousel ul li div a {color: white; display: inline-block; background: white; text-shadow: none; color: black; padding: 5px 15px; font-size: 18px; text-decoration: none; border-radius: 4px; margin-top: 10px;}
.carousel ol {position: absolute; bottom: 15px; display: flex; justify-content: center; left: 50%; transform: translateX(-50%); z-index: 9;}
.carousel ol li {list-style: none; padding: 0 5px;}
.carousel ol li a {display: block; height: 10px; width: 10px; border: 2px solid white; background: transparent; border-radius: 100%;}
.carousel ol li.selected a {background: white;}
.carousel .prev, .carousel .next {user-select:none; cursor: pointer; font-size: 50px; color: white; position: absolute; left: 0; padding: 15px; top: 50%; transform: translateY(-50%); z-index: 9;}
.carousel .next {left: auto; right: 0;}

/* RESPONSIVE CSS */
@media only screen and (max-width: 600px) {
  .carousel ul li div {display: none;}
}

/* DEMO CSS */
.carousel ul li:nth-child(2) {background-image: url(https://images.pexels.com/photos/1056251/pexels-photo-1056251.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=1500);}
.carousel ul li:nth-child(3) {background-image: url(https://images.pexels.com/photos/4492170/pexels-photo-4492170.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=1500);}
.carousel ul li:nth-child(4) {background-image: url(https://images.pexels.com/photos/2558605/pexels-photo-2558605.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=1500);}
p, h2 {margin: 15px auto; padding: 0 15px; max-width: 800px;}
h2 {margin-top: 30px;}
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', function() {
 
  const carousels = document.querySelectorAll('.carousel');
  carousels.forEach(function( carousel ) {

    const ele = carousel.querySelector('ul');
    const bullets = carousel.querySelectorAll('ol li');
    const nextarrow = carousel.querySelector('.next');
    const prevarrow = carousel.querySelector('.prev');
    bullets[0].classList.add('selected');

    const setSelected = function() {
        bullets.forEach(function(bullet) {
           bullet.classList.remove('selected');
        });
        let nthchild = (Math.round(ele.scrollLeft/carousel.scrollWidth)+1);
        carousel.querySelector('ol li:nth-child('+nthchild+')').classList.add('selected'); 
    }
    
    const nextSlide = function() {
      if(!carousel.querySelector('ol li:last-child').classList.contains('selected')) {
        carousel.querySelector('ol li.selected').nextElementSibling.querySelector('a').click();
        ele.scrollLeft = ele.scrollLeft + carousel.scrollWidth;
      } else {
        carousel.querySelector('ol li:first-child a').click();
        ele.scrollLeft = 0;
      }
    }

    const prevSlide = function() {
      if(!carousel.querySelector('ol li:first-child').classList.contains('selected')) {
        carousel.querySelector('ol li.selected').previousElementSibling.querySelector('a').click();
        ele.scrollLeft = ele.scrollLeft - carousel.scrollWidth;
      } else {
        carousel.querySelector('ol li:last-child a').click();
        ele.scrollLeft = ele.scrollWidth - carousel.scrollWidth;
      }
    }
      
    // Attach the handlers
    ele.addEventListener("scroll", setSelected);
    nextarrow.addEventListener("click", nextSlide);
    prevarrow.addEventListener("click", prevSlide);

    //setInterval for autoplay
    if(carousel.getAttribute('duration')) {
      setInterval(function(){ 
        if (ele != document.querySelector(".carousel:hover ul")) {
          if(ele.scrollWidth > ele.scrollLeft + carousel.scrollWidth) {
            ele.scrollLeft = ele.scrollLeft + carousel.scrollWidth;
          } else ele.scrollLeft = 0;
        }
      }, carousel.getAttribute('duration'));
    }
    
  }); //end foreach
  
}); //end onload
              
            
!
999px

Console