[英]How can I fire only once for each comment in this case?
我制作了动画,其中所有加载的注释都出现并且在下一个注释出现时消失。
我做了演示,所以请检查一下!
问题是timeupdate
每秒大约工作10次。
因此,对于每个评论,动画都会被触发10次:(
请查看DEMO,您会发现它看起来很奇怪。
我该如何处理? 任何人都可以在JSfiddle中修改我的代码。
这些是我的代码。
JavaScript的
jQuery(document).ready(function () {
$('#video').on('timeupdate',function(e){
showComments(this.currentTime);
});
});
var comments = [{'time':'10','message':'hello! 10 secs has past'},{'time':'15','message':'hello! 15 secs has past'},{'time':'5','message':'hello! 5 secs has past'},{'time':'20','message':'hello! 20 secs has past'}];
function showComments(time){
var comments = findComments(time);
$.each(comments,function(i,comment){
$('.newsticker p').animate({"marginLeft":"400px","opacity":".0"}, 600).fadeOut(100);
$('.newsticker').append("<p style='margin-left:400px;opacity:0'>"+comment.message+"</p>");
$('.newsticker p').animate({"marginLeft":"0px","opacity":"1"}, 600);
});
}
function findComments(time){
return $.grep(comments, function(item){
return item.time == time.toFixed();
});
}
HTML
<body>
<div class="newsticker">
</div>
<br />
<br />
<video id="video" controls="controls" autoplay="autoplay" name="media"><source src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4"></video>
</body>
CSS
div.newsticker{
border:1px solid #666666;
width:100%;
height:50px;
}
.newsticker p{
height:20px;
width:150px;
float:left;
position:absolute;
}
将显示的标记添加到注释中,然后检查是否需要更新。
请注意,我还重命名了一些局部变量,以防止与父作用域的冲突(不会影响代码,但是firebug对于向我展示正确的东西有点可笑)
更新
出于清洁的考虑,将其转换为一个jquery插件,还将检查时间作为@BMH的答案(因此,请随时将其标记为已接受)。 停止了它,如果有多个时间戳,则显示一个时间戳的所有注释;如果重新缠绕到上一个时间,它将重新显示注释:
jQuery.fn.videoComments = function(options){
var defaults = {
"comments" : [
{'time':'10','message':'hello! 10 secs has past'},
{'time':'15','message':'hello! 15 secs has past'},
{'time':'5','message':'hello! 5 secs has past'},
{'time':'20','message':'hello! 20 secs has past'}
],
};
var options = $.extend(defaults, options);
if(!options.commentHolder){
throw "videoComments requires a commentHolder to put the comments in";
}
function setComment(message){
$commentContainer.css({
"marginLeft" : "400px",
"opacity": "0"
}).html(message);
};
var $commentContainer = $("<p></p>");
setComment("");
$(options.commentHolder).append($commentContainer);
function showComments(time){
var foundComments = findComments(time);
$.each(foundComments,function(i,comment){
$commentContainer.animate({"marginLeft":"400px","opacity":".0"}, 600);
setComment(comment.message);
$commentContainer.animate({"marginLeft":"0px","opacity":"1"}, 600);
});
};
function findComments(timeToFind){
var matchingComments = $.grep(options.comments, function(item){
return (item.time == timeToFind);
});
return matchingComments;
};
return $(this).each(function(){
var currentTime = -1;
$(this).on("timeupdate", function(e) {
var localTime = this.currentTime.toFixed();
if(currentTime != localTime){
currentTime = localTime;
showComments(currentTime);
}
});
});
};
$("#video").videoComments({
"commentHolder" : $(".newsticker")
})
我将标志放在ready函数中:
http://jsfiddle.net/b_m_h/9zqhF/11/
jQuery(document).ready(function () {
var fixedTime = 0;
$('#video').on('timeupdate',function(e){
if(this.currentTime.toFixed() != fixedTime){
showComments(fixedTime);
fixedTime = this.currentTime.toFixed()
}
});
});
var comments = [{'time':'10','message':'hello! 10 secs has past'},{'time':'10','message':'hello! part-2 10 secs has past'},{'time':'15','message':'hello! 15 secs has past'},{'time':'5','message':'hello! 5 secs has past'},{'time':'20','message':'hello! 20 secs has past'}];
function showComments(time){
var coms = findComments(time);
if(coms[0]){
$('.newsticker p').animate({"marginLeft":"400px","opacity":".0"}, 600).fadeOut(100);
$('.newsticker').append("<p style='margin-left:400px;opacity:0'>"+coms[0].message+"</p>");
$('.newsticker p').animate({"marginLeft":"0px","opacity":"1"}, 600);
}
}
function findComments(time){
return $.grep(comments, function(item){
return item.time == time;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.