Last active
August 29, 2015 14:17
-
-
Save en30/24d4c5b901f453774358 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = require 'lodash' | |
module.exports = ({target, topBarrier, upperBound, bottomBarrier, lowerBound, wait, stickIf})-> | |
getLowerBound = (topBarrier)-> | |
return 0 unless topBarrier | |
topBarrier = topBarrier?() || topBarrier | |
topBarrierRect = topBarrier.getBoundingClientRect() | |
window.scrollY + topBarrierRect.bottom | |
getUpperBound = (bottomBarrier)-> | |
return document.documentElement.getBoundingClientRect().height unless bottomBarrier | |
bottomBarrier = bottomBarrier?() || bottomBarrier | |
bottomBarrierRect = bottomBarrier.getBoundingClientRect() | |
window.scrollY + bottomBarrierRect.top | |
top = dy = 0 | |
_.throttle( | |
-> | |
return if typeof stickIf == 'function' and !stickIf() | |
oldDy = dy | |
dy = window.scrollY - top | |
top = window.scrollY | |
bottom = top + document.documentElement.clientHeight | |
target = target?() || target | |
targetRect = target.getBoundingClientRect() | |
targetTop = top + targetRect.top | |
targetBottom = top + targetRect.bottom | |
lb = lowerBound || getLowerBound(topBarrier) | |
ub = upperBound || getUpperBound(bottomBarrier) | |
cssText = if dy <= 0 && targetTop <= lb | |
"position: absolute; top: #{lb}px" | |
else if dy >= 0 && targetBottom >= ub | |
"position: absolute; top: #{ub - targetRect.height}px" | |
else if dy <= 0 && top < targetTop | |
"position: fixed; top: 0" | |
else if dy >= 0 && bottom > targetBottom | |
"position: fixed; bottom: 0" | |
else if dy * oldDy < 0 | |
"position: absolute; top: #{targetTop}px" | |
target.style.cssText = cssText if cssText && target.style.cssText != cssText | |
wait | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "stickifier", | |
"version": "0.0.2", | |
"description": "Make elements sticky", | |
"main": "main.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"postinstall": "coffee -bc main.coffee" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "" | |
}, | |
"keywords": [ | |
], | |
"author": "en30 <en30.git@gmail.com>", | |
"license": "MIT", | |
"dependencies": { | |
"lodash": "^2.4.1", | |
"coffee-script": "^1.8.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to install
package.json
TODO
Tidy up