Created
October 28, 2015 08:07
-
-
Save wspringer/e9a5a0975b8cda97f435 to your computer and use it in GitHub Desktop.
Angular directive for tracking scroll events
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 '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' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was just playing around with it, but I don't want to loose it.