Skip to content

Instantly share code, notes, and snippets.

@Luxiyalu
Created September 30, 2014 09:10
Show Gist options
  • Save Luxiyalu/6206ae3d62b8b49ac4d5 to your computer and use it in GitHub Desktop.
Save Luxiyalu/6206ae3d62b8b49ac4d5 to your computer and use it in GitHub Desktop.
AngularJS Directive: {Position: Sticky}
# 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