如何在Angular2中实现滚动隐藏和显示导航栏

时间:2016-12-06 23:03:19

标签: jquery angular

我试图在angular2

中复制以下小提琴

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
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){
    //alert(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;
}
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}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

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

这就是我的尝试:

 constructor(
   private _element: ElementRef
   ) {}
      hasScrolled(evt) {
        let st = evt.target.scrollTop;
        let lastScrollTop = 0;

        // Make sure they scroll more than delta
        if(Math.abs(lastScrollTop - st) <= this.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 > this.navbarHeight){

                this._element.nativeElement.classList.add('nav-down');
                this._element.nativeElement.classList.remove('nav-up');
        } else {
            // Scroll Up
            if(st + window.innerHeight < evt.target.clientTop) {
                //$('header').removeClass('nav-up').addClass('nav-down');
                this._element.nativeElement.classList.remove('nav-up');
                this._element.nativeElement.classList.add('nav-down');
            }
        } 
        lastScrollTop = st;
        } 
       updateHeader(evt) {
            this.currPos = (window.pageYOffset || evt.target.scrollTop) - (evt.target.clientTop || 0);
            if(this.currPos >= this.changePos ) {
                this.isScrolled = true;
                hasScrolled();
                this.isScrolled = false;
            } else {
                this.isScrolled = false;
            }
        }

向下滚动时导航栏消失,向上滚动时再次显示。 但似乎我的逻辑完全不合适。 我如何在angular2中作为指令执行此操作?

1 个答案:

答案 0 :(得分:0)

处理dom元素时,特别是动态添加的元素(以及加载等),setInterval和/或setTimeouts很重要。没有Domcontent-loaded-callback,但超时或间隔(0)将起作用。因此,在超时时包装一些代码。您的示例使用间隔。