javascriptowl-carousel

autoplay owl carousel when video ends


I have a problem with Owl Carousel, I would like to create a carousel consisting of both images and video. I have problems with the video, I would like the scrolling to the next element to happen when the video is finished (each video has its own duration) now it always does it the same with a set time. Thank you all for your help.

var videoSlider = $('.owl-carousel');
videoSlider.owlCarousel({
  loop: true,
  margin: 0,
  nav: true,
  dots: true,
  autoplay: true,
  items: 1
});

videoSlider.on('translate.owl.carousel', function (e) {
  $('.owl-item .item video').each(function () {
    // pause playing video - after sliding
    $(this).get(0).pause();
  });
});

videoSlider.on('translated.owl.carousel', function (e) {
  // check: does the slide have a video?
  if ($('.owl-item.active').find('video').length !== 0) {
    // play video in active slide
    $('.owl-item.active .item video').get(0).play();
  }
});
/* ---------------- */
/* HTML Reset Stuff */
/* ---------------- */
* {
  box-sizing: border-box;
  outline: none;
}

body, html {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  font-family: Tahoma;
  background: #222;
}

h1 {
  color: white;
  margin: 30px;
  font-size: 25px;
}

button span {
  color: white;
}

video {
  max-width: 100%;
}

/* --------------- */
/* Content Wrapper */
/* --------------- */
.wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  transition: all .6s;
  position: relative;
  z-index: 1;
  overflow: hidden;
}

/* ---------------------- */
/* OWL Active Slider Mask */
/* ---------------------- */
.slider-mask {
  position: absolute;
  width: 100%;
  max-width: 400px;
  height: 220px;
  top: 0;
  left: 50%;
  z-index: 2;
  pointer-events: none;
  margin: 0 auto;
  transform: translateX(-50%);
}

.slider-mask::before,
.slider-mask::after {
  position: absolute;
  z-index: 1;
  left: 100%;
  width: 100vw;
  height: 100%;
  background: rgba(34,34,34,.8);
  display: block;
  content: ' ';
}

.slider-mask::after {
  left: auto;
  right: 100%;
}

/* ----------------- */
/* Owl Styling Stuff */
/* ----------------- */
.owl-carousel {
  width: 100%;
  max-width: 400px;
  position: relative;
  z-index: 1;
}

.owl-stage-outer {
  overflow: visible !important;
}

.owl-item {
  width: 100%;
  max-width: 400px;
  height: 220px;
  background-color: #efefef;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  overflow: hidden;
}

.owl-nav {
  font-size: 60px;
}

.owl-next {
  float: right;
}

.owl-dots {
  text-align: center;
  width: 80%;
  margin: -40px auto 0;
}

.owl-dot span{
  background: white;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  display: block;
  margin: 0 4px;
}

.owl-dot.active span{
  background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.4.8/plyr.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>


<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.4.8/plyr.css" rel="stylesheet"/>
<header>
  <h1>Owl Slider - Play / Stop / Continue HTML5 Video</h1>
</header>

<main class="wrapper">

  <div class="slider-mask"></div>
  <div class="owl-carousel owl-theme">

    <div class="item"><h4>1</h4></div>
    <div class="item">
      <video class="video-item" muted loop>
        <source src="https://ak9.picdn.net/shutterstock/videos/5007479/preview/stock-footage-christmas-snow-globe-snowflake-with-snowfall-on-blue-background.webm" type="video/webm">
        <source src="https://ak9.picdn.net/shutterstock/videos/5007479/preview/stock-footage-christmas-snow-globe-snowflake-with-snowfall-on-blue-background.mp4" type="video/mp4">
      </video>
    </div>
    <div class="item"><h4>3</h4></div>
    <div class="item">
      <video class="video-item" muted loop>
        <source src="https://ak4.picdn.net/shutterstock/videos/1014705914/preview/stock-footage--rd-happy-anniversary-text-greeting-and-wishes-card-made-from-glitter-particles-from-golden.mp4" type="video/mp4">
      </video>
    </div>
    <div class="item"><h4>5</h4></div>
    <div class="item">
      <video class="video-item" muted loop>
        <source src="https://ak9.picdn.net/shutterstock/videos/1027722329/preview/stock-footage-ramadan-candle-lanterns-are-hanging-on-dawn-sky-background-with-glowing-stars-and-a-crescent-there.webm" type="video/webm">
        <source src="https://ak9.picdn.net/shutterstock/videos/1027722329/preview/stock-footage-ramadan-candle-lanterns-are-hanging-on-dawn-sky-background-with-glowing-stars-and-a-crescent-there.mp4" type="video/mp4">
      </video>
    </div>
  </div>
  
</main>


Solution

  • You should detect event of video ended, see Detect when an HTML5 video finishes

    Then you should manually call an owl-carousel "next slide" method.

    I set autoplay: false but you would need to add a timeout to auto play the images.

    var videoSlider = $('.owl-carousel');
    videoSlider.owlCarousel({
      loop: true,
      margin: 0,
      nav: true,
      dots: true,
      autoplay: false,
      items: 1
    });
    
    videoSlider.on('translate.owl.carousel', function(e) {
      $('.owl-item .item video').each(function() {
        // pause playing video - after sliding
        $(this).get(0).pause();
      });
    });
    
    videoSlider.on('translated.owl.carousel', function(e) {
      // check: does the slide have a video?
      if ($('.owl-item.active').find('video').length !== 0) {
        // play video in active slide
        $('.owl-item.active .item video').get(0).play();
    
    
      }
    });
    
    
    $(".video-item").on('ended', myHandler)
    
    function myHandler(ev) {
      // there is no method 'next'
      // so we will fake a click
      $('.owl-next').trigger('click');
    }
    /* ---------------- */
    
    
    /* HTML Reset Stuff */
    
    
    /* ---------------- */
    
    * {
      box-sizing: border-box;
      outline: none;
    }
    
    body,
    html {
      height: 100%;
      width: 100%;
      padding: 0;
      margin: 0;
      font-family: Tahoma;
      background: #222;
    }
    
    h1 {
      color: white;
      margin: 30px;
      font-size: 25px;
    }
    
    button span {
      color: white;
    }
    
    video {
      max-width: 100%;
    }
    
    
    /* --------------- */
    
    
    /* Content Wrapper */
    
    
    /* --------------- */
    
    .wrapper {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: flex-start;
      justify-content: center;
      transition: all .6s;
      position: relative;
      z-index: 1;
      overflow: hidden;
    }
    
    
    /* ---------------------- */
    
    
    /* OWL Active Slider Mask */
    
    
    /* ---------------------- */
    
    .slider-mask {
      position: absolute;
      width: 100%;
      max-width: 400px;
      height: 220px;
      top: 0;
      left: 50%;
      z-index: 2;
      pointer-events: none;
      margin: 0 auto;
      transform: translateX(-50%);
    }
    
    .slider-mask::before,
    .slider-mask::after {
      position: absolute;
      z-index: 1;
      left: 100%;
      width: 100vw;
      height: 100%;
      background: rgba(34, 34, 34, .8);
      display: block;
      content: ' ';
    }
    
    .slider-mask::after {
      left: auto;
      right: 100%;
    }
    
    
    /* ----------------- */
    
    
    /* Owl Styling Stuff */
    
    
    /* ----------------- */
    
    .owl-carousel {
      width: 100%;
      max-width: 400px;
      position: relative;
      z-index: 1;
    }
    
    .owl-stage-outer {
      overflow: visible !important;
    }
    
    .owl-item {
      width: 100%;
      max-width: 400px;
      height: 220px;
      background-color: #efefef;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 30px;
      overflow: hidden;
    }
    
    .owl-nav {
      font-size: 60px;
    }
    
    .owl-next {
      float: right;
    }
    
    .owl-dots {
      text-align: center;
      width: 80%;
      margin: -40px auto 0;
    }
    
    .owl-dot span {
      background: white;
      width: 15px;
      height: 15px;
      border-radius: 50%;
      display: block;
      margin: 0 4px;
    }
    
    .owl-dot.active span {
      background: red;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.4.8/plyr.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script>
    
    
    <link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.4.8/plyr.css" rel="stylesheet" />
    <header>
      <h1>Owl Slider - Play / Stop / Continue HTML5 Video</h1>
    </header>
    
    <main class="wrapper">
    
      <div class="slider-mask"></div>
      <div class="owl-carousel owl-theme">
    
        <div class="item">
          <h4>1</h4>
        </div>
        <div class="item">
          <video class="video-item" muted controls>
            <source src="https://ak9.picdn.net/shutterstock/videos/5007479/preview/stock-footage-christmas-snow-globe-snowflake-with-snowfall-on-blue-background.webm" type="video/webm">
            <source src="https://ak9.picdn.net/shutterstock/videos/5007479/preview/stock-footage-christmas-snow-globe-snowflake-with-snowfall-on-blue-background.mp4" type="video/mp4">
          </video>
        </div>
        <div class="item">
          <h4>3</h4>
        </div>
        <div class="item">
          <video class="video-item" muted controls>
            <source src="https://ak4.picdn.net/shutterstock/videos/1014705914/preview/stock-footage--rd-happy-anniversary-text-greeting-and-wishes-card-made-from-glitter-particles-from-golden.mp4" type="video/mp4">
          </video>
        </div>
        <div class="item">
          <h4>5</h4>
        </div>
        <div class="item">
          <video class="video-item" muted controls>
            <source src="https://ak9.picdn.net/shutterstock/videos/1027722329/preview/stock-footage-ramadan-candle-lanterns-are-hanging-on-dawn-sky-background-with-glowing-stars-and-a-crescent-there.webm" type="video/webm">
            <source src="https://ak9.picdn.net/shutterstock/videos/1027722329/preview/stock-footage-ramadan-candle-lanterns-are-hanging-on-dawn-sky-background-with-glowing-stars-and-a-crescent-there.mp4" type="video/mp4">
          </video>
        </div>
      </div>
    
    </main>