Background
I was asked to write a script that would create a vertical scrolling parallax background effect for the project. My initial attempt looks something like this:
(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); return this.each(function(){ $(window).bind('scroll', function() { windowTop = $(window).scrollTop(); if((windowTop >= opts.start) && (windowTop <= opts.stop)) { newCoord = windowTop * opts.coeff; $$.css({ "background-position": "0 "+ newCoord + "px" }); } }); }); }; })(jQuery);
This code gives the desired effect, but binding to a scroll event is a real performance leak.
Question
Part 1. How can I change my plugin to be more efficient? Part 2. Are there resources (books, links, tutorials) that I can read to learn more?
source share