Skip to content

Instantly share code, notes, and snippets.

@axelav
Forked from alxhill/scrollspy.coffee
Last active January 4, 2016 15:18

Revisions

  1. axelav revised this gist Jan 27, 2014. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion scrollspy.coffee
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,8 @@ angular.module('directives.scrollSpy', [])
    $('html, body').stop().animate
    scrollTop: target.offset().top + 1
    speed
    easeInOutCubuc = (x, t, b, c, d) ->
    c * ((t = t / d - 1) * t * t + 1) + b
    else
    $('html, body').stop().animate scrollTop: 0, speed

    @@ -51,7 +53,10 @@ angular.module('directives.scrollSpy', [])
    spyElems[spy.id] = elem.find('#' + spy.id)

    if spyElems[spy.id]? and spyElems[spy.id].length isnt 0
    if (pos = spyElems[spy.id].offset().top) - $window.scrollY <= topBuffer
    # TODO: fix for IE9+ ?
    # https://gist.github.com/alxhill/6886760#comment-952714
    if (pos = spyElems[spy.id].offset().top) - $window.pageYOffset <= topBuffer
    # if (pos = spyElems[spy.id].offset().top) - $window.scrollY <= topBuffer
    spy.pos = pos
    highlightSpy ?=spy
    if highlightSpy.pos < spy.pos
  2. axelav revised this gist Jan 26, 2014. 1 changed file with 37 additions and 26 deletions.
    63 changes: 37 additions & 26 deletions scrollspy.coffee
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,61 @@
    angular.module('jobFoundryDirectives').directive 'spy', ($location) ->
    restrict: "A"
    require: "^scrollSpy"
    angular.module('directives.scrollSpy', [])

    .directive('spy', ($location) ->
    restrict: 'A'
    require: '^scrollSpy'
    link: (scope, elem, attrs, scrollSpy) ->
    attrs.spyClass ?= "current"
    attrs.spyClass ?= 'active'

    elem.click ->
    scope.$apply ->
    $location.hash(attrs.spy)
    elem.click (e) ->
    e.stopPropagation()
    if attrs.spy
    target = $('#' + attrs.spy)
    speed = 750
    $('html, body').stop().animate
    scrollTop: target.offset().top + 1
    speed
    else
    $('html, body').stop().animate scrollTop: 0, speed

    scrollSpy.addSpy
    id: attrs.spy
    in: -> elem.addClass attrs.spyClass,
    in: ->
    elem.addClass attrs.spyClass
    elem.parents('li').addClass attrs.spyClass
    out: -> elem.removeClass attrs.spyClass
    )

    angular.module('jobFoundryDirectives').directive 'scrollSpy', ($window) ->
    .directive('scrollSpy', ($window, $timeout) ->
    restrict: 'A'
    controller: ($scope) ->
    $scope.spies = []
    @addSpy = (spyObj) -> $scope.spies.push spyObj
    @addSpy = (spyObj) ->
    $scope.spies.push spyObj
    return
    scope: true
    link: (scope, elem, attrs) ->
    spyElems = []

    scope.$watch 'spies', (spies) ->
    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)
    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.
    spyElems[spy.id] =
    if spyElems[spy.id].length is 0
    elem.find('#'+spy.id)
    else
    spyElems[spy.id]

    # the element could still not exist, so we check first to avoid errors
    if spyElems[spy.id].length isnt 0
    if (pos = spyElems[spy.id].offset().top) - $window.scrollY <= 0
    if !spyElems[spy.id]? or spyElems.length is 0
    spyElems[spy.id] = elem.find('#' + spy.id)

    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
    highlightSpy ?=spy
    if highlightSpy.pos < spy.pos
    highlightSpy = spy

    highlightSpy?.in()
    highlightSpy?.in()
    )
  3. @alxhill alxhill created this gist Oct 8, 2013.
    50 changes: 50 additions & 0 deletions scrollspy.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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 = []

    scope.$watch '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.
    spyElems[spy.id] =
    if spyElems[spy.id].length is 0
    elem.find('#'+spy.id)
    else
    spyElems[spy.id]

    # the element could still not exist, so we check first to avoid errors
    if spyElems[spy.id].length isnt 0
    if (pos = spyElems[spy.id].offset().top) - $window.scrollY <= 0
    spy.pos = pos
    highlightSpy ?= spy
    if highlightSpy.pos < spy.pos
    highlightSpy = spy

    highlightSpy?.in()