Skip to content

Instantly share code, notes, and snippets.

@rcherny
Forked from drinks/gist:2024654
Created October 10, 2012 19:10
Show Gist options
  • Save rcherny/3867752 to your computer and use it in GitHub Desktop.
Save rcherny/3867752 to your computer and use it in GitHub Desktop.
$.scrollalax
(function($){
$.fn.scrollalax = function(opts){
var el = $(this)
, options = {}
, defaults ={
'speed': 80
}
, scroller
, init = function(){
options = $.extend(options, defaults, opts);
if(el[0].tagName.match(/^(body|html)$/i)){
scroller = $(window);
}else{
scroller = el;
}
$(scroller).scroll(handleScroll);
return el;
}
, handleScroll = function(e){
el.css('backgroundPosition', ($(scroller).scrollLeft() * -1 * (options['speed'] / 100)) + 'px ' + ($(scroller).scrollTop() * -1 * (options['speed'] / 100)) + 'px');
}
return init();
};
})(jQuery);
// use it like this:
// (function($){
// $('body').scrollalax() // higher number = faster speed
// })(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment