Created
June 9, 2017 20:25
-
-
Save nvurgaft/259573e73c098e8aa0f2fa660aec581d to your computer and use it in GitHub Desktop.
AngularJS directive for affixing elements
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
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