开发者

Stop a jQuery content slider when playing a YouTube video

I am trying to create a jQuery content slider that will stop when a YouTube video is played and resume when the video is stopped. Similar to the slider on taprootfoundation.org.

player.getPlayerState() in the YouTube JavaScript API returns the status of the player. A video that has not started should return a value of -1.

My script below uses return false; to stop the slideshow if getPlayerState() returns any value other than -1. Taking the bottom block in the code below (followed by //detect playing YouTube video) out, it works fine, but currently the "return false;" is firing regardless of the getPlayerState() value.

I used swfobject to embed the player, as recommended in the YouTube JavaScript API. Here's a screenshot showing the HTML of the embedded player.

I must be calling .getPlayerState() incorrectly, but I'm not sure how to correct it.

$(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);
});

});


Resolved it after much pulling of hair. I was having a scope issue when placing the YouTube API call inside the

jQuery $(document).ready(function(){
});

What I did was place said YouTube API call in the top of the jQuery document.

//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);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜