简体   繁体   English

向下滚动“向上滑动”导航菜单,并在向上滚动时立即显示

[英]“slide up” nav menu on scroll-down, and appear as soon as scroll up

I want my nav menu to slide up to a certain level as soon as I scroll down, and reappear as soon as I scroll up. 我希望我的导航菜单在向下滚动时立即向上滑动到某个水平,并在向上滚动时重新出现。 I want it to be exactly as the following menu: http://www.defringe.com/ If you notice, when we slide down, we can still notice a tip of the menu. 我希望它与以下菜单完全相同: http : //www.defringe.com/如果您注意到,当我们向下滑动时,我们仍然可以注意到菜单的提示。

Now what I was able to find so far is a code that hides the menu completely. 现在,到目前为止,我能找到的是一个完全隐藏菜单的代码。 here is my code: 这是我的代码:

HTML: HTML:

<header id="header-wrap">
<nav id="topnav">
    <ul>
      <li>point1</li>
      <li>point2</li>
      <li>point3</li>
      <li>point4</li>
    </ul>
</nav>

CSS: CSS:

#topnav{
position:absolute;
margin:auto;
padding-top:20px;
height:60px;
width:576px;
bottom: 0;}

#header-wrap{
background:#f1f2f2;
height:60px;
position:fixed;
width:100%;}

jQuery: jQuery的:

 $(function(){
    $('header').data('size','big');
   var previousScroll = 0;
    headerOrgOffset = $('#topnav').height()

$('#header-wrap').height($('#topnav').height());
});

$(window).scroll(function () {
    var currentScroll = $(this).scrollTop();
    if (currentScroll > headerOrgOffset) {
        if (currentScroll > previousScroll) {
            $('#header-wrap').slideUp("fast");
        } else {
            $('#header-wrap').slideDown("fast");
        }
    } 
    previousScroll = currentScroll;
});

Thanks! 谢谢!

In this case you do not want to you slideUp / slideDown but rather build your own function that slides the menu only a bit to -margin 在这种情况下,您不想使用slideUp / slideDown,而是构建自己的功能,该功能仅将菜单滑动至-margin

Somnething like

$(window).scroll(function () {
var currentScroll = $(this).scrollTop();
if (currentScroll > headerOrgOffset) {
    if (currentScroll > previousScroll) {
        // Say the menu height is 100px
        $('#header-wrap').css("margin-top", "-90px");
    } else {
        $('#header-wrap').css("margin-top", "0");
    }
} 
previousScroll = currentScroll;
});

Obviously you need to format the menu properly, have it fixed etc, but this should do the trick. 显然,您需要正确设置菜单格式,进行修复等,但这应该可以解决问题。 If you are not sure how to make it work, let me know and I can do it for you completely. 如果您不确定如何使它工作,请告诉我,我可以为您完全做到。

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

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