-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmomentum.coffee
49 lines (41 loc) · 1.72 KB
/
momentum.coffee
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
class KeepVisible
constructor: (options) ->
return new KeepVisible options unless @ instanceof KeepVisible
throw new Error "'selector' argument missing" unless options.selector
@options = _.defaults options,
duration: 500
easing: 'ease-in-out'
insertElement: (node, next, done) =>
$node = $(node)
currentScrollStop = $(window).scrollTop()
# If outside a selected part of the page we add the element with a nice animation.
# TODO: Should we also check if user scrolled pass the selector and is outside it on the bottom?
if currentScrollStop <= $(@options.selector).offset().top
$node.insertBefore(next).velocity 'slideDown',
easing: @options.easing
duration: @options.duration
queue: false
complete: =>
done()
return
# Otherwise we insert it directly and keep the currently visible element visible by scrolling.
$node.insertBefore(next)
nodeHeight = $node.outerHeight true
# We first move scroll location based on the height which is
# available immediately. This makes position not jump around.
$(window).scrollTop currentScrollStop + nodeHeight
Tracker.afterFlush =>
# But after flush we check if anything has changed (templates can add more
# content in their rendered callback) and move scroll location accordingly.
newNodeHeight = $node.outerHeight true
$(window).scrollTop $(window).scrollTop() + (newNodeHeight - nodeHeight)
done()
removeElement: (node, done) =>
$node = $(node)
$node.velocity 'slideUp',
easing: @options.easing
duration: @options.duration
complete: =>
$node.remove()
done()
Momentum.registerPlugin 'keep-visible', KeepVisible