-
-
Save pandafulmanda/7762008 to your computer and use it in GitHub Desktop.
fork of alxhill's scrollspy.coffee at https://gist.github.com/alxhill/6886760 not sure why, but the original wasn't working for me when the scope model updated. spyElems[spy.id] was undefined for new elements at line 37 and caused scrollfix to break after a model change. am using v1.2.0-8336b3a also add a quick attribute to allow easy custom buf…
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
angular.module('jobFoundryDirectives').directive 'spy', ($location) -> | |
restrict: "A" | |
require: "^scrollSpy" | |
link: (scope, elem, attrs, scrollSpy) -> | |
attrs.spyClass ?= "current" | |
elem.click -> | |
scope.$apply -> | |
$location.hash(attrs.spy) | |
scrollSpy.addSpy | |
id: attrs.spy | |
in: -> elem.addClass attrs.spyClass, | |
out: -> elem.removeClass attrs.spyClass | |
angular.module('jobFoundryDirectives').directive 'scrollSpy', ($window) -> | |
restrict: 'A' | |
controller: ($scope) -> | |
$scope.spies = [] | |
@addSpy = (spyObj) -> $scope.spies.push spyObj | |
link: (scope, elem, attrs) -> | |
spyElems = {} | |
topBuffer = if attrs.topBuffer then attrs.topBuffer else 0 | |
scope.$watchCollection 'spies', (spies) -> | |
for spy in spies | |
unless spyElems[spy.id]? | |
spyElems[spy.id] = elem.find('#'+spy.id) | |
$($window).scroll -> | |
highlightSpy = null | |
for spy in scope.spies | |
spy.out() | |
# the elem might not have been available when it was originally cached, | |
# so we check again to get another element in case this one doesn't exist. | |
if !spyElems[spy.id]? or spyElems[spy.id].length is 0 | |
spyElems[spy.id] = elem.find('#'+spy.id) | |
# the element could still not exist, so we check first to avoid errors | |
if spyElems[spy.id]? and spyElems[spy.id].length isnt 0 | |
if (pos = spyElems[spy.id].offset().top) - $window.scrollY <= topBuffer | |
spy.pos = pos | |
highlightSpy ?= spy | |
if highlightSpy.pos < spy.pos | |
highlightSpy = spy | |
highlightSpy?.in() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment