简体   繁体   English

如何仅在用户未滚动时运行 jQuery 函数?

[英]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. swinglinear是 Jquery 库支持的缓动函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当未触发另一个mouseover事件时,如何才能为jquery mouseout事件创建异常? - How can I create an exception for a jquery mouseout event only if another mouseover event hasn't been triggered? 在jquery中,如果有人在1秒钟内没有输入内容,我该如何调用函数? - In jquery how can I call function when someone hasn't typed in 1 second? 如何仅在需要时运行 jQuery 函数? - How can I run a jQuery function only when it's needed? 我怎么能自信地知道用户没有更改文本框值? - How can I know with confidence that a user hasn't changed a textbox value? 仅当用户滚动到首屏以下时,如何使用jquery触发灯箱 - How do I trigger a lightbox with jquery, only if the user has scrolled below the fold jQuery-运行功能,即使之前的功能尚未触发。 - Jquery - Run function even when function before hasn't fired. 仅当用户滚动到页面底部时,如何添加显示DOM元素的jQuery事件监听器(在这种情况下为iframe) - How do I add a jQuery Event Listener that show a DOM element in this case an iframe only if the user has scrolled to the bottom of the page 如何检查使用jQuery滚动了多少元素? - How can I check how much an element was scrolled using jQuery? 我如何获得水平滚动jQuery的div多少 - How can I get how much a div is horizontally scrolled jquery jQuery-当用户从当前位置向上滚动100px时运行一个函数 - jQuery - Run a function when user has scrolled up 100px from current location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM