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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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">
  <button class="carousel__button" data-left>prev</button>
  <button class="carousel__button" data-right>next</button>
  
  <div class="carousel__track-container">
    <ul class="carousel__track" data-track>
      <li class="carousel__slide current-slide"><img src="https://images.pexels.com/photos/2289407/pexels-photo-2289407.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940" ></li>
      <li class="carousel__slide"><img src="https://images.pexels.com/photos/2404370/pexels-photo-2404370.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></li>
      <li class="carousel__slide"><img src="https://images.pexels.com/photos/2408692/pexels-photo-2408692.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"></li>
    </ul>

  
</div>
              
            
!

CSS

              
                *{
  margin: 0;
  padding: 0;
  list-style: none;
}


.carousel__track-container {
  margin: 20px;
  width: 60vw;
  height: 60vh;
  overflow: hidden;
}
.carousel__track {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 250ms ease;
}
.carousel__slide{
  width: 100%;
  height: 100%;
  position: absolute;
}
.carousel__slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}


button {
  margin: 20px 0 0 20px;
}

.copy-slides {
  width: 100%;
  height: 100%;
}
              
            
!

JS

              
                const track = document.querySelector('[data-track]');
const slides = Array.from(track.children);
const prevButton = document.querySelector('[data-left]');
const nextButton = document.querySelector('[data-right]');
const last =  track.lastChild;
const first =  track.firstChild;


// This is the first slide copy that need to be push to the end
const firstSlide = slides[0];
const firstSlideCopy = firstSlide.cloneNode(true);
firstSlideCopy.classList.add('.copy-slides');
slides.push(firstSlideCopy);

track.appendChild(firstSlideCopy);

// This is the last slide copy that needs to be unshifted in front
const lastSlide = slides[slides.length-1];
const lastSlideCopy = lastSlide.cloneNode(true);
lastSlideCopy.classList.add('.copy-slides');
slides.unshift(lastSlideCopy);

track.appendChild(lastSlideCopy);


console.log(track);
console.log(slides);




const slideWidth = slides[0].getBoundingClientRect().width;

const setSlidePosition = (slide, index) => {
  slide.style.left = slideWidth * index + 'px';
};

slides.forEach(setSlidePosition);



const moveToSlide = (track, currentSlide, targetSlide) => {
  track.style.transform = 'translateX(-'+ targetSlide.style.left +')';
  
  currentSlide.classList.remove('current-slide');
  targetSlide.classList.add('current-slide');
}

nextButton.addEventListener('click', (e) => {
  const currentSlide = track.querySelector('.current-slide');
  const nextSlide = currentSlide.nextElementSibling;
  
  moveToSlide(track, currentSlide, nextSlide);
});

prevButton.addEventListener('click', (e) => {
  const currentSlide = track.querySelector('.current-slide');
  const prevSlide = currentSlide.previousElementSibling;
  
  moveToSlide(track, currentSlide, prevSlide);
})





              
            
!
999px

Console