Slick.js 和 html5 视频自动播放和暂停

Slick.js and html5 video autoplay and pause on video

是否可以将 slick.js 与 html5 自托管视频一起使用,无需用户交互即可播放和暂停视频?

我目前有以下代码,它有一个带有垂直光滑幻灯片的双滑块和一个主要部分,其中大图像和视频将出现并自动滚动。这将在电视上进行,因此不会有用户交互。

WelcomeTV Screen Site

如何让包含视频的幻灯片在出现后完全播放,并在播放结束后继续播放并无限期重复。视频可能包含各种长度,因此需要动态检测长度。

我尝试在 this question 上实现代码,但是我的示例无法正常工作。

    <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 autoplay loop width="900px">
                <source src="video/SampleVideo.mp4" />
            </video>
        </div>
    </div>

    <script type="text/javascript">

    $(document).ready(function(){
        $('.sliderMain').slick({
            slidesToShow: 1,
            slidesToScroll: 1,
            arrows: false,
            fade: true,
            asNavFor: '.sliderSidebar',
            autoplay: true,
            autoplaySpeed: 3000,
            onAfterChange : function() {
                player.stopVideo();
            }
        });
        $('.sliderSidebar').slick({
            slidesToShow: 5,
            slidesToScroll: 1,
            asNavFor: '.sliderMain',
            dots: false,
            centerMode: false,
            focusOnSelect: true,
            vertical: true,
            arrows: false
        });
        var video = $('.sliderMain .slick-active').find('video').get(0).play();

        $('.sliderMain').on('afterChange', function(event, slick, currentSlide, nextSlide){
        $('.sliderMain .slick-slide').find('video').get(0).pause();
        var video = $('.sliderMain .slick-active').find('video').get(0).play();
        });
    });
</script>

Demo

是的,可以使用 JavaScript。
当视频幻灯片变成 currentSlide 您需要:

  1. 暂停滑块
  2. 播放视频

您可以使用 slick.js 事件 afterChange:

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

您还需要为视频何时结束添加一个事件侦听器,以便让滑块继续播放。您可以这样做:

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

如果您在这方面遇到问题,请添加一个 JSFiddle,我会尽力帮助您。

编辑:这是一个有效的 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="https://via.placeholder.com/200x100"></div>
    <div><img src="https://via.placeholder.com/200x100"></div>
    <div><img src="https://via.placeholder.com/200x100"></div>
    <div><img src="https://via.placeholder.com/200x100"></div>
    <div><img src="https://via.placeholder.com/200x100"></div>
    <div><img src="https://via.placeholder.com/200x100/C8102E/FFFFFFF?text=Video" /></div>
  </div>

  <div id="main-image" class="sliderMain">
    <div><img src="https://via.placeholder.com/900x500"></div>
    <div><img src="https://via.placeholder.com/900x500"></div>
    <div><img src="https://via.placeholder.com/900x500"></div>
    <div><img src="https://via.placeholder.com/900x500"></div>
    <div><img src="https://via.placeholder.com/900x500"></div>
    <div id="slide-video">
      <video width="900px" id="theVideo">
        <source src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.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>