播放 YouTube 视频时停止 jQuery 内容滑块

发布于 2024-10-05 14:10:27 字数 2923 浏览 2 评论 0原文

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

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

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

我按照 YouTube JavaScript API 中的建议使用 swfobject 来嵌入播放器。这是显示嵌入式播放器 HTML 的屏幕截图

我一定是错误地调用了 .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);
});

});

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

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2024-10-12 14:10:27

经过多次拉扯头发后解决了。 时,我遇到了范围问题。

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

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);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文