【发布时间】:2016-01-17 20:06:08
【问题描述】:
我正在尝试使用 AngularJS 重新创建这个小提琴:http://jsfiddle.net/mariusc23/s6mLJ/31/。
实际上,当用户向下滚动页面时,header 消失了。如果在任何时候,用户向上滚动,甚至 1/2 像素……header 就会向下滚动。
我创建了一个 plunker:http://plnkr.co/edit/DBiY57kKUWiISVDJiDU4?p=preview,它似乎应用了 hide-header 类,但是,我似乎无法出现在 scrollUp 上。
指令:
app.directive("myDirective", function ($window) {
return function(scope, element, attrs) {
angular.element($window).bind("scroll", function() {
var lastScrollTop = 0;
var delta = 50;
var windowInnerHeight = $window.innerHeight;
var windowHeight = $window.height;
var st = this.pageYOffset;
// 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){
if (st > lastScrollTop && st > 50){
// Scroll Down
//$('header').removeClass('nav-down').addClass('nav-up');
console.log("in if...");
scope.navUp = true;
} else {
// Scroll Up
if(st + windowInnerHeight < windowHeight) {
//$('header').removeClass('nav-up').addClass('nav-down');
console.log("in else...");
}
}
lastScrollTop = st;
scope.$apply();
});
};
});
HTML:
<body ng-controller="MainCtrl">
<header my-directive ng-class="{true : 'hide-header'}[navUp]">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</header>
</body>
【问题讨论】:
-
以前从未见过 {true : 'hide-header'}[navUp] 这样的符号
标签: javascript css angularjs angularjs-directive angularjs-scope