Created
December 23, 2016 13:39
-
-
Save koohz/097d51bb1d4bdf0733aca94ba572efdf to your computer and use it in GitHub Desktop.
Parallax Background Image
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
// The plugin code | |
(function($){ | |
$.fn.parallax = function(options){ | |
var $$ = $(this); | |
offset = $$.offset(); | |
var defaults = { | |
"start": 0, | |
"stop": offset.top + $$.height(), | |
"coeff": 0.95 | |
}; | |
var opts = $.extend(defaults, options); | |
var updateBgPos = function() { | |
windowTop = $(window).scrollTop(); | |
if((windowTop >= opts.start) && (windowTop <= opts.stop)) { | |
newCoord = windowTop * opts.coeff; | |
$$.css({ | |
"background-position": "0 "+ newCoord + "px" | |
}); | |
} | |
} | |
return this.each(function(){ | |
$(window).bind('scroll', updateBgPos); | |
}); | |
}; | |
$("[data-parallax]").each(function(){ | |
$(this).parallax({ coeff: $(this).data("parallax") }) | |
}) | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment