0

我正在寻找包含 YouTube 视频的幻灯片中的幻灯片。它应该:

  • 鼠标悬停暂停
  • 播放 YouTube 视频时暂停
  • 包含分页

我为幻灯片使用了 JQuery Cycle Plugin。 http://jquery.malsup.com/cycle/

并使用了 YouTube 的 iframe API https://developers.google.com/youtube/iframe_api_reference

幻灯片暂停和恢复正确播放/暂停视频;但是,如果您将鼠标移出视频幻灯片,它将恢复。

有没有办法使用 JQuery Cycle Plugin 来防止这种情况发生?

谢谢。

<!-- added to example from http://jquery.malsup.com/cycle/ -->

<html>
<head>
<title>JQuery Cycle Plugin - with YouTube</title>

<style type="text/css">
.slideshow { height: 232px; width: 232px; margin: auto }
.slideshow img { padding: 15px; border: 1px solid #ccc; background-color: #eee; }
/* slideshow pagination */
#nav_slideshow { width: 232px; margin: auto }
#nav_slideshow a { border: 1px solid #ccc; background: #fc0; text-decoration: none; margin: 0 5px; padding: 3px 5px;  }
#nav_slideshow a.activeSlide { background: #ea0 }
#nav_slideshow a:focus { outline: none; }
</style>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script type="text/javascript" src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js"></script>
    <script type="text/javascript">

      var theslideshow;
      $(document).ready(function() {
        theslideshow = $(".slideshow")
            .before('<div id="nav_slideshow">')
            .cycle({
                fx: 'fade',
                pause: true,  // pause on mouseover - will still fire resume 
                              // after cycle('pause') is called to play video 
                              // then if user leaves YouTube's iframe it resumes slideshow
                timeout: 2500,
                speed: 2500,
                sync: 1,
                pager:  '#nav_slideshow'
            });
      }); 

      // Access YouTube's APIs
      // https://developers.google.com/youtube/iframe_api_reference
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          events: {
            'onStateChange': onPlayerStateChange
          }
        });
      }

      // pause slide show when youtube video is playing <-- not working yet!
      // mouseout event triggers resume in Cycle's pause on mouseover
      function onPlayerStateChange(event){
        console.log(event.data);
        if(event.data == '1') {
            theslideshow.cycle('pause');
        } else if((event.data == '0') || (event.data == '2')) {
            theslideshow.cycle('resume');
        }
      }

    </script>
    </head>
    <body>
        <div class="slideshow">
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach1.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach2.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach3.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach4.jpg" width="200" height="200" />
            <img src="http://cloud.github.com/downloads/malsup/cycle/beach5.jpg" width="200" height="200" />
            <iframe id="player" width="560" height="315" src="http://www.youtube.com/embed/7CGQzQuH4X4?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
        </div>
    </body>
    </html>
4

1 回答 1

0

不是解决确切问题的方法 - 而是一个简单的替代方案。

由于您使用循环 - 升级到 cycle2 - http://jquery.malsup.com/cycle2/ - youtube 视频已经有一个 cycle2 扩展(检查下载部分)。

您可以使用 90% 的代码,因为语法基本相同 - 除了一些新选项(并且很少有重命名)。

于 2013-01-17T12:44:11.757 回答