[英]Javascript - Can I make this table scroll to top?
I have to make a self-scrolling table.我必须制作一个自滚动表。 I tried using this: https://codepen.io/salman31/pen/dYdGLa
我试过用这个: https://codepen.io/salman31/pen/dYdGLa
var my_time;
$(document).ready(function() {
pageScroll();
$("#contain").mouseover(function() {
clearTimeout(my_time);
}).mouseout(function() {
pageScroll();
});
});
function pageScroll() {
var objDiv = document.getElementById("contain");
objDiv.scrollTop = objDiv.scrollTop + 1;
$('p:nth-of-type(1)').html('scrollTop : '+ objDiv.scrollTop);
$('p:nth-of-type(2)').html('scrollHeight : ' + objDiv.scrollHeight);
//if (objDiv.scrollTop == (objDiv.scrollHeight - 50)) {
objDiv.scrollTop = 0;
//}
my_time = setTimeout('pageScroll()', 25);
}
And it works, but it doesn't automatically scroll back to top when it hits the "bottom".它可以工作,但是当它到达“底部”时它不会自动滚动回顶部。 What do I need to change?
我需要改变什么?
Thank you!谢谢!
try this尝试这个
var my_time;
$(document).ready(function() {
pageScroll();
$("#contain").mouseover(function() {
clearTimeout(my_time);
}).mouseout(function() {
pageScroll();
});
$('#contain').bind('scroll', function()
{
if($(this).scrollTop() + $(this).innerHeight()>=$(this)[0].scrollHeight)
{
pageScroll();
}
})
});
function pageScroll() {
var objDiv = document.getElementById("contain");
objDiv.scrollTop = objDiv.scrollTop + 1;
$('p:nth-of-type(1)').html('scrollTop : '+ objDiv.scrollTop);
$('p:nth-of-type(2)').html('scrollHeight : ' + objDiv.scrollHeight);
if (objDiv.scrollTop == (objDiv.scrollHeight - 100)) {
objDiv.scrollTop = 0;
}
my_time = setTimeout('pageScroll()', 25);
}
The line:该行:
objDiv.scrollTop = 0
is what's supposed to reset the scroll position, using the if statement you've got commented to execute it only once it's finished scrolling up.是什么应该重置滚动 position,使用您已注释的 if 语句仅在完成向上滚动后执行它。 With the if statement commented out, the table shouldn't be scrolling at all, as the pageScroll function will always set the scrollTop property to 0.
注释掉 if 语句后,表格根本不应该滚动,因为 pageScroll function 将始终将 scrollTop 属性设置为 0。
Try uncommenting the if statement, and making sure that you're subtracting the height of your container from objDiv.scrollHeight in the comparison.尝试取消注释 if 语句,并确保在比较中从 objDiv.scrollHeight 中减去容器的高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.