From d81f7770e60d8de4da2387f492696804b5693521 Mon Sep 17 00:00:00 2001 From: Mathieu 'p01' Henri Date: Wed, 14 May 2014 16:47:04 +0200 Subject: [PATCH] Animate only when the scroll position changes The previous test had a few issues: 1. Didn't call animateElements when reaching the top of the page 2. Didn't account for float windowHeight and bodyHeight and thus occasionally didn't call animateElements when reaching the bottom of the page 3. Called animateElements even if the scroll position didn't change --- parallax/js/picasso.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/parallax/js/picasso.js b/parallax/js/picasso.js index 6ae8eef..da28913 100644 --- a/parallax/js/picasso.js +++ b/parallax/js/picasso.js @@ -13,6 +13,7 @@ windowHeight = 0, windowWidth = 0, prevKeyframesDurations = 0, + prevScrollTop = 0, scrollTop = 0, relativeScrollTop = 0, currentKeyframe = 0, @@ -357,9 +358,10 @@ updatePage = function() { window.requestAnimationFrame(function() { setScrollTops(); - if(scrollTop > 0 && scrollTop <= (bodyHeight - windowHeight)) { + if(prevScrollTop != scrollTop) { animateElements(); setKeyframe(); + prevScrollTop = scrollTop; } }); } @@ -447,4 +449,4 @@ init(); }) -}).call(this); \ No newline at end of file +}).call(this);