首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当YouTube幻灯片不可见时,停止播放FlexSlider视频

当YouTube幻灯片不可见时,停止播放FlexSlider视频
EN

Stack Overflow用户
提问于 2019-02-23 01:37:01
回答 1查看 963关注 0票数 1

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

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

HTML:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
$(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

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-23 04:53:12

在帮助下,通过添加一些js就可以解决这个问题。

代码语言:javascript
复制
$(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":""}', '*')
  });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54832387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档