Skip to content

Instantly share code, notes, and snippets.

@chaitu87
Last active September 9, 2015 10:13
Show Gist options
  • Save chaitu87/b0b8bbc14773c0371ab2 to your computer and use it in GitHub Desktop.
Save chaitu87/b0b8bbc14773c0371ab2 to your computer and use it in GitHub Desktop.
Fixed Directive
.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