滚动到/从div底部滚动时显示/隐藏页眉页脚

时间:2016-01-15 13:37:56

标签: jquery scroll show-hide

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

我正在使用jQuery,目前我的代码中有一个让我疯狂的错误:
滚动后一切都开始了#34;弹跳&#34;反复...

我尝试使用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); } });  
    }

});

});

我在overflow: hiddenhtml

上也使用了CSS body

1 个答案:

答案 0 :(得分:-1)

http://jsfiddle.net/mariusc23/s6mLJ/31/ 这就是我认为你要求的..只是检查出来。我相信这会对你有所帮助 HTML代码 -

> git clone the project files
> create new directory
> Run composer install (Assume the project is Laravel 4 App)
> Checking the provided DB user and pass if they are correct, if yes migrating the DB, if No redirect back with an error.
> Display the 'installation success' page if everything is OK.

javascript代码 -

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

css代码 -

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;
}