简体   繁体   English

jQuery onScroll()显示和隐藏DIV

[英]jQuery onScroll() show and hide DIVs

I have a simple onScroll function, which shows a DIV when the scroll (down) height is 100 for example, and then if scrolled up soon as it reach 100 it hides the div, works perfect. 我有一个简单的onScroll函数,例如,当滚动(向下)高度为100时显示DIV,然后如果在达到100时立即向上滚动,则会隐藏div,可以完美工作。

However, if I scroll down quickly and while its showing the DIV if I quickly scroll up & down 2 three times, it doesn't catch the event, even if its up again, it still shows the DIV, but again if I scroll even 1 pixel down, it hides it and if reaches 100 then it shows DIV again.. I hope I made it clear, I dont have an online demo as I am working on localhost.. below is my function that I am using standalone in the template within just <*script> tag.. 但是,如果我快速向下滚动并在快速显示两次上下滚动两次时显示DIV,则即使该事件再次向上滚动,它也不会显示该事件,但即使我再次滚动也仍然显示DIV向下移1像素,将其隐藏,如果达到100,则再次显示DIV。.我希望我明确表示,由于我正在使用localhost,所以没有在线演示。.下面是我在独立环境中使用的函数<* script>标签内的模板。

  jQuery(document).scroll(function ($) {

      var y = jQuery(this).scrollTop();    
      var hoffset = 100;

      if (y > hoffset) {
          // show div
      } else {
          // hide div
      }
  });

Can someone please guide me to right direction, what other best approaches can be done for this, basically I am doing this for header nav div.. 有人可以指导我正确的方向,还有什么其他最佳方法可以做到这一点,基本上我是在头导航分区中这样做的。

regards 问候

Do you want like this? 你要这样吗 See my Fiddle 见我的小提琴

I use fadeIn() and fadeOut() instead. 我改用fadeIn()fadeOut()

我发现停止动画的唯一方法是在动画制作过程中进行操作。

 jQuery('.thedivclass').stop(false, true).slideDown();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM