【问题标题】:Stop YouTube Video from Playing when FlexSlider Slide is out of view当 FlexSlider Slide 不在视野范围内时,停止播放 YouTube 视频
【发布时间】:2019-07-16 20:39:42
【问题描述】:

我正在尝试将一些 YouTube 嵌入视频放入 FlexSlider Carousel / Slider。我遇到的问题是,如果幻灯片不在视图中,视频会停止。目前,如果您播放视频并选择一个新视频,则前一个视频会继续播放。

预期:更换幻灯片时,停止视频。 实际:在幻灯片更改时,视频会继续播放。

HTML:

<section class="videolist">
  <div class="container">
    <div id="slider" class="flexslider">
      <ul class="slides">
        <li>
          <iframe width="560" height="315" src="https://www.youtube.com/embed/xcJtL7QggTI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        </li>
        <li><iframe width="560" height="315" src="https://www.youtube.com/embed/a3ICNMQW7Ok" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        </li>
        <li> <iframe width="560" height="315" src="https://www.youtube.com/embed/S-thTTqefls" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </li>
        <li> <iframe width="560" height="315" src="https://www.youtube.com/embed/Bey4XXJAqS8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </li>

      </ul>
    </div>
    <div id="carousel" class="flexslider">
      <ul class="slides">
        <li> <img src="https://quidsup.net/wallpaper/abstract/the-cube-1920x1080-wallpaper-5189.jpg" alt="This is the alt text of the video"> </li>
        <li> <img src="https://www.setaswall.com/wp-content/uploads/2017/08/Spaceship-Background-20-1920x1080.jpg" alt="This is the alt text of the video"> </li>
        <li> <img src="https://wallpapercave.com/wp/trlzN6b.jpg" alt="This is the alt text of the video"> </li>
        <li> <img src="https://i.redd.it/vjcavsc1lcs11.jpg" alt="This is the alt text of the video"> </li>

      </ul>
    </div>
  </div>
</section>

JS:

$(window).load(function() {  
  $('#carousel').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    itemWidth: 210,
    itemMargin: 5,
    asNavFor: '#slider'
  }); 
  $('#slider').flexslider({
    animation: "slide",
    controlNav: false,
    animationLoop: false,
    slideshow: false,
    sync: "#carousel"
  });
});

我这里有一个密码笔YouTube FlexSlider Carousel | Codepen

有什么想法可以做到这一点吗?

谢谢

【问题讨论】:

    标签: javascript youtube flexslider


    【解决方案1】:

    在帮助下,加了一些js就搞定了。

    $(window).load(function() {  
      $('#carousel').flexslider({
        animation: "slide",
        controlNav: false,
        animationLoop: false,
        slideshow: false,
        itemWidth: 210,
        itemMargin: 5,
        asNavFor: '#slider',
        start: function() {
          $('#carousel li').on('click', function(e){
            stopVideos();
          });
          $('#carousel li').on('click', function(e){
            stopVideos();
          });
        }
      }); 
      $('#slider').flexslider({
        animation: "slide",
        controlNav: false,
        animationLoop: false,
        slideshow: false,
        sync: "#carousel",
        start: function() {
          $('.flex-next').on('click', function(e){
            stopVideos();
          });
          $('.flex-prev').on('click', function(e){
            stopVideos();
          });
        }
      });
    });
    
    function stopVideos() {
      $('.iframe-vid').each(function() {
      this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*')
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      相关资源
      最近更新 更多