1

I am trying to set currentTime = 0 to the slides with video when the slider is in a image slide.

Its possible with swiper js?

https://codepen.io/josedeharo/pen/QWwgaoK

The actual js:

var swiper = new Swiper('.swiper-container', {
  pagination: '.swiper-pagination',
  paginationClickable: true,
  nextButton: '.swiper-button-next',
  prevButton: '.swiper-button-prev',
  spaceBetween: 30,
  autoplayDisableOnInteraction: true,
    speed: 1000,
   autoplay: true,
});
Richard Dallaway
  • 4,250
  • 1
  • 28
  • 39
Jose
  • 117
  • 1
  • 8

1 Answers1

3

First reset and autoplay video not related to swiper API - use simple JS:

stackoverflow: How to Reset video using html5

You should use Swiper API events:

mySwiper.on('slideChange', function () {
  console.log('slide changed do something');
});

update answer (Full code example)

  • Remove "autoplay" from all videos.
  • jquery require
  • The first video play under init event
  • The prev video and current video play/stop under slideChange event
  • I use var inside Jquery selector to get the prev/current video - read more her: stackoverflow: How to use JavaScript variables in jQuery selectors?

var swiper = new Swiper('.swiper', {
  loop: true,
  pagination: {
    el: '.swiper-pagination',
    clickable: true
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },

  /* ON INIT AUTOPLAY THE FIRST VIDEO */
  on: {
    init: function () {
      console.log('swiper initialized');
      var currentVideo =  $("[data-swiper-slide-index=" + this.realIndex + "]").find("video");
      currentVideo.trigger('play');
    },
  },
});

/* GET ALL VIDEOS */
var sliderVideos = $(".swiper-slide video");

/* SWIPER API - Event will be fired after animation to other slide (next or previous) */
swiper.on('slideChange', function () {
  console.log('slide changed');
  /* stop all videos (currentTime buggy without this loop idea) */
  sliderVideos.each(function( index ) {
    this.currentTime = 0;
  });

  /* SWIPER GET CURRENT AND PREV SLIDE (AND VIDEO INSIDE) */
  var prevVideo =  $("[data-swiper-slide-index=" + this.previousIndex + "]").find("video");
  var currentVideo =  $("[data-swiper-slide-index=" + this.realIndex + "]").find("video");
  prevVideo.trigger('stop');
  currentVideo.trigger('play');
});
html, body {
  position: relative;
  height: 100%;
}
body {
  background: #eee;
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color:#000;
  margin: 0;
  padding: 0;
}
.swiper-container {
  width: 100%;
  height: 100%;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  /* Center slide text vertically */
  display: flex;
  justify-content: center;
  align-items: center;
}

/* reset list */
ul.swiper-wrapper{
  list-style-type: none;
  padding: 0px;
  margin: 0px;
}

h2{
  flex-basis: 100%;
}
<link rel="stylesheet" href="https://unpkg.com/swiper@8.0.7/swiper-bundle.min.css">

<!-- Slider main container -->
<div class="swiper">
  <!-- Additional required wrapper -->
  <ul class="swiper-wrapper">
    <!-- Slides -->
    <li class="swiper-slide">
      <div>
        <h3>Slide 1 - <small>Reset video and play when slide change</small></h3>
        <video 
               width="320" height="240" controls muted loop>
          <source src="https://ak2.picdn.net/shutterstock/videos/34123252/preview/stock-footage-coworkers-discussing-in-the-future-of-their-company-over-several-charts-and-graphs-business.mp4" type="video/mp4">
          Your browser does not support the video tag.
        </video>
      </div>
    </li>
    <li class="swiper-slide">
      <div>
        <h3>Slide 2 - <small>Reset video and play when slide change</small></h3>
        <video width="320" height="240" controls muted loop>
          <source src="https://ak1.picdn.net/shutterstock/videos/25348301/preview/stock-footage--business-new-customers-sale-and-people-concept-thousands-of-people-formed-qr-code-crowd-flight.webm" type="video/webm">
          Your browser does not support the video tag.
        </video>

      </div>
    </li>
    <li class="swiper-slide">
      <div>
        <h3>Slide 3 - <small>Reset video and play when slide change</small></h3>
        <video width="320" height="240" controls muted loop>
          <source src="https://ak4.picdn.net/shutterstock/videos/17795644/preview/stock-footage-receiving-email-e-mail-sign-on-holographic-screen-the-person-received-the-email-and-opens-it-with.mp4" type="video/mp4">
          Your browser does not support the video tag.
        </video>
      </div>
    </li>
  </ul>
  <!-- If we need pagination -->
  <div class="swiper-pagination"></div>

  <!-- If we need navigation buttons -->
  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>

  <!-- If we need scrollbar -->
  <div class="swiper-scrollbar"></div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://unpkg.com/swiper@8.0.7/swiper-bundle.min.js"></script>

codepen: https://codepen.io/ezra_siton/pen/povLPRY?editors=1111

Ezra Siton
  • 6,887
  • 2
  • 25
  • 37
  • This is so cool. But its possible to On the actual slide and Off the other slides (with currentTime = 0), just on slideChange? – Jose Jan 08 '20 at 18:12
  • If i refresh the page the video starts, why? – Jose Jan 09 '20 at 00:41
  • Yes! Just i did this, but if u slide so fast, sometimes something happens and multiple videos at the same time start. Obiously if i muted all of them its ok... but you know... i would make it perfect, i will post the code later. Maybe someone can help me. – Jose Jan 09 '20 at 14:06
  • I update the answer so check again (i don't see any issue or problem). – Ezra Siton Jan 09 '20 at 20:28
  • Thx so much for ur time. But anyway, if i didnt muted, they will reproduce all of them. – Jose Jan 09 '20 at 22:55
  • On chrome again no way to solve this (Chrome block autoplay videos). Not related to swiper or any other custom code or triggers. Anyway, some of your source videos without sound (You want sound?). I don't understand your issue - be more clear. – Ezra Siton Jan 09 '20 at 23:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205710/discussion-between-ezra-siton-and-jose). – Ezra Siton Jan 09 '20 at 23:12
  • I tried your code, but it's generating JS errors in some cases: Uncaught (in promise) DOMException: The play() request was interrupted by a new load request. I'm using loop settings, maybe that's the case. – Alexander Apr 30 '21 at 02:43