[英]How can I run jQuery function only if user hasn’t scrolled?
I've been trying to incorporate an auto scroll function on my website ( live here ).我一直在尝试在我的网站上加入自动滚动功能(住在这里)。 I want it to scroll to the content 6 seconds after load, but only if the user is on the very top of the page and hasn't scrolled enough to uncover the #introduction element.
我希望它在加载后 6 秒滚动到内容,但前提是用户位于页面的最顶部并且没有滚动到足以发现 #introduction 元素。
I have this code, which works, but I don't want it to execute if the user has already scrolled by themself:我有这段代码,它可以工作,但如果用户已经自己滚动了,我不希望它执行:
$('html,body').delay(6000).animate({
scrollTop: $('#introduction').offset().top
}, 1000);
I would also like it to happen with a type of ease-in-out, if possible.如果可能的话,我也希望它能以一种缓入缓出的方式发生。
You can do something like this:你可以这样做:
$('html')
.delay(6000)
// If the user has scrolled down, do nothing. Otherwise scroll to element
.queue(function(next) {
if ($(window).scrollTop() > 0) {
return;
} else {
next();
}
})
.animate(
{
scrollTop: $('#introduction').offset().top,
},
1000,
'swing',
);
As for the ease-in-out
animation you need JqueryUI for that.至于
ease-in-out
动画,你需要JqueryUI 。 swing
and linear
are the Jquery library supported easing functions. swing
和linear
是 Jquery 库支持的缓动函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.