How to hide/show nav bar when user scrolls up/down

后端 未结 6 1359
面向向阳花
面向向阳花 2020-12-12 18:49

Hide/show nav bar when user scrolls up/down

Here\'s the example I\'m trying to achieve: http://haraldurthorleifsson.com/ or http://www.teehanlax.com

6条回答
  •  温柔的废话
    2020-12-12 19:09

    To get the inner content of the nav to slide up instead of being progressively hidden, you need to do the animation on the parent element, and keep the inner element at the bottom of the parent, like so:

    jsfiddle

    css

    body {
        height: 1000px;
    }
    
    #header {
        width: 100%;
        position: absolute;
        bottom: 0;
    }
    
    #header-wrap {
        width: 100%;
        position: fixed;
        background-color: #e0e0e0;
    }
    

    js

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

提交回复
热议问题