Last active
September 9, 2015 10:13
-
-
Save chaitu87/b0b8bbc14773c0371ab2 to your computer and use it in GitHub Desktop.
Fixed Directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive('setClassWhenAtTop', function($window) { | |
var $win = angular.element($window); // wrap window object as jQuery object | |
return { | |
restrict: 'A', | |
link: function(scope, element, attrs) { | |
var topClass = attrs.setClassWhenAtTop, // get CSS class from directive's attribute value | |
offsetTop = element.offset().top; // get element's offset top relative to document | |
$win.on('scroll', function(e) { | |
if ($win.scrollTop() >= offsetTop) { | |
element.addClass(topClass); | |
} else { | |
element.removeClass(topClass); | |
} | |
}); | |
} | |
}; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment