我正在使用这个简单的代码,它工作得很好。
$("#mydiv").animate({ scrollTop: $('#mydiv')[0].scrollHeight}, 20000)
我想要的是在到达底部后立即回到页面顶部并开始缓慢向下滚动。如何在JQuery中实现这些呢?
谢谢大家!
您可以使用以下javascript函数跳转到页面顶部:
function jump(elementId){
var location = document.getElementById(elementId).offsetTop;
window.scrollTo(0, location);
}
只使用页面顶部某些元素的id
。
不知道你想做什么,也不知道"reload"是什么意思,但这里有一个快速的代码片段让你开始:
<<p> JSnippet演示/strong>你可以看到它的可配置性和易于理解:
$(function() {
var pageScan = {
speed : 10000,
loop : true,
delayRestart : 1000,
start : function(){
pageHeight = $('body').height() - window.innerHeight;
pageScan.proc(pageHeight);
},
proc : function(to){
$("body").animate(
{scrollTop: to},
pageScan.speed,
"linear",
function(){
if (pageScan.loop) {
setTimeout(function() {
window.scrollTo(0, 0);
pageScan.start();
}, pageScan.delayRestart);
}
});
}
};
pageScan.start();
});
我们可以这样做:
(function (){
var div = $('#myDiv'),
infiniteScroll = function () {
//---gets the bottom of the page value
var bottomHeight = div[0].scrollHeight;
var callback1 = function () {
//once it gets into this callback function
//it resets the position to zero
div.scrollTop(0);
//invokes again the function infinite scroll
infiniteScroll();
};
div.animate({scrollTop:bottomHeight},20000,callback1)
};
//starts the function for the very first time
infiniteScroll();
})();
我创建了一个codepen,这样你就可以看到它的工作情况并使用它:
http://codepen.io/dieggger/pen/VLoZWv?编辑= 101