简体   繁体   English

AngularJS:使用$ animate在滚动条上隐藏元素

[英]AngularJS: Hide element on scroll with $animate

I have the following directive: 我有以下指令:

angular.module('mymod').directive("hideOnScroll", function($animate, $document) {
    return function(scope, element, attrs) {
        $document.bind('scroll', function () {
            if ($document.scrollTop() > 80) {
                console.log("this is fired1")
                $animate.addClass(element, "fade");
            } else {
                console.log("this is fired2")
                $animate.removeClass(element, "fade");
            }
        });
    };
});

I have both "this is fired" messages in the log at some point 有时我在日志中都有“这被解雇”消息

Plus, I have the following animation service: 另外,我有以下动画服务:

angular.module('mymod').animation(".fade", function() {
    console.log("this is never fired3")

    return {
        addClass: function(element, className) {
            console.log("this is never fired4")
            //TweenMax.to(element, 1, {opacity: 0});
        },
        removeClass: function(element, className) {
            console.log("this is never fired5")
            //TweenMax.to(element, 1, {opacity: 1});
        }
    };
});

None of it's console messages is fired. 不会触发任何控制台消息。 at all (3, 4 and 5). 全部(3、4和5)。 I checked if it's added to the browser, it is. 我检查了它是否已添加到浏览器中。 And I have ngAnimate as a dependency 我有ngAnimate作为依赖项

This is the element: 这是元素:

<div hide-on-scroll>Hello</div>

Edit: I can see in chrome's element inspector that the div doesn't get the new class after '$animate.addClass(element, "fade")' is fired 编辑:我可以在chrome的元素检查器中看到,在触发'$ animate.addClass(element,“ fade”)'之后,div没有获得新类

What am I missing? 我想念什么?

When event handlers attached manually by for example addEventListener() or by the jqLite/jQuery methods on and bind execute you need to manually trigger the digest loop to let Angular know that something has changed. 当事件处理程序手动连接例如通过addEventListener()或由jqLit​​e / jQuery方法on ,并bind执行,你需要手动触发消化循环,让角知道事情有了变化。

You can use $apply (like for example ng-click does internally): 您可以使用$apply (例如ng-click在内部执行):

$document.bind('scroll', function() {
  scope.$apply(function() {
    if ($document.scrollTop() > 80) {
      console.log("this is fired1");
      $animate.addClass(element, "fade");
    } else {
      console.log("this is fired2");
      $animate.removeClass(element, "fade");
    }
  });
});

Also note that when you attach event listeners to the document you should manually remove them when the scope is destroyed: 另请注意,将事件侦听器附加到文档时,在销毁范围时应手动将其删除:

var onScroll = function() {
  scope.$apply(function() {
    if ($document.scrollTop() > 80) {
      console.log("this is fired1");
      $animate.addClass(element, "fade");
    } else {
      console.log("this is fired2");
      $animate.removeClass(element, "fade");
    }
  });
};

$document.bind('scroll', onScroll);

scope.$on('$destroy', function() {
  $document.unbind('scroll', onScroll);
});

Demo: http://plnkr.co/edit/wl0vujSnBcb24FHGQ4il?p=preview 演示: http : //plnkr.co/edit/wl0vujSnBcb24FHGQ4il?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM