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 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="slide-wrap">
  <div class="slide">
    <div class="image" style="background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/unsplash-1.jpg)"></div>
  </div>
  <div class="text">
    <p>Sand &amp;</p>
    <p>Rocks &amp;</p>
    <p>Water &amp;</p>
    <p>Sky</p>
  </div>
</div>
<div class="slide-wrap">
  <div class="slide">
    <div class="image" style="background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/unsplash-2.jpg)"></div>
  </div>
  <div class="text">
    <p>River &amp;</p>
    <p>Rocks &amp;</p>
    <p>Mountain &amp;</p>
    <p>Trees</p>
  </div>
</div>
<div class="slide-wrap">
  <div class="slide">
    <div class="image" style="background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/unsplash-3.jpg)"></div>
  </div>
  <div class="text">
    <p>Water &amp;</p>
    <p>Waves &amp;</p>
    <p>Clouds &amp;</p>
    <p>Shore</p>
  </div>
</div>
<div class="slide-wrap">
  <div class="slide">
    <div class="image" style="background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/unsplash-4.jpg)"></div>
  </div>
  <div class="text">
    <p>Castle &amp;</p>
    <p>Fields &amp;</p>
    <p>Lakes &amp;</p>
    <p>Trees</p>
  </div>
</div>
<div class="slide-wrap">
  <div class="slide">
    <div class="image" style="background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/unsplash-5.jpg)"></div>
  </div>
  <div class="text">
    <p>Waterfall &amp;</p>
    <p>Stream &amp;</p>
    <p>Woods &amp;</p>
    <p>Mist</p>
  </div>
</div>
<div class="instructions">
  Click &amp; drag to swipe (desktop)<br/>or touch swipe (mobile)
</div>
              
            
!

CSS

              
                html, body {
  width: 100%;
  height: 100%;
}

body {
  overflow: hidden;
  font-family: "Merriweather Sans", sans-serif;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}

.slide-wrap {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: absolute;
  cursor: pointer;
}

.slide {
  width: 100%;
  height: 100%;
  position: absolute;
  
  .image {
    background-size: cover;
    width: 100%;
    height: 100%;
  }
}

.text {
  position: absolute;
  font-size: 3rem;
  color: #fff;
  top: 0px;
  left: 0px;
  text-transform: uppercase;
  margin-left: 20px;
  
  p {
    margin: 0;
  }
}

.instructions {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 1000;
  color: #fff;
  font-size: 14px;
}
              
            
!

JS

              
                var $images = $(".slide");
var $slides = $(".slide-wrap");
var slides = [];
var windowWidth, windowHeight, windowRatio;
var imageWidth, imageHeight, imageTop, imageLeft;
var imageRatio = 1200/900;
var fontSize = 20;
var parallaxTarget = {x: 0, y: 0};
var parallaxPosition = {x: 0, y: 0};
var mouseDownPosition = {};
var moveSlidePosition = {x: 0, y: 0};
var currentSlide, slideMoving;
var easeOutQuad = "cubic-bezier(0.215, 0.61, 0.355, 1)";

function Slide(elem) {
  this.init(elem);
  this.splitText();
}

Slide.prototype = {
  init: function (elem) {
    this.wrap = $(elem);
    this.image = $(this.wrap.find(".slide")[0]);
    this.text = $(this.wrap.find(".text")[0]);
  },
  splitText: function() {
    var lines = this.text.find("p");
    this.lines = [];
    for (var i = 0, l = lines.length; i < l; i++) {
      var t = lines[i].innerText || lines[i].textContent;
      var letters = t.split("");
      var text = letters.map(function(l) {
        return "<span>"+l+"</span>";
      });
      lines[i].innerHTML = text.join("");
      lines.css({position: "relative"});
      this.lines.push($(lines[i]).find("span"));
    }
    this.letters = this.text.find("span");
    this.resetText();
  },
  resetText: function() {
    for (var i = 0, l = this.letters.length; i < l; i++) {
      this.letters[i].style.opacity = 0;
      this.letters[i].style.transform = "translate3d(0, "+(fontSize*0.5)+"px, 0)";
    }
  },
  animateTextIn: function() {
    for (var i = 0, l = this.lines.length; i < l; i++) {
      var delay = i*400;
      for (var j = 0, m = this.lines[i].length; j < m; j++) {
        this.lines[i][j].style.transition = "opacity 300ms linear "+(delay+(j*100))+"ms, " +
          "transform 300ms "+easeOutQuad+" "+(delay+(j*100))+"ms";
        this.lines[i][j].style.opacity = 1;
        this.lines[i][j].style.transform = "translate3d(0, 0, 0)";
      }   
    }
  },
  transitionOut: function(e) {
    var tweenTo, _this = this;
    if (-(mouseDownPosition.x - e.pageX) > 0) {
      tweenTo = windowWidth;
    } else {
      tweenTo = -windowWidth;
    }
    this.wrap.css({transition: "transform 300ms "+easeOutQuad, transform: "translateX("+tweenTo+"px)"});
    this.wrap.on("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend", function() {
      _this.wrap.css({transition: "", transform: "translateX(0px)"});
      updateStack();
      _this.wrap.off("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend");
    })
  },
  move: function() {
    this.image.css({transform: "translateX("+parallaxPosition.x+"px)"});
    this.image.css({webkitTransform: "translateX("+parallaxPosition.x+"px)"});
  },
  resize: function() {
    this.wrap.css({width: windowWidth, height: windowHeight});
    this.image.css({width: imageWidth, height: imageHeight, top: imageTop, left: imageLeft});
    this.text.css({fontSize: fontSize, height: fontSize*5, top: "50%", marginTop: fontSize*5*-0.5});
    this.text.find("p").css({height: fontSize*1.2});
    for (var i = 0, l = this.lines.length; i < l; i++) {
      this.lines[i].css({position: "relative"});
      var left = 0;
      for (var j = 0, m = this.lines[i].length; j < m; j++) {
        this.lines[i][j].style.left = left+"px";
        this.lines[i][j].style.width = this.lines[i][j].offsetWidth+"px";
        left += this.lines[i][j].offsetWidth;
      }
      this.lines[i].css({position: "absolute"});
    }
  }
}

function initSlides() {
  for (var i = 0, l = $slides.length; i < l; i++) {
    var slide = new Slide($slides[i]);
    slides.push(slide);
  }
}

/*
* resize
*/
function resize() {
  windowWidth = window.innerWidth;
  windowHeight = window.innerHeight;
  windowRatio = windowWidth/windowHeight;
  if (windowRatio > imageRatio) {
    imageWidth = windowWidth*1.1;
    imageHeight = imageWidth/imageRatio;
  } else {
    imageHeight = windowHeight;
    imageWidth = imageHeight*imageRatio;
  }
  imageTop = (imageHeight-windowHeight)*-0.5;
  imageLeft = (imageWidth-windowWidth)*-0.5;
  fontSize = windowHeight*0.18;
  if (windowWidth < fontSize*6) {
    fontSize = windowWidth/6;
  }
  
  for (var i = 0, l = slides.length; i < l; i++) {
    slides[i].resize();
  }
}

function mousedown(e) {
  if (e.type == "mousedown") {
    mouseDownPosition.x = e.pageX;
    mouseDownPosition.y = e.pageY;
  } else if (e.type == "touchstart") {
    mouseDownPosition.x = e.pageX = e.originalEvent.touches[0].pageX;
    mouseDownPosition.y = e.pageY = e.originalEvent.touches[0].pageY;
  }
  
  slideMoving = true;
  moveSlide(e);
}

function mousemove(e) {
  if (e.type == "touchmove") {
    e.preventDefault();
    e.pageX = e.originalEvent.touches[0].pageX;
    e.pageY = e.originalEvent.touches[0].pageY;
  } else {
    parallaxSlide(e);
  }
  moveSlide(e);
}

function motion(e) {
  parallaxSlideMotion(e);
}

function mouseup(e) {
  if (e.type == "touchend") {
    e.pageX = e.originalEvent.changedTouches[0].pageX;
    e.pageY = e.originalEvent.changedTouches[0].pageY;
  }
  releaseSlide(e);
}

function parallaxSlide(e) {
  parallaxTarget.x = (windowWidth/2 - e.pageX);
}

function parallaxSlideMotion(e) {
  parallaxTarget.x = event.accelerationIncludingGravity.x*900;
}

function moveSlide(e) {
  moveSlidePosition.x = -(mouseDownPosition.x - e.pageX);
}

function releaseSlide(e) {
  var leavingSlide = currentSlide;
  leavingSlide.transitionOut(e);
  currentSlide = null;
  slideMoving = false;
}

function updateStack() {
  slides.unshift(slides.pop());
  $.each(slides, function(i, slide) {
    slide.wrap.css({zIndex: i*100});
  })
  slides[0].resetText();
  currentSlide = slides[slides.length-1];
  currentSlide.animateTextIn();
}

function loop() {
  requestAnimationFrame(loop);
  moveSlides();
}

function moveSlides() {
  parallaxPosition.x = (parallaxTarget.x - parallaxPosition.x)*0.01;
  for (var i = 0, l = slides.length; i < l; i++) {
    slides[i].move();
  }
  if (currentSlide && slideMoving) currentSlide.wrap.css({transform: "translateX("+moveSlidePosition.x+"px)", webkitTransform: "translateX("+moveSlidePosition.x+"px)"});
}

initSlides();
resize();
updateStack();

loop();
$(window).on("resize", resize);
$(document).on("mousedown touchstart", mousedown);
$(document).on("mousemove touchmove", mousemove);
$(document).on("mouseup touchend", mouseup);
$(document).on("devicemotion", motion);



              
            
!
999px

Console