Created
September 30, 2014 09:10
-
-
Save Luxiyalu/6206ae3d62b8b49ac4d5 to your computer and use it in GitHub Desktop.
AngularJS Directive: {Position: Sticky}
This file contains 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
# to provide a fallback for the new CSS property, position: sticky; | |
# which let an element stays where it is when it's within the screen | |
# and when it's disappearing into the top of the page with scrolling, | |
# change the position's behavior to fix. | |
app.directive 'sticky', ($window, WindowObj) -> | |
restrict: 'A' | |
link: (scope, element, attr) -> | |
sticky = undefined | |
originalOffsetY = element[0].offsetTop | |
# this needs to be used together with directive SCROLL | |
# and with a $scope.$broadcast('scroll::scroll') | |
scope.$on 'scroll::scroll', -> | |
if originalOffsetY < WindowObj.scrollY | |
return if sticky is true | |
sticky = true | |
element.addClass('sticky') | |
else | |
return if sticky is false | |
sticky = false | |
element.removeClass('sticky') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment