From 4078f06d61d1e84712a34ba5485d51413ba21410 Mon Sep 17 00:00:00 2001 From: Florian Jenett Date: Mon, 8 Dec 2014 12:40:10 +0100 Subject: [PATCH 1/3] fixed $.offset().top usage As per API definition offset() returns the position of the FIRST element, using $this will cause to return always the same value. --- scripts/jquery.parallax-1.1.3.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/jquery.parallax-1.1.3.js b/scripts/jquery.parallax-1.1.3.js index f3569dc..8c085af 100644 --- a/scripts/jquery.parallax-1.1.3.js +++ b/scripts/jquery.parallax-1.1.3.js @@ -26,8 +26,8 @@ http://www.gnu.org/licenses/gpl.html var paddingTop = 0; //get the starting position of each element to have parallax applied to it - $this.each(function(){ - firstTop = $this.offset().top; + $this.each(function(i,e){ + firstTop = $(e).offset().top; }); if (outerHeight) { From c5eb6e3ed65504874b7968577c54dc5895ead643 Mon Sep 17 00:00:00 2001 From: Florian Jenett Date: Mon, 8 Dec 2014 12:59:07 +0100 Subject: [PATCH 2/3] firstTop should be stored per element The `firstTop` variable stores the top of the first element only, should instead store per element. --- scripts/jquery.parallax-1.1.3.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/jquery.parallax-1.1.3.js b/scripts/jquery.parallax-1.1.3.js index 8c085af..9dac737 100644 --- a/scripts/jquery.parallax-1.1.3.js +++ b/scripts/jquery.parallax-1.1.3.js @@ -27,7 +27,8 @@ http://www.gnu.org/licenses/gpl.html //get the starting position of each element to have parallax applied to it $this.each(function(i,e){ - firstTop = $(e).offset().top; + $e = $(e); + $e.data('firstTop', $e.offset().top); }); if (outerHeight) { @@ -49,8 +50,8 @@ http://www.gnu.org/licenses/gpl.html function update(){ var pos = $window.scrollTop(); - $this.each(function(){ - var $element = $(this); + $this.each(function(i,e){ + var $element = $(e); var top = $element.offset().top; var height = getHeight($element); @@ -59,7 +60,7 @@ http://www.gnu.org/licenses/gpl.html return; } - $this.css('backgroundPosition', xpos + " " + Math.round((firstTop - pos) * speedFactor) + "px"); + $element.css('backgroundPosition', xpos + " " + Math.round(( $e.data('firstTop') - pos) * speedFactor) + "px"); }); } From 9dfca58ed903a9832553181b39c3deb0d3018188 Mon Sep 17 00:00:00 2001 From: Florian Jenett Date: Mon, 8 Dec 2014 13:08:52 +0100 Subject: [PATCH 3/3] Fixed a typo and did minor cleanup $e should have been $element; removed unused firstTop variable; renamed $e to $element --- scripts/jquery.parallax-1.1.3.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scripts/jquery.parallax-1.1.3.js b/scripts/jquery.parallax-1.1.3.js index 9dac737..0a06b62 100644 --- a/scripts/jquery.parallax-1.1.3.js +++ b/scripts/jquery.parallax-1.1.3.js @@ -22,13 +22,12 @@ http://www.gnu.org/licenses/gpl.html $.fn.parallax = function(xpos, speedFactor, outerHeight) { var $this = $(this); var getHeight; - var firstTop; var paddingTop = 0; //get the starting position of each element to have parallax applied to it $this.each(function(i,e){ - $e = $(e); - $e.data('firstTop', $e.offset().top); + $element = $(e); + $element.data('firstTop', $element.offset().top); }); if (outerHeight) { @@ -60,7 +59,7 @@ http://www.gnu.org/licenses/gpl.html return; } - $element.css('backgroundPosition', xpos + " " + Math.round(( $e.data('firstTop') - pos) * speedFactor) + "px"); + $element.css('backgroundPosition', xpos + " " + Math.round(( $element.data('firstTop') - pos) * speedFactor) + "px"); }); }