【问题标题】:Stop a jQuery content slider when playing a YouTube video播放 YouTube 视频时停止 jQuery 内容滑块
【发布时间】:2011-05-18 12:52:02
【问题描述】:

我正在尝试创建一个 jQuery 内容滑块,该滑块将在播放 YouTube 视频时停止,并在视频停止时恢复。类似于taprootfoundation.org 上的滑块。

YouTube JavaScript API 中的

player.getPlayerState() 返回播放器的状态。尚未开始的视频应返回值 -1。

我下面的脚本使用 return false;如果 getPlayerState() 返回 -1 以外的任何值,则停止幻灯片放映。取出下面代码中的底部块(后跟//检测播放YouTube视频),它工作正常,但目前“return false;”无论 getPlayerState() 值如何,都会触发。

按照 YouTube JavaScript API 中的建议,我使用 swfobject 嵌入播放器。这是screenshot showing the HTML of the embedded player

我一定是错误地调用了 .getPlayerState(),但我不知道如何纠正它。

$(document).ready(function(){
//initial setup
    //adds class "last" to last link-tab item
    $('.link-tab:last').addClass('last');

    //moves summary elements down out of view
    $('.rotate-image > div').children('.summary').css({bottom:-150});

//initial variables
    var captionAnimationTime = 300;
    var captionPauseTime = 4800;
    var slideTime = 6000;

//detect playing YouTube video
/*  window.setTimeout(function(){
            video = document.getElementById("boundless-playground-video");
    }
    , captionAnimationTime);
*/
//main function
slideSwitch = function(){
    var $active = $('.rotate-image > div.active');

    if ($active.length == 0) $active = $('.rotate-image > div:last');

    var $next = $active.next().length ? $active.next() : $('.rotate-image > div:first');

    //sets indexCurrent variable as the index of the next active banner item in the slideshow
    var indexCurrent = $next.prevAll().length;

    //removes "active" class from link-tab items that already have it
    $('.link-tab.active').removeClass('active');
    //gives class "active" to next link-tab item
    $('.link-tab').eq(indexCurrent).addClass('active');

    $active.addClass('last-active');

    //function to slide down the caption
    captionDown = function(){
        $next.children('.summary').animate({bottom:-150}, captionAnimationTime);
    }

    $next.css({opacity:0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 700, function(){
            //remove classes from the previously active item
            $active.removeClass('active last-active');

            //animates slide of summary element
            $next.children('.summary').animate({bottom: 0}, captionAnimationTime);
            window.setTimeout(captionDown, captionPauseTime);
        });

    //detect playing YouTube video - currently stopping regardless of player state
    video = document.getElementById("boundless-playground-video");
    if(video.getPlayerState() != -1){
        return false;
    }
};

//run the function once on document ready to show first slide
slideSwitch();

$(function(){
    setInterval( "slideSwitch()", slideTime);
});

});

【问题讨论】:

  • 我认为您需要显示控制滑块的代码。我们可以就 API 调用提供建议,但不知道您是如何准确实现滑块的。除非代码与给出的链接相同。
  • 将停止 jQuery 滑块的函数(使用 jQuery 滑块具有的任何方法,例如 $('#slider').stop(); )绑定到 youtube 播放器的“onStateChange”事件(参见API)。
  • 用我控制滑块的代码更新了问题。

标签: jquery video youtube youtube-api


【解决方案1】:

拉了很多头发后解决了。将 YouTube API 调用放在

中时,我遇到了范围问题
jQuery $(document).ready(function(){
});

我所做的就是将 YouTube API 调用放在 jQuery 文档的顶部。

//playerState variable shows status of YouTube video. 
//see http://code.google.com/apis/youtube/youtube_player_demo.html
//currently hardcoded with id of video
function onYouTubePlayerReady(){
  ytplayer = document.getElementById('boundless-playground-video');
  ytplayer.addEventListener('onStateChange','onytplayerStateChange');
}
function onytplayerStateChange(newState) {
  playerState = newState;
}

$(document).ready(function(){
//insert other stuff here
if (typeof (playerState) == 'undefined'){
window.playerState = 5;
}
alert(window.playerState);
});

【讨论】:

    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多