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

              
                <div class="carousel-wrapper" style="max-width: 1080px;">
  <button class="carousel-action" data-action="prev">prev</button>
  <button class="carousel-action" data-action="next">next</button>  
  <div class="carousel ratio-16by9" data-slides="3" data-scroll="1" data-direction="x">
    <div class="carousel-item">
      <div class="background-image"></div>
    </div>
    <div class="carousel-item">
      <div class="background-image"></div>
    </div>
    <div class="carousel-item">
      <div class="background-image"></div>
    </div>
    <div class="carousel-item">
      <div class="background-image"></div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                img {
  max-width: 100%;
  max-height: 100%;
}

.carousel {   
  position: relative;
  width: 100%;
  height: 100%;  
  overflow: hidden;

  .carousel-item {       
    position: absolute;
    font-size: 5rem;
    width: 100%;    
    height: 100%;    
    background: lightgray;    
    cursor: pointer;
    
    display: flex;
    justify-content: center;
    align-items: center;
    &:nth-child(even) {
      background: lightblue;
    }
  }
}

.carousel-wrapper {  
  position: relative;
  outline: 3px solid black;      
}

.ratio-16by9 {
  padding-top: calc(9/16 * 100%);
  width: 100%;

  > div {
    position: absolute;
    top: 0;
    left: 0;
  }
}
.ratio-1by1 {
  padding-top: 100%;
  width: 100%;

  > div {
    position: absolute;
    top: 0;
    left: 0;
  }
}

.carousel-action {
  position: absolute;
  z-index: 10;  
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  padding: 10px;  
  border: 1px solid black;
  cursor: pointer;

  &:first-child {
    left: 0;
  }

  &:nth-child(2) {
    right: 0;
  }
}

@function rand($min, $max) {
  @return (random($max - $min) + $min);
}

.background-image {
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  
  height: 100%;
  width: 100%;
}

// generate random images
.carousel-item {
  @for $i from 1 through 50 {
    &:nth-child(#{$i}) .background-image {
      $image: "https://picsum.photos/" + rand(250,1000) + "/" + rand(250,1000);
      background-image: url(#{$image});
    }
  }
}


              
            
!

JS

              
                console.clear();

$(function() {
  $(".carousel").each(function() { 
    initCarousel($(this));
  });  
})

function initCarousel(carousel) {  
  // Carousel settings
  let speed = carousel.attr("data-speed") || .5,
      slides = carousel.find(".carousel-item");
  let numSlides = slides.length,
      numShow = carousel.attr("data-slides") || 1,
      numScroll = carousel.attr("data-scroll") || 1,
      direction = carousel.attr("data-direction") || "x";
  if (direction !== "x") direction = "y";    
  let percent = direction + "Percent"; //xPercent or yPercent
  
  /*
  If slides is the same as the number to show, 
  clone them so the wraparound is smooth  
  Also prevents the blank flickering
  */  
  if (numSlides == numShow) {
    //carousel.append(slides.first().clone());
    slides.each(function() {
      carousel.append($(this).clone());
    });
    slides = carousel.find(".carousel-item");
    numSlides = slides.length;
  }

  // Width or height based on how many slides to show at a time.
  // Set the x or y percent of each slide
  let options = {};
  options[direction === "x" ? "width" : "height"] = 100/numShow + "%"; 
  options[percent] = i => i * 100;  
  gsap.set(slides, options);    

  // Each slide size and wrap size.
  let size = direction === "x" ? slides.width() : slides.height();
  let entireSize = size * numSlides;  

  // Slides animation settings
  options = {
    ease: "none",
    paused: true,
    repeat: -1,
    modifiers: {}
  };
  options[percent] = "+=" + (numSlides * 100);  
  // This is the infinite loop
  options.modifiers[percent] = gsap.utils.wrap( -100, (numSlides-1) * 100 );

  // Animation setups
  let animation = gsap.to(slides, speed, options);
  let proxyAnimation = gsap.to({}, .1, {});

  // Proxy for dragging
  let proxy = document.createElement("div");
  options = {};
  options[direction] = snap;
  let draggable = new Draggable(proxy, {
    trigger: carousel,
    onDrag: updateProgress,
    allowContextMenu: true,
    // Premium
    inertia: true,
    onThrowUpdate: updateProgress,    
    snap: options    
  });
  
  // Next and prev function
  function animateSlides(dir) {    
    if (gsap.isTweening(proxy) || gsap.isTweening(slides)) return;

    proxyAnimation.kill();    
    let x = snap(transform() + dir * size * numScroll);         

    options = {
      onUpdate: updateProgress,
      onComplete: () => gsap.data = {clicked: false}
    };
    options[direction] = x;        
    proxyAnimation = gsap.to(proxy, speed, options);
  }
  
  // Allows drag to move slides
  function updateProgress() {    
    animation.progress(gsap.utils.wrap(0, 1, transform() / entireSize));       
  }
  
  // Helper functions
  function snap(x) {
    return Math.round(x / size) * size;
  }
  function transform() {
    return gsap.getProperty(proxy, direction);    
  }

  // Controls
  carousel.parent().find(".carousel-action").on("click tap", function() {
    animateSlides($(this).attr("data-action") !== "next" ? 1 : -1);    
  });  
  
  // Jump to this slide when clicked
  $(slides).on("click tap", function() {
    let x = gsap.getProperty(this, "transform");
    x = x.split(") ").find(el => el.includes("translate("));
    
    if (x) {
      x = x.slice("translate(".length, -1).split(",");
      x.forEach((el, i) => x[i] = el.replace(/%|px/, " ").trim());      
            
      x = x[direction === "x" ? 0 : 1];
      x = gsap.utils.snap(100, x);
      
      if (x == 0) {
        animateSlides(1);        
      }
      else if (x == (numShow-1) * 100) {
        animateSlides(-1);
      }
      else return;
    }        
  });
}
              
            
!
999px

Console