最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - BXSlider Pause Youtube Video When Switching Slides - Stack Overflow

programmeradmin0浏览0评论

I have the following example of BXSlider, where I have 4 slides each having a Youtube video. The problem is that when I play a video and go to the next slide, it keeps on playing:

/

$(document).ready(function () {
$('.bxslider').bxSlider();
});

Can someone help out to make the youtube video pause when going to the next slide?

I have the following example of BXSlider, where I have 4 slides each having a Youtube video. The problem is that when I play a video and go to the next slide, it keeps on playing:

http://jsfiddle/isherwood/XWL9Y/

$(document).ready(function () {
$('.bxslider').bxSlider();
});

Can someone help out to make the youtube video pause when going to the next slide?

Share Improve this question asked Dec 16, 2014 at 8:37 Jab018Jab018 411 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3
var slider = $("#gallery").bxSlider({
    adaptiveHeight:true,
    auto:true, 
    autoStart:true, 
    autoControls:true, 
    video:true,
    onSlideAfter: function(slide){
       if (slide.find("iframe:first").length) {
           slider.stopAuto();
       }
    }
});

You might also be able to use something like slide.find("div.fluid-width-video-wrapper:first") if you use iframes for other things in the in the slider.

Source

----Update----

var slider = $('.bxslider').bxSlider({
  onSlideAfter: function(slide, oldindex, currentSlide){
    oldSlide = $( '.bxslider > li:nth-child(' + (oldindex+1) + ')');
      youtubeVideo = oldSlide.find('iframe[src*=youtube]');
      if ( youtubeVideo.length ) {
        youtubeVideo.get(0).contentWindow.postMessage('{"event":"mand","func":"pauseVideo","args":""}','*');
      }
  }
});

Source

----Edit----

Also make sure to add ?enablejsapi=true to the the iframe src, otherwise the above JavaScript will not work (source).

After long search i found the solution. Try this:

onSlideAfter: function(){
    jQuery("iframe").each(function() { 
        var src= jQuery(this).attr('src');
        jQuery(this).attr('src',src);  
    });
}

This will stop all videos. It is basically resetting the video urls.

发布评论

评论列表(0)

  1. 暂无评论