Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!-- Autoplay won't start if the viewport height is not large enough to show the entire carousel due to Intersection Observer -->
<div class="carousel-wrapper">
<div 
  class="carousel"
  role="group"
  aria-label="Ryoan-ji Temple’s Rock Garden"
  aria-roledescription="carousel" 
>
  <div aria-label="Choose slide to display" role="group" class="carousel__navdots home-page">
    <button type="button" aria-label="1 of 4" aria-disabled="true"></button>
    <button type="button" aria-label="2 of 4" aria-disabled="false"></button>
    <button type="button" aria-label="3 of 4" aria-disabled="false"></button> 
    <button type="button" aria-label="4 of 4" aria-disabled="false"></button> 
  </div>
  <div
    class="carousel__slides"
    aria-atomic="false" 
    aria-live="off" 
  >
    <div
      class="carousel__slide"
      role="group"
      aria-label="1 of 4"
      aria-roledescription="slide"
    >
      <img src="https://translating-japanese-gardens.pages.dev/ryoanji/ryoanji-banner-spring-1882.jpg" width="991" height="702"/>
    </div>
    <div
      class="carousel__slide"
      role="group"
      aria-label="2 of 4"
      aria-roledescription="slide"
    >
      <img src="https://translating-japanese-gardens.pages.dev/ryoanji/ryoanji-banner-summer-1882.jpg" width="991" height="702"/>
    </div>
    <div
      class="carousel__slide"
      role="group"
      aria-label="3 of 4"
      aria-roledescription="slide"
    >
      <img src="https://translating-japanese-gardens.pages.dev/ryoanji/ryoanji-banner-autumn-1882.jpg" width="991" height="702"/>
    </div>
    <div
      class="carousel__slide"
      role="group"
      aria-label="4 of 4"
      aria-roledescription="slide"
    >
      <img src="https://translating-japanese-gardens.pages.dev/ryoanji/ryoanji-banner-winter-1882.jpg" width="991" height="702"/>
    </div>
  </div>
</div>
</div>
              
            
!

CSS

              
                /* Show just one 991px wide slide at a time */
.carousel {
  width: 991px;
}
.carousel__slides,
.carousel__slide {
  width: 100%;
}
/* Make carousel swipeable */
.carousel__slides {
  column-gap: 20px;
  display: flex;
  overflow: auto;
  scroll-snap-type: x mandatory;
}
.carousel__slide {
  flex: 0 0 auto;
  scroll-snap-align: center;
}
.carousel__slides.smooth-scroll {
  scroll-behavior: smooth;
}
/* Hide scrollbar */
.carousel__slides {
  scrollbar-width: none; /* Firefox and latest Chromium */
}
.carousel__slides::-webkit-scrollbar {
  display: none; /* Safari and legacy Chromium */
}

/* Styling nav dots */
.carousel {
  padding-bottom: 60px;
  position: relative;
}
.carousel__navdots {
  bottom: 0;
  column-gap: 16px;
  display: flex;
  justify-content: center;
  position: absolute;
  left: 0;
  right: 0;
}
.carousel__navdots button {
  /* reset default style */
  -moz-appearance: none;
  -webkit-apperance: none;
  appearance: none;
  border: 0;
  cursor: pointer;
  /* style as a grey dot */
  background-color: #9a9a9a; 
  border-radius: 50%;
  height: 10px;
  padding: 0;
  width: 10px;
}
.carousel__navdots button.is-active {
  background-color: #0060a8; /* blue */
}
.carousel__navdots button:focus-visible {
  outline: 2px solid #0060a8; /* blue */
  outline-offset: 2px;
}

/* Stylistic purpose */
.carousel-wrapper {
  align-items: center;
  display: flex;
  height: 100vh;
  padding: 0 200px;
}
.carousel {
  margin: 0 auto;
}
img {
  height: auto;
  max-width: 100%;
}
body {
  margin: 50px;
}
p {
  font-family: Georgia, serif;
  font-size: 75px;
}
              
            
!

JS

              
                // Components
const carouselContainer = document.querySelector('.carousel');
const slideWrapper = document.querySelector('.carousel__slides');
const slides = document.querySelectorAll('.carousel__slide');
const navdotWrapper = document.querySelector('.carousel__navdots');
const navdots = document.querySelectorAll('.carousel__navdots button');

// Parameters
const n_slides = slides.length;
const n_slidesCloned = 1;
let slideWidth = slides[0].offsetWidth;
let spaceBtwSlides = Number(window.getComputedStyle(slideWrapper).getPropertyValue('grid-column-gap').slice(0, -2)); // drop px at the end
function index_slideCurrent() {
  return Math.round(slideWrapper.scrollLeft / (slideWidth + spaceBtwSlides) - n_slidesCloned);
}

// Nav dot click handler
function goto(index) {
  slideWrapper.scrollTo((slideWidth + spaceBtwSlides) * (index + n_slidesCloned), 0);
}
for (let i = 0; i < n_slides; i++) {
  navdots[i].addEventListener('click', () => goto(i));
}

// Marking nav dots
function markNavdot(index) {
  navdots[index].classList.add('is-active');
  navdots[index].setAttribute('aria-disabled', 'true');
}
function updateNavdot() {
  const c = index_slideCurrent();
  if (c < 0 || c >= n_slides) return; // in these cases, forward() and rewind() will be executed soon
  markNavdot(c);
}
let scrollTimer;
slideWrapper.addEventListener('scroll', () => {
  // reset
  navdots.forEach(navdot => {
    navdot.classList.remove('is-active');
    navdot.setAttribute('aria-disabled', 'false');
  });
  // handle infinite scrolling
  if (scrollTimer) clearTimeout(scrollTimer); // to cancel if scroll continues
  scrollTimer = setTimeout(() => {
    if (slideWrapper.scrollLeft < (slideWidth + spaceBtwSlides) * (n_slidesCloned - 1/2)) {
      forward();
    }
    if (slideWrapper.scrollLeft > (slideWidth + spaceBtwSlides) * ((n_slides - 1 + n_slidesCloned) + 1/2)) {
      rewind();
    }
  }, 100);
  // mark the navdot
  updateNavdot();
});

// Handle window resizing
let resizeTimer;
window.addEventListener('resize', () => {
  // update parameters
  slideWidth = slides[0].offsetWidth;
  spaceBtwSlides = Number(window.getComputedStyle(slideWrapper).getPropertyValue('grid-column-gap').slice(0, -2)); // drop px at the end
  // for autoplay
  if(resizeTimer) clearTimeout(resizeTimer);
  stop();
  resizeTimer = setTimeout(()=>{
    play();
  }, 400);
});

// Infinite scrolling
const firstSlideClone = slides[0].cloneNode(true);
firstSlideClone.setAttribute('aria-hidden', 'true');
slideWrapper.append(firstSlideClone);

const lastSlideClone = slides[n_slides - 1].cloneNode(true);
lastSlideClone.setAttribute('aria-hidden', 'true');
slideWrapper.prepend(lastSlideClone);

function rewind() {
  slideWrapper.classList.remove('smooth-scroll');
  setTimeout(() => { // wait for smooth scroll to be disabled
    slideWrapper.scrollTo((slideWidth + spaceBtwSlides) * n_slidesCloned, 0);
    slideWrapper.classList.add('smooth-scroll');
  }, 100);
}

function forward() {
  slideWrapper.classList.remove('smooth-scroll');
  setTimeout(() => { // wait for smooth scroll to be disabled
    slideWrapper.scrollTo((slideWidth + spaceBtwSlides) * (n_slides - 1 + n_slidesCloned), 0);
    slideWrapper.classList.add('smooth-scroll');
  }, 100);
}

// Autoplay
function next() {
  goto(index_slideCurrent() + 1);
}
const pause = 2500;
let itv;
function play() {
  // early return if the user prefers reduced motion
  if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
    return;
  }
  clearInterval(itv);
  slideWrapper.setAttribute("aria-live", "off");
  itv = setInterval(next, pause);
}
function stop() {
  clearInterval(itv);
  slideWrapper.setAttribute("aria-live", "polite");
}
const observer = new IntersectionObserver(callback, {threshold: 0.99});
observer.observe(carouselContainer);
function callback(entries, observer) {
  entries.forEach((entry) => {
    console.log(`entry.intersectionRatio: ${entry.intersectionRatio}`)
    if (entry.isIntersecting) {
      console.log(`entry.isIntersecting is true.`)
      play();
    } else {
      console.log(`entry.isIntersecting is false.`)
      stop();
    }
  })
}
// for mouse users
carouselContainer.addEventListener("pointerenter", () => stop());
carouselContainer.addEventListener("pointerleave", () => play());
// for keyboard users
carouselContainer.addEventListener("focus", () => stop(), true);
carouselContainer.addEventListener("blur", () => {
  if (carouselContainer.matches(":hover")) return;
  play();
}, true);
// for touch device users
carouselContainer.addEventListener("touchstart", () => stop());

// Initialization
goto(0);
markNavdot(0);
slideWrapper.classList.add('smooth-scroll');

              
            
!
999px

Console