【发布时间】:2014-03-06 04:54:50
【问题描述】:
我有一个启用自动滚动的网站,但给用户几秒钟的时间来禁用它或开始滚动也会禁用它。我以为我可以切换真/假,但这似乎不起作用。我不确定如何实现鼠标滚动事件。这是我的代码:
var autoplay=true;
window.setTimeout(function(){
if (autoplay)
$("html, body").animate({ scrollTop: $(document).height() }, 22000);
},2000);
$(document).ready(function() {
$(".scrollToggle").click(function(){
if(autoplay==true) {
autoplay==false;
}
else {
autoplay==true;
}
});
});
另外,有谁知道我怎样才能让这个自动滚动更流畅?
更新: 非常感谢!这太棒了!唯一的问题是; 1)由于某种原因,它不会在页面加载时自动启动,2)当关闭时,它会从顶部重新开始(这并不是什么大问题),3)它似乎在滚动时会加快速度下。这是我正在运行的其他功能,也许有什么东西阻止了它:
$(window).load(function() { // makes sure the whole site is loaded
$('#status').fadeOut(); // will first fade out the loading animation
$('#preloader').delay(350).fadeOut('slow'); // will fade out the white DIV that covers the website.
$('body').delay(350).css({'overflow':'visible'});
setTimeout (function () {
scrollTo(0,0);
}, 0);
});
var autoplay = true,
root = $("html, body"),
t = 24000, //overall duration for the animation, you can set this
n = 0,
d = 0;
function scrollStart() {
n = Date.now();//get the current time in ms
t -= d;//the current duration minus the time already traverse
root.animate({scrollTop: $(document).height() }, t);
autoplay = false;
}
function scrollStop() {
//on stop instance, get the time already took to travel
d = Date.now() - n;
root.stop(true);
autoplay = true;
}
$(".scrollToggle").click(function(){
if(autoplay) {
scrollStart();
$("#onOff").text("OFF");
} else {
scrollStop();
$("#onOff").text("ON");
}
});
$(window).scroll(function(){
autoplay = false;
});
$(window).bind('resize', function(e)
{
if (window.RT) clearTimeout(window.RT);
window.RT = setTimeout(function()
{
this.location.reload(false); /* false to get page from cache */
}, 100);
});
【问题讨论】:
标签: jquery autoscroll