UNPKG

721 BJavaScriptView Raw
1
2El.bindings.topFloat = function(el, offset) {
3 var h = el.offsetHeight + offset
4 , lastAbs = 0
5 , lastTop = 0
6 , toFix = 0
7
8 El.on(window, "scroll", function(e) {
9 var pos
10 , top = El.scrollTop()
11
12 // scroll up
13 if (top < lastTop) {
14 if (top <= toFix) {
15 el.style.position = "fixed"
16 el.style.top = toFix = 0
17 } else {
18 pos = lastTop - h
19 if (toFix < 0 || lastAbs < pos) {
20 el.style.position = "absolute"
21 el.style.top = (lastAbs = toFix = pos < 0 ? 0 : pos < lastAbs ? lastAbs : pos) + "px"
22 }
23 }
24 } else if (toFix === 0) {
25 el.style.position = "absolute"
26 el.style.top = lastTop + "px"
27 lastAbs = lastTop
28 toFix = -1
29 }
30 lastTop = top
31 })
32}
33
34El.bindings.topFloat.once = 1
35
36