播放 YouTube 视频时停止 jQuery 内容滑块
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过多次拉扯头发后解决了。 时,我遇到了范围问题。
当我将 YouTube API 调用放置在 jQuery 文档顶部
Resolved it after much pulling of hair. I was having a scope issue when placing the YouTube API call inside the
What I did was place said YouTube API call in the top of the jQuery document.