Skip to content

Instantly share code, notes, and snippets.

@nvurgaft
Created June 9, 2017 20:25
Show Gist options
  • Save nvurgaft/259573e73c098e8aa0f2fa660aec581d to your computer and use it in GitHub Desktop.
Save nvurgaft/259573e73c098e8aa0f2fa660aec581d to your computer and use it in GitHub Desktop.
AngularJS directive for affixing elements
angular.module('affix', []).directive('affix', [function () {
return {
restrict: 'A',
scope: {offset: "="},
link: function (scope, element, attrs) {
var offset = Number.isFinite(scope.offset) ? scope.offset : 300;
var onScroll = function () {
if (window.pageYOffset >= offset) {
element.addClass('affix'); // add affix class
} else {
element.removeClass('affix'); // remove affix class
}
};
window.onscroll = onScroll;
scope.$on('$destroy', function () {
window.onscroll = angular.noop;
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment