简体   繁体   English

在光滑的轮播中为视频添加自动播放

[英]add autoplay for video in slick carousel

I use slick carousel http://kenwheeler.github.io/slick/ on my page and now I need to add video with autoplay in it.我在我的页面上使用 slick carousel http://kenwheeler.github.io/slick/ ,现在我需要添加带有自动播放功能的视频。
I use this code我用这个代码
HTML HTML

<div class="main-slider" id="main-slider">
            <div>
                <div class="video-wrapper">
                    <video autoplay loop class="pageBackground-video">
                        <source src="/Content/video/332145276.mp4" type="video/mp4">
                    </video>
                </div>
            </div>
            <div>
                <div class="video-wrapper">
                    <video autoplay loop class="pageBackground-video">
                        <source src="/Content/video/332145276.mp4" type="video/mp4">
                    </video>
                </div>
            </div>          
        </div>

and script和脚本

$('#main-slider').slick({
      slidesToShow: 1,
      slidesToScroll: 1,
      autoplay: true,
      autoplaySpeed: 30000,
      dots: true,
      infinite: true,
      adaptiveHeight: true,
      arrows: false
  });

but autoplay doesn't work.但自动播放不起作用。 Is there any way to make autoplay work?有没有办法让自动播放工作?

upd I tried to use更新我尝试使用

$('#main-slider').on('afterChange', function(event, slick, currentSlide, nextSlide){
  var video = currentSlide.children('video').get(0).play();
});

but I get an error Uncaught TypeError: currentSlide.children is not a function because currentSlide it's just a number (0,1,etc).但我收到一个错误Uncaught TypeError: currentSlide.children is not a function因为currentSlide它只是一个数字(0,1 等)。 How to get current element?如何获取当前元素?

upd2 I used this code and it works upd2我使用了这个代码并且它有效

$('#main-slider').on('afterChange', function(event, slick, currentSlide, nextSlide){
    $('#main-slider .slick-slide').find('video').get(0).pause();
    var video = $('#main-slider .slick-active').find('video').get(0).play();
});

but now autoplay work for all video perfectly but only after first slick changes.但是现在自动播放可以完美地适用于所有视频,但只有在第一次光滑的更改之后。 How to make it work for the first video after page only loaded.如何让它在页面加载后的第一个视频中工作。

upd3 I used this code upd3我用了这个代码

 var video = $('#main-slider .slick-active').find('video').get(0).play();

  $('#main-slider').on('afterChange', function(event, slick, currentSlide, nextSlide){
    $('#main-slider .slick-slide').find('video').get(0).pause();
    var video = $('#main-slider .slick-active').find('video').get(0).play();
});

and now all work as I want but I'm afraid my code is ugly (现在一切都如我所愿,但恐怕我的代码很难看(

You can use functions from the slick docs.您可以使用 slick 文档中的函数。

// On slide change, pause all videos
$('#main-slider').on('beforeChange', function(event, slick, currentSlide, nextSlide){
  $('video').each(function() {
    $(this).get(0).pause();
  });
});

// On slide chnage, play a video inside the current slide
$('#main-slider').on('afterChange', function(event, slick, currentSlide, nextSlide){
  if( $('.project-slide.slick-current').find('video').length !== 0) {
    $("#main-slider .slick-current video")[0].play();
  }
});

this line of code这行代码

$('#main-slider .slick-slide').find('video').get(0).pause(); $('#main-slider .slick-slide').find('video').get(0).pause();

inside afterChange call back function will pause all the video's present in the slide container irrespective of the video playing or not.无论视频是否播放,afterChange 回调函数都将暂停幻灯片容器中的所有视频。

If there are 100 slides, it pauses all 100 videos though they are not playing.如果有 100 张幻灯片,它会暂停所有 100 个视频,尽管它们没有播放。 Its better you find the previous slide and pause only the video in that slide最好找到上一张幻灯片并仅暂停该幻灯片中的视频

您可以从视频标签中获取 id 并

$("#videotagId").pause();

You can update your following codes.您可以更新以下代码。 It will work.它会起作用。


$('#main-slider').on('afterChange', function(event, slick, currentSlide, nextSlide){
  var video = $($(currentSlide.children('video')).get(0)).play();
});


get() method doesn't return jquery object. get() 方法不返回 jquery 对象。 It returns Javascript object so in order to use any jquery function you have to make it a jquery object by wrapping it inside the '$'.它返回 Javascript 对象,因此为了使用任何 jquery 函数,您必须通过将其包装在“$”中来使其成为 jquery 对象。

For the first video, you can add muted to the video tag to autoplay in all browsers.对于第一个视频,您可以在视频标签中添加muted以在所有浏览器中autoplay

<video autoplay muted></video>

I know this doesn't help if your video has sound, but having sound immediately play on the initial page load is probably not preferred anyway.我知道如果您的视频有声音,这无济于事,但是在初始页面加载时立即播放声音可能不是首选。

For subsequent videos, you can use the afterChange event:对于后续视频,您可以使用afterChange事件:

$('#your-slider-id').on('afterChange', function(event, slick, currentSlide, nextSlide){
  $('video').each(function() {
    $(this).get(0).pause();
  });
  if( $('.slick-current').find('video').length !== 0 ) {
    $(this).find('video').get(0).play();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM