Swiper offset slider

In this example below you will see how to do a Swiper offset slider with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by Fabio Ottaviani, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright Fabio Ottaviani ©
  • HTML
  • CSS
  • JavaScript
    <div class="swiper-container">
  <div class="swiper-wrapper">
    <div class="swiper-slide">
      <div class="slide-inner" style="background-image:url(https://raw.githubusercontent.com/supahfunk/supah-codepen/master/canyon-3.jpg)"></div>
    </div>
    <div class="swiper-slide">
      <div class="slide-inner" style="background-image:url(https://raw.githubusercontent.com/supahfunk/supah-codepen/master/canyon-2.jpg)"></div>
    </div>
    <div class="swiper-slide">
      <div class="slide-inner" style="background-image:url(https://raw.githubusercontent.com/supahfunk/supah-codepen/master/canyon-1.jpg)"></div>
    </div>
    <div class="swiper-slide">
      <div class="slide-inner" style="background-image:url(https://raw.githubusercontent.com/supahfunk/supah-codepen/master/canyon-4.jpg)"></div>
    </div>
  </div>
  <div class="swiper-button-next swiper-button-white"></div>
  <div class="swiper-button-prev swiper-button-white"></div>
</div>



/*Downloaded from https://www.codeseek.co/supah/swiper-offset-slider-aKmQXG */
    body {
  background: #fff;
  margin: 0;
  padding: 0;
}

.swiper-container {
  height: calc(100vh - 100px);
  margin: 50px;
}

.swiper-slide {
  overflow: hidden;
}

.slide-inner {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background-size: cover;
  background-position: center;
}

.swiper-button-next {
  right: 30px;
}

.swiper-button-prev {
  left: 30px;
}


/*Downloaded from https://www.codeseek.co/supah/swiper-offset-slider-aKmQXG */
    var options = {
  loop: true,
  speed: 1000,
  grabCursor: true,
  watchSlidesProgress: true,
  mousewheelControl: true,
  keyboardControl: true,
  longSwipesRatio: 0.1,
  mousewheel: true,
  navigation: {
    nextEl: ".swiper-button-next",
    prevEl: ".swiper-button-prev"
  },
  on: {
    progress: function() {
      var swiper = this;
      for (var i = 0; i < swiper.slides.length; i++) {
        var slideProgress = swiper.slides[i].progress;
        var innerOffset = swiper.width * 2;
        var innerTranslate = slideProgress * innerOffset;
        console.log(slideProgress);
        swiper.slides[i].querySelector(".slide-inner").style.transform =
          "translate3d(" + innerTranslate + "px, 0, 0)";
      }      
    },
    touchStart: function() {
      var swiper = this;
      for (var i = 0; i < swiper.slides.length; i++) {
        swiper.slides[i].style.transition = "";
      }
    },
    setTransition: function(speed) {
      var swiper = this;
      for (var i = 0; i < swiper.slides.length; i++) {
        swiper.slides[i].style.transition = speed + "ms";
        swiper.slides[i].querySelector(".slide-inner").style.transition =
          speed + "ms";
      }
    }
  }
};

var swiper = new Swiper(".swiper-container", options);


Comments