Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
fork of alxhill's at not sure why, but the original wasn't working for me when the scope model updated. spyElems[] 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…
angular.module('jobFoundryDirectives').directive 'spy', ($location) ->
restrict: "A"
require: "^scrollSpy"
link: (scope, elem, attrs, scrollSpy) ->
attrs.spyClass ?= "current" ->
scope.$apply ->
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[]?
spyElems[] = elem.find('#'
$($window).scroll ->
highlightSpy = null
for spy in scope.spies
# 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[]? or spyElems[].length is 0
spyElems[] = elem.find('#'
# the element could still not exist, so we check first to avoid errors
if spyElems[]? and spyElems[].length isnt 0
if (pos = spyElems[].offset().top) - $window.scrollY <= topBuffer
spy.pos = pos
highlightSpy ?= spy
if highlightSpy.pos < spy.pos
highlightSpy = spy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment