Skip to content

Instantly share code, notes, and snippets.

@nvurgaft nvurgaft/affix.js
Created Jun 9, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.