【问题标题】:show/hide header footer when scroll to/from a div bottom滚动到/从 div 底部显示/隐藏页眉页脚
【发布时间】:2016-01-15 13:37:56
【问题描述】:

我需要隐藏页眉并显示页脚(反之亦然)
当我向下滚动(到达底部)并向上滚动(离开底部)某个<div>

我正在使用 jQuery,目前我的代码中有一个让我发疯的错误:
滚动后,一切都开始反复“弹跳”...

我尝试使用stop()one()queue: false,但没有成功...

当我设置#main的高度时,问题似乎来了,
(如果该部分被注释掉,似乎可以正常工作,如下面的代码)

但我需要调整 #main 的大小,因为页脚比页眉大。

$(window).load(function(){

xxx = '';
$main.css('height','calc(100% - 79px)');

$('.column.right').on('scroll', function(){

    if( ($(this).scrollTop() + $(this).innerHeight()) == $(this)[0].scrollHeight){
        xxx = 'equal';
        console.log('equal');

        $header.one().stop().slideUp();
        $footer.one().stop().slideDown({ queue: false, duration: 100, complete: function(){ setTimeout(function(){ /*$main.css('height','calc(100% - 221px)');*/ }, 0); } });
    }
    else if( ($(this).scrollTop() + $(this).innerHeight()) < ($(this)[0].scrollHeight - 0) && xxx == 'equal'){
        xxx = 'minor';
        console.log('minor');

        $header.one().stop().slideDown();
        $footer.one().stop().slideUp({ queue: false, duration: 100, complete: function(){ setTimeout(function(){ /*$main.css('height','calc(100% - 79px)');*/ }, 0); } });  
    }

});

});

我也在 htmlbody 上使用 CSS overflow: hidden

【问题讨论】:

    标签: jquery scroll show-hide


    【解决方案1】:

    http://jsfiddle.net/mariusc23/s6mLJ/31/ 这就是我认为你要的..看看这个。我相信这会对你有所帮助 html代码-

    <header class="nav-down">
        This is your menu.
    </header>
    <main>
        This is your body.
    </main>
    <footer>
        This is your footer.
    </footer>
    

    javascript代码-

    var delta = 5;
    var navbarHeight = $('header').outerHeight();
    
    $(window).scroll(function(event){
        didScroll = true;
    });
    
    setInterval(function() {
        if (didScroll) {
            hasScrolled();
            didScroll = false;
        }
    }, 250);
    
    function hasScrolled() {
        var st = $(this).scrollTop();
    
        // Make sure they scroll more than delta
        if(Math.abs(lastScrollTop - st) <= delta)
            return;
    
        // If they scrolled down and are past the navbar, add class .nav-up.
        // This is necessary so you never see what is "behind" the navbar.
        if (st > lastScrollTop && st > navbarHeight){
            // Scroll Down
            $('header').removeClass('nav-down').addClass('nav-up');
        } else {
            // Scroll Up
            if(st + $(window).height() < $(document).height()) {
                $('header').removeClass('nav-up').addClass('nav-down');
            }
        }
    
        lastScrollTop = st;
    }
    

    css代码-

    body {
        padding-top: 40px;
    }
    
    header {
        background: #f5b335;
        height: 40px;
        position: fixed;
        top: 0;
        transition: top 0.2s ease-in-out;
        width: 100%;
    }
    
    .nav-up {
        top: -40px;
    }
    
    main {
       background:url(
    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAPklEQVQYV2O8dOnSfwYg0NPTYwTRuAAj0QqxmYBNM1briFaIzRbi3UiRZ75uNgUHGbfvabgfsHqGaIXYPAMAD8wgC/DOrZ4AAAAASUVORK5CYII=
       ) repeat;
        height: 2000px;
    }
    
    footer { background: #ddd;}
    * { color: transparent}
    

    【讨论】:

    • 似乎是根据鼠标滚轮加速显示/隐藏,我需要使用div“滚动到/从底部”点代替
    猜你喜欢
    • 2021-11-21
    • 2018-10-02
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    相关资源
    最近更新 更多