I have a directive, which I'm using as an attribute. The directive adds and removes a class, which triggers a css animation to fade the div in and out. I have this in multiple locations on my page; however, once the first div picks up the value, the rest of the divs(which are out of view), pick up the value as well. How would I make these directives work independently?
Directive:
.directive("scroll", function ($window) {
return function (scope, element, attrs) {
function getScrollOffsets(w) {
// Use the specified window or the current window if no argument
w = w || window;
// This works for all browsers except IE versions 8 and before
if (w.pageXOffset != null) return {
x: w.pageXOffset,
y: w.pageYOffset
};
}
angular.element($window).bind("scroll", function (e) {
var offset = getScrollOffsets($window);
if (offset.y >= 10) {
e.preventDefault();
e.stopPropagation();
element.removeClass('not-in-view');
element.addClass('in-view');
} else {
e.preventDefault();
e.stopPropagation();
element.removeClass('in-view');
element.addClass('not-in-view');
}
scope.$apply();
});
};
});
HTML:
<div class="sidebar col-md-4" scroll>
<h1>Content</h1>
</div>
<div class="sidebar col-md-4" scroll>
<h1>More Content</h1>
</div>