简体   繁体   English

在滚动时隐藏 div 但在活动时保持可见

[英]Hide a div on scroll but keep it visible when active

I'm trying to hide the .site-header on scroll and have it re-appear after inactivity for a couple of seconds.我试图在滚动时隐藏.site-header并让它在几秒钟不活动后重新出现。

I found the most of the answer here: How to hide a Div when the scroll bar is moving with jQuery?我在这里找到了大部分答案: How to hide a Div when the scroll bar are move with jQuery?

 var $header = $(".site-header"); var opacity = $header.css("opacity"); var scrollStopped; var fadeInCallback = function () { if (typeof scrollStopped != 'undefined') { clearInterval(scrollStopped); } scrollStopped = setTimeout(function () { $header.animate({ opacity: 1 }, "slow"); }, 500); } $(window).scroll(function () { if (!$header.is(":animated") && opacity == 1) { $header.animate({ opacity: 0 }, "slow", fadeInCallback); } else { fadeInCallback.call(this); } }); $('.menu-toggle').on('click', function(){ $('.menu-toggle').addClass('activated'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script> <section style="height:5000px;"> <div class="site-header" style="position:fixed; top:5px; left:5px; width:200px; height:100px; display:block; background:#000; opacity:1;"> <button class="menu-toggle">Menu</button> <nav class="nav-primary"> <ul> <li>Menu item 1</li> <li>Menu item 2</li> <li>Menu item 3</li> </ul> </nav> </div> </section>

The nav is activated by clicking button通过单击button激活nav

However, the nav is nested inside the .site-header which means when the menu is activated it still fades out on scroll...但是, nav嵌套在.site-header ,这意味着当菜单被激活时,它仍然会在滚动时淡出......

I'm wondering how to alter this javascript so that when the .activated class is applied to the button the nav remains visible even while scrolling.我想知道如何更改此 javascript,以便将.activated类应用于button ,即使在滚动时nav保持可见。

Here is the fiddle https://jsfiddle.net/fwa16eok/这是小提琴https://jsfiddle.net/fwa16eok/

Thanks so much!非常感谢!

Add !$menu.is(":active") before the fade out animation call在淡出动画调用之前添加!$menu.is(":active")

 var $menu = $("#menu"); var opacity = $menu.css("opacity"); var scrollStopped; var fadeInCallback = function () { if (typeof scrollStopped != 'undefined') { clearInterval(scrollStopped); } scrollStopped = setTimeout(function () { $menu.animate({ opacity: 1 }, "slow"); }, 2000); } $(window).scroll(function () { if (!$menu.is(":active") &&!$menu.is(":animated") && opacity == 1) { $menu.animate({ opacity: 0 }, "slow", fadeInCallback); } else { fadeInCallback.call(this); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script> <section style="height:5000px;"> <div id="menu" style="position:fixed; top:5px; left:5px; width:200px; height:100px; display:block; background:#000; opacity:1;"></div> </section>

HTML: HTML:

<div>
   <div class="a">A</div>
</div>​

Javascript: Javascript:

$(window).scroll(function() {  
   if ($(this).scrollTop() > 0) {
      $('.a').fadeOut();  } 
   else {
      $('.a').fadeIn();
 }
});

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

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