Skip to content

Instantly share code, notes, and snippets.

@wspringer
Created October 28, 2015 08:07
Show Gist options
  • Select an option

  • Save wspringer/e9a5a0975b8cda97f435 to your computer and use it in GitHub Desktop.

Select an option

Save wspringer/e9a5a0975b8cda97f435 to your computer and use it in GitHub Desktop.
Angular directive for tracking scroll events
angular.module 'ngScrollSupport', []
.directive 'ngScrollSupport', ->
restrict: 'A'
link: (scope, element, attrs) ->
atTop = true
atBottom = false
check = ->
scrollTop = element.prop 'scrollTop'
scrollHeight = element.prop 'scrollHeight'
clientHeight = element.prop 'clientHeight'
switch
when scrollTop is 0 and not atTop
atTop = true
element.addClass 'at-top'
when scrollTop is scrollHeight - clientHeight and not atBottom
atBottom = true
element.addClass 'at-bottom'
else
atTop = false
atBottom = false
element.removeClass 'at-top'
element.removeClass 'at-bottom'
element.addClass 'at-top'
element.on 'scroll', _.throttle check, 300
scope.$on '$destroy', ->
element.off 'scroll'
@wspringer
Copy link
Copy Markdown
Author

Was just playing around with it, but I don't want to loose it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment