Skip to content

Instantly share code, notes, and snippets.

@koohz
Created December 23, 2016 13:39
Show Gist options
  • Save koohz/097d51bb1d4bdf0733aca94ba572efdf to your computer and use it in GitHub Desktop.
Save koohz/097d51bb1d4bdf0733aca94ba572efdf to your computer and use it in GitHub Desktop.
Parallax Background Image
// 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