Skip to content

Instantly share code, notes, and snippets.

@lipelopeslage
Created November 21, 2012 18:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lipelopeslage/4126702 to your computer and use it in GitHub Desktop.
Save lipelopeslage/4126702 to your computer and use it in GitHub Desktop.
My parallax secret
/*########################################################################*/
/*###### using jQuery selector for a fast/cross-browser resolution #######*/
/*########################################################################*/
var totalAreas = 6, // this value is obviously arbitrary
winH = $(window).height, // the window height
top = $(window).scrollTop(), // the current scroll value
maxScroll = $(document).height() - winH, // the maximum scroll value
scrollPerc = top/maxScroll, // the current scroll percentage
areaPercent = 0; // init variable
// just logging the percentage, from here is up to you
function logPercent(index, percent) {
console.log("Area index: "+index+" is "+percent+"%. Calculate proportions!");
}
/* calculate the percentage from each area */
for(var i = 0; i < totalAreas; i++){//######### here is the catch: the percent of the total scroll times the total of areas minus the current index, plus one :)
areaPercent = 1 + ((scrollPerc * totalAreas) - i);
logPercent(i, areaPercent); // loggin result
}
//######################################################################
//##### PS: Assuming the areas has the same height, the window height
@lipelopeslage
Copy link
Author

LIVE EXAMPLE AT: www.fullsocialwifi.com.br

@lipelopeslage
Copy link
Author

LIVE EXAMPLE AT: www.fullsocialwifi.com.br

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment