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

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="gallery-stack">  
  <div class="stack-inner">
    <figure>
      <div class="image">
        <img src="https://source.unsplash.com/featured/300x200/?greencolor">
      </div>
    </figure>     
    <figure>
      <div class="image">
        <img src="https://source.unsplash.com/featured/300x200/?sock">
      </div>
    </figure>
    <figure>
      <div class="image">
        <img src="https://source.unsplash.com/featured/200x300/?socks">
      </div>
    </figure>
    <figure>
      <div class="image">
        <img src="https://source.unsplash.com/featured/300x200/?coding">
      </div>
    </figure>
    <figure>
      <div class="image">
        <img src="https://source.unsplash.com/featured/300x300/?superhero">
      </div>
    </figure>
    <figure>
      <div class="image">
        <img src="https://source.unsplash.com/featured/300x300/?animation">
      </div>
    </figure> 
  </div>
</div>

              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  min-height: 100%;
  font-family: Roboto, sans-serif;
  background: #e2e0e0;
}

.gallery-stack {
  heigth: 100%;
  display: flex;
  justify-content: center;
	position: relative;
  z-index:10;
  overflow:hidden;
}
.gallery-stack .stack-inner {
	position: relative;
	width: 40%;
	height: 100%;
}
.gallery-stack figure {
	background-color: transparent;
	width: 100% !important;
	overflow: visible;
  pointer-events: none;
}
.gallery-stack figure:before {
	content: "";
	width: 1px;
	margin-left: -1px;
	float: left;
	padding-top: 70%;
}
.gallery-stack figure:after {
	content: "";
	display: table;
	clear: both;
}
.gallery-stack figure + figure {
	position: absolute;
	top: 0;
	left: 0;
}
.gallery-stack figure div {
	display: flex;
	justify-content: center;
	position: absolute;
	top: 0;
	left: 0;
	height: 100%;
	width: 100%;
	align-items: center;
}
.gallery-stack figure img {
	backface-visibility: hidden;
	object-fit: cover;
  position: absolute;
	height: 100%;
	width: 100%;
}
              
            
!

JS

              
                var slider = document.querySelector(".stack-inner");
var slides = document.querySelectorAll(".stack-inner figure");
var slidesDiv = document.querySelectorAll(".stack-inner figure div");
var progressWrap = gsap.utils.wrap(0, 1);
var indexWrap = gsap.utils.wrap(0, slides.length);

var slideDelay = 0;
var slideDuration = 1;
var snapX;

var slider = document.querySelector(".stack-inner");
var slides = document.querySelectorAll(".stack-inner figure");
var progressWrap = gsap.utils.wrap(0, 1);
var indexWrap = gsap.utils.wrap(0, slides.length);

var numSlides = slides.length;
var rotationX = -20;

gsap.set(slides, {
  xPercent: (i) => i * 100
});

gsap.set(slider, {attr: {"data-current-slide": 1 }});

var wrap = gsap.utils.wrap(-100, (numSlides - 1) * 100);

let animation = gsap.timeline({paused: true, repeat: -1})


animation.to(slides, {
  xPercent: "-=" + (numSlides * 100),
  duration: numSlides,
  ease: "none",
  modifiers: {
    xPercent: wrap
  }
});

slides.forEach(function(elem,index) {
  animation.fromTo(slides[indexWrap(index)].querySelector("img"), {
    xPercent: 0,
    opacity: 1,
    rotation: 0,
    scale:1,
  }, {
    rotation: rotationX,
    xPercent: 100,
    opacity: 0,
    duration: 0.5,
    scale:0.5,
    ease: "power1.inOut",
    immediateRender: true,
    onReverseComplete: function() {
      gsap.set(slides, {zIndex: -1});
      gsap.set(this.targets()[0].parentElement.parentElement, {zIndex: 1});
    },

  }, indexWrap(index+0.5));

  animation.fromTo(slides[indexWrap(index+1)].querySelector("img"), {
    xPercent: -100,
    opacity: 0,
    scale:0.5,
    rotation: -rotationX,
  }, {
    xPercent: 0,
    scale:1,
    opacity: 1,
    duration: 0.5,
    rotation: 0,
    onComplete: function() {
      gsap.set(slides, {zIndex: -1});
      gsap.set(this.targets()[0].parentElement.parentElement, {zIndex: 1});
    },
    ease: "power1.inOut",
  }, indexWrap(index));
});


animation.progress(1).progress(0);


if (numSlides > 1)
  setupDraggable(slider);


function setupDraggable(slider) {
  if (slideDelay > 0) var timer = gsap.delayedCall(slideDelay, autoPlay);
  var proxy = document.createElement("div");
  const tracker = InertiaPlugin.track(proxy, "x")[0]; 
  let rotationProxy = { rotation: 0 },
      rotationSetter = gsap.quickTo(slidesDiv, "rotation", {duration: 0.3}),
      rotationClamp = gsap.utils.clamp(-10, 10);
  var slideAnimation = gsap.to({}, {});
  var slideWidth = 0;
  var wrapWidth = 0;
  resize();

  var draggable = new Draggable(proxy, {
    trigger: slider,
    inertia: true,
    type: "x",
    allowNativeTouchScrolling: true,
    maxDuration: 1.5,
    minDuration: 0.75,
    onDragStart:function(){
      gsap.set(this.vars.trigger, {attr:{"dragging": "true" }});
    },
    onDragEnd:function(){
      gsap.set(this.vars.trigger, {attr:{"dragging": "false" }});
      const i = indexWrap(-this.endX / slideWidth);
      gsap.set(slider, {attr: {"data-current-slide": i+1 }});
    },
    onPress: updateDraggable,
    onDrag: updateProgress,
    onThrowUpdate: updateProgress,
    allowContextMenu : true,
    snap: {
      x: value => snapX(value, draggable.deltaX < 0 ? -1 : 1)
    }
  });


  window.addEventListener("resize", resize);
  //document.querySelector(".prevButton").addEventListener("click", () => animateSlides(-1));
  //document.querySelector(".nextButton").addEventListener("click", () => animateSlides(1));

  function updateDraggable() {
    if (slideDelay > 0) timer.restart(true);
    slideAnimation.kill();
    this.update();
  }

  function animateSlides(direction) {
    if (slideDelay > 0) timer.restart(true);
    slideAnimation.kill();
    var x = snapX(gsap.getProperty(proxy, "x") + direction * -slideWidth);
    var index = indexWrap(-x / slideWidth);
    gsap.set(slider, {attr: {"data-current-slide": index+1 }});
    slideAnimation = gsap.to(proxy, {
      x: x,
      duration: slideDuration,
      onUpdate: updateProgress,
    })
  }


  function autoPlay() {  
    if (draggable.isPressed || draggable.isDragging || draggable.isThrowing) {
      if (slideDelay > 0) timer.restart(true);
    } else {
      animateSlides(1);
    }
  }

  function updateProgress() {
    let rotateVelocity = rotationClamp(tracker.get("x") / 300);

    if (Math.abs(rotateVelocity) > Math.abs(rotationProxy.rotation)) {
      rotationProxy.rotation = rotateVelocity;
      gsap.to(rotationProxy, {rotation: 0, duration: 0.8, ease: "power3", overwrite: true, onUpdate: () => rotationSetter(rotationProxy.rotation)});
    }
    animation.progress(progressWrap(gsap.getProperty(proxy, "x") / -wrapWidth));
  }

  function resize() {
    var norm = (gsap.getProperty(proxy, "x") / wrapWidth) || 0;

    slideWidth = slides[0].offsetWidth;
    wrapWidth = slideWidth * numSlides;
    snapX = snapDirectional(slideWidth);

    gsap.set(proxy, {
      x: norm * wrapWidth
    });
    animateSlides(0);
    slideAnimation.progress(1);
  }

  function snapDirectional(increment) {
    let snap = gsap.utils.snap(increment);
    return (value, direction, threshold = 22) => {
      let snapped = snap(value);
      return !direction || Math.abs(snapped - value) < threshold || ((snapped - value < 0) === direction < 0) ? snapped : snap(direction < 0 ? value - increment : value + increment);
    };
  }
}
              
            
!
999px

Console