Pause all Vimeo videos with API?

2019-05-27 12:16发布

I have hit a wall and cannot figure out how to make this work. With Vimeo's Advanced API I am pulling in all videos from an account with thumbnails. When clicking on a thumbnail the video is shown above and clicking on a different thumbnail hides the currently shown video. The problem is that the video continues to play even when it is hidden. I have spent a few hours looking at the API and I cannot get it to pause when hidden.

var iframe = $('.video')[0];

  $('.thumbnail a').click(function(e) {

  $f(iframe).api('pause');

  });

The above code pauses only the first video. If I change the number [0] to [1], then the second video pauses when you click on another thumbnail. Does anyone have any thoughts? I am using froogaloop in the above code.

2条回答
等我变得足够好
2楼-- · 2019-05-27 12:27

I used this with jQuery:

<script type="text/javascript" src="http://a.vimeocdn.com/js/froogaloop2.min.js"></script>
<script type="text/javascript">// <![CDATA[
function pauseAll() {
    $('iframe[src*="vimeo.com"]').each(function () {
        $f(this).api('pause');
    });
}
// ]]></script>

use it on an onclick.

<a href="#" onclick="pauseAll(); return false;">pause all</a>
查看更多
爷、活的狠高调
3楼-- · 2019-05-27 12:33

You can use this code to pause all YouTube and Vimeo videos on the page:

$('iframe').each(function() {
  this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  this.contentWindow.postMessage('{"method":"pause","value":""}', '*');
});
查看更多
登录 后发表回答