gpt4 book ai didi

javascript - 为什么这不会跳到正确的行?

转载 作者:行者123 更新时间:2023-11-28 13:40:09 25 4
gpt4 key购买 nike

我做了DEMO请参阅显示所有评论及其时间的表格。

当视频开始播放时,随着视频播放时间的推移,它开始显示每条评论。

我面临的问题是它不会跳转到表格中的正确行(表格看起来像文本区域)
它来回走动,但不会瞄准。
我希望每次弹出评论时,正确的行都显示在表格的顶部位置。
它应该滚动,并跟随表中当前评论的行。

我该如何解决这个问题?任何人都可以修改和更新我的 jsfiddle 吗?

Javascript

var row= '';

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':'11','message':'hello! 11 secs has past'},
{'time':'10','message':'hello! 10-2 secs has past'},
{'time':'5','message':'hello! 5 secs has past'},
{'time':'20','message':'hello! 20 secs has past'},
{'time':'21','message':'hello! 21 secs has past'},
{'time':'20','message':'hello! 20-2 secs has past'},
{'time':'25','message':'hello! 25 secs has past'},
{'time':'30','message':'hello! 30 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);

var row = "tr.comment" + time;
$("tr").css("background-color","#ffffff");
jQuery(row).each(function() {
$(this).css("background","#87cefa");
$('div#container').animate({
scrollTop: jQuery(this).offset().top
}, 500);
});
}
}

function findComments(time){
return $.grep(comments, function(item){
return item.time == time;
});
}

HTML

    <body>
<div class="newsticker">
</div>
<br />
<br />
<video id="video" width="320" height="180" controls="controls" autoplay="autoplay" name="media"><source src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4"></video>
</body>

<div id="container" style="border:solid 1px;height:70px; width:500px; overflow-y:scroll;">
<table border="0" height="100" width="400">
<tr class="comment5">
<td>00:00:05</td>
<td>hello! 5 secs has past</td>
</tr>

<tr class="comment10">
<td>00:00:10</td>
<td>hello! 10 secs has past</td>
</tr>

<tr class="comment10">
<td>00:00:10</td>
<td>hello! 10-2 secs has past</td>
</tr>

<tr class="comment11">
<td>00:00:11</td>
<td>hello! 11 secs has past</td>
</tr>

<tr class="comment20">
<td>00:00:20</td>
<td>hello! 20 secs has past</td>
</tr>

<tr class="comment20">
<td>00:00:20</td>
<td>hello! 20-2 secs has past</td>
</tr>

<tr class="comment21">
<td>00:00:21</td>
<td>hello! 21 secs has past</td>
</tr>

<tr class="comment25">
<td>00:00:25</td>
<td>hello! 25 secs has past</td>
</tr>

<tr class="comment30">
<td>00:00:30</td>
<td>hello! 30 secs has past</td>
</tr>
</table>
</div>

CSS

div.newsticker{
border:1px solid #666666;
width:100%;
height:50px;
}

.newsticker p{
float:left;
position:absolute;
}

最佳答案

我不知道为什么,但是 offset().top 为注释行提供的值太大了,没有用...它们只是让div一直向下滚动。

我用 DOM 属性 offsetTop 替换它,它似乎工作正常:

$('div#container').animate({
scrollTop: this.offsetTop
}, 500);

http://jsfiddle.net/9zqhF/19/

关于javascript - 为什么这不会跳到正确的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com