我有一个基本上包含一个大表的网页,我使用 ajax 每分钟自动创建和填充该表。基本的代码结构是这样的:
$(document).ready(function(){
setInterval(function(){
$.ajax({
//code to call backend, get the data,
//create/populate the table, and refresh every minute
});
});
}
由于表格很长,我想让它自动向下滚动,暂停几秒钟,继续向下滚动,再次暂停。当它到达尽头时,它会向上滚动、暂停……或者一旦到达尽头,从顶部开始并重新开始。当它滚动时,我还想将其表格标题行固定在顶部。
我的问题是:
- 是否可以使用一些 jquery 函数或 javascript 来做到这一点?
- 这个自动滚动功能会如何干扰 ajax?说它正在向下滚动,是时候刷新 ajax 表了?
最佳答案
- is it possible to do this using some jquery function or javascript?
使用 jQuery/javascript 很容易完成。
- how would this automatic scrolling function interfere with the ajax? say it's in the middle of scrolling down and it's time for ajax table refresh?
我会把你的代码放在你的 ajax 成功的滚动动画中。将滚动设置为比 AJAX 间隔短的时间间隔。
像这样
setInterval(function(){
$.ajax({
type : "",
dataType: "",
url : "",
data : "",
success : function(data)
{
// create/append data to table
// animate scroll to bottom of table
$("#my_table").animate({ scrollTop: $("#my_table").height() }, "slow");
}
});
});
不确定滚动代码是否 100% 正确,但它应该让你开始。
关于javascript - jQuery 自动向下滚动、暂停、再次向下和向上滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437359/