Created
September 3, 2012 21:30
-
-
Save ken210/3613726 to your computer and use it in GitHub Desktop.
jQuery.parallax.coffee
This file contains hidden or 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
### | |
jQuery.parallax | |
by ken210.com | |
Based on homonymous plugin by Ian Lunn | |
Author URL: http://www.ianlunn.co.uk/ | |
Plugin URL: http://www.ianlunn.co.uk/plugins/jquery-parallax/ | |
Dual licensed under the MIT and GPL licenses: | |
http://www.opensource.org/licenses/mit-license.php | |
http://www.gnu.org/licenses/gpl.html | |
### | |
$.fn.parallax = (options) -> | |
defaults = | |
bgPosX: '50% ' | |
bgPosY: '50%' | |
adjuster: 0 | |
inertia: 0.1 | |
outerHeight: true | |
mode: 'backgroundPosition' | |
$.extend(defaults, options) | |
@each -> | |
self = $(this) | |
top = self.position().top | |
height = if defaults.outerHeight then self.outerHeight(true) else self.height() | |
# caching usefull props | |
mode = defaults.mode | |
bgPosX = defaults.bgPosX | |
adjuster = defaults.adjuster | |
inertia = defaults.inertia | |
getStyle = (now) -> | |
output = '' | |
expression = Math.round((adjuster - height - now) * inertia) | |
if (mode == 'backgroundPosition') | |
output = bgPosX + expression + 'px' | |
else | |
output = parseInt(top + (top - expression)) + 'px' | |
return output | |
onScrolled = () -> | |
now = $(scrollContainer).scrollTop() | |
self.css(mode, getStyle(now)) | |
### | |
cssObj = {} | |
cssObj[mode] = getStyle(now) | |
self.stop().animate(cssObj, 300) | |
### | |
$(scrollContainer).bind(scrollEvent, onScrolled) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment