Slick.js and html5 video autoplay and pause on video

南笙酒味 提交于 2019-11-30 09:11:39
Asaf David

Yes it can be done using JavaScript.
When the video slide becomes the currentSlide you need to:

  1. pause the slider
  2. play the video

You can do this using the slick.js event afterChange:

$('.sliderMain').on('afterChange', function(event, slick, currentSlide){
  if (currentSlide == 5) {
    $('.sliderMain').slick('slickPause');
    myVideo.play();
  }
});

You will also need to add an event listener for when to video ends in order to order the slider to continue. You can do so like this:

document.getElementById('myVideo').addEventListener('ended',myHandler,false);
function myHandler(e) {
    $('.sliderMain').slick('slickPlay');
}

If you're having trouble with this, please add a JSFiddle and I'll try help you there.

Edit: here's a working JSFiddle

$(document).ready(function() {
  $('.sliderMain').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    arrows: false,
    fade: true,
    asNavFor: '.sliderSidebar',
    autoplay: true,
    autoplaySpeed: 3000
  });
  $('.sliderSidebar').slick({
    slidesToShow: 5,
    slidesToScroll: 1,
    asNavFor: '.sliderMain',
    dots: false,
    centerMode: false,
    focusOnSelect: true,
    vertical: true,
    arrows: false
  });
  $('.sliderMain').on('afterChange', function(event, slick, currentSlide) {
    if (currentSlide == 5) {
      $('.sliderMain').slick('slickPause');
      theVideo.play();
    }
  });

  document.getElementById('theVideo').addEventListener('ended', myHandler, false);

  function myHandler(e) {
    $('.sliderMain').slick('slickPlay');
  }
});
#slideBox {
  width: 1300px;
  max-height: 500px;
  overflow: hidden;
  margin: 1% auto;
  position: relative;
  top: 1em;
  background-color: #54585A;
  border-radius: .3em;
}
video {
  background-color: black;
}
#slideBox .slick-vertical .slick-slide {
  border: none;
}
.sliderSidebar {
  width: 200px;
  height: 500px;
  float: left;
}
#slideBox .slick-vertical .slick-slide {
  border: none;
}
.sliderMain {
  width: 900px;
  height: 500px;
  position: relative;
  float: left;
}
<div id="slideBox">
  <!--Sidebar-->
  <div class="sliderSidebar">
    <div><img src="http://placehold.it/200x100"></div>
    <div><img src="http://placehold.it/200x100"></div>
    <div><img src="http://placehold.it/200x100"></div>
    <div><img src="http://placehold.it/200x100"></div>
    <div><img src="http://placehold.it/200x100"></div>
    <div><img src="http://placehold.it/200x100/C8102E/FFFFFFF?text=Video" /></div>
  </div>

  <div id="main-image" class="sliderMain">
    <div><img src="http://placehold.it/900x500"></div>
    <div><img src="http://placehold.it/900x500"></div>
    <div><img src="http://placehold.it/900x500"></div>
    <div><img src="http://placehold.it/900x500"></div>
    <div><img src="http://placehold.it/900x500"></div>
    <div id="slide-video">
      <video width="900px" id="theVideo">
        <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" />
      </video>
    </div>
  </div>
</div>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.7.1/slick.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.7.1/slick-theme.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.7.1/slick.min.js"></script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!