forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsticky.coffee
More file actions
56 lines (41 loc) · 1.18 KB
/
Copy pathsticky.coffee
File metadata and controls
56 lines (41 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
define ['underscore', 'jquery'], (_, $) ->
class Sticky
@instances: []
@initialized: false
@$container = $ window
@initialize: ->
@$container.on 'scroll', _.debounce(@checkInstances, 10)
@initialized = true
@addInstance: (instance) ->
@initialize() unless @initialized
@instances.push instance
@checkInstances()
@removeInstance: (instance) ->
@initialize() unless @initialized
@instances = _.reject @instances, (i) -> i == instance
@checkInstances()
@checkInstances: =>
containerTop = @$container.scrollTop()
for instance in @instances
if containerTop >= instance.top
instance.stick() unless instance.stuck
else
instance.unstick() if instance.stuck
null
constructor: (@$el) ->
@top = @$el.offset().top
@stuck = false
@constructor.addInstance this
stick: ->
@$el.addClass 'sticky'
@stuck = true
unstick: ->
@$el.removeClass 'sticky'
@stuck = false
remove: ->
@unstick()
@constructor.removeInstance this
$.fn.sticky = ->
@each -> new Sticky $ this
$ -> $('[data-sticky]').sticky()
Sticky