css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html>
<head>
</head>
<body>
  <div class="slideshow-holder">

   <div class="slide-holder">
    <div class="slide">
      <img src="http://placekitten.com/g/300/250" alt="slide 1">
    </div>

    <div class="slide">
      <img src="http://placekitten.com/g/301/250" alt="slide 2">
    </div>

    <div class="slide">
      <img src="http://placekitten.com/g/302/250" alt="slide 3">
    </div>

    <div class="slide">
      <img src="http://placekitten.com/g/303/250" alt="slide 4">
    </div>

    <div class="slide">
      <img src="http://placekitten.com/g/304/250" alt="slide 5">
    </div>  
  </div>
  
  <button id="btn-prev">Previous</button>
  <button id="btn-pause">Stop for a few seconds</button>
  <button id="btn-next">Next</button>
</div>

</body>
</html>
            
          
!
            
              .slideshow-holder {

}

.slide-holder {
  height:250px;
  margin-bottom:10px;
  position:relative;
}

.slide {
  width:300px;
  display:block;
  overflow:hidden;
  position:absolute;
  top:0;
  transition: opacity 0.25s;
  opacity:0;
}

.slide-visible {
  opacity:1;
}
            
          
!
            
              var slideElements = document.getElementsByClassName('slide');
var nextBtn = document.getElementById('btn-next');
var prevBtn = document.getElementById('btn-prev');
var pauseBtn = document.getElementById('btn-pause');
const TIME_PER_SLIDE = 1000;
const INACTIVITY_TIME = 8000;
var currentSlideIndex = 0;
var slideChangeInterval;
var inactivityTimeout;

nextBtn.addEventListener('click', onNextClick);
prevBtn.addEventListener('click', onPrevClick);
pauseBtn.addEventListener('click', onPauseClick);


// show 1 slid at the passed in index, hide others
function showSlideAtIndex(index) {
    
  var slideCount = slideElements.length;
  
  // fix invalid indexes
  if (index >= slideCount) index = 0;
  if (index < 0 ) index = slideCount -1;
  
  // update the value that all functions have access to
  currentSlideIndex = index;
  
  console.log('will show slide at index: ', index);
  
  for (var i = 0; i < slideCount; i += 1 ) {
	    
	var slideElement = slideElements[i]; 
    	if (i === index) {
        //slideElement.hidden = false;
        slideElement.classList.add('slide-visible');
      } else {
        //slideElement.hidden = true;
        slideElement.classList.remove('slide-visible');
      }    
	  }
  }





// starts a timer that rotates the slides
function animateSlides() {

  var slideCount = slideElements.length;
  
  var interval = setInterval(function() {
		currentSlideIndex += 1;
		showSlideAtIndex(currentSlideIndex);
  }, TIME_PER_SLIDE);
  
  return interval;
  
}

// advance to next slide
function onNextClick(event) {
	console.log('next click');
	onUserSlideshowInteraction();
  currentSlideIndex +=1;
	showSlideAtIndex(currentSlideIndex);
}

// go back 1 slide
function onPrevClick(event) {
	console.log('prev click');
	onUserSlideshowInteraction();  
	currentSlideIndex -=1;
  showSlideAtIndex(currentSlideIndex);
}

// stay on current slide
function onPauseClick(event) {
	onUserSlideshowInteraction();  
}

// when the user interacts with the slidehsow
function onUserSlideshowInteraction() {
  
  // stop moving to the next slide
  clearInterval(slideChangeInterval); 

  // set a timeout to restart the slideshow after a period of inactivity
  clearTimeout(inactivityTimeout);
  inactivityTimeout = setTimeout(function() {
    slideChangeInterval = animateSlides();
  }, INACTIVITY_TIME);
}

// start the show!
showSlideAtIndex(currentSlideIndex);
slideChangeInterval = animateSlides();


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console