Skip to content

Instantly share code, notes, and snippets.

@Ahrengot
Created January 25, 2012 13:50
Show Gist options
  • Save Ahrengot/1676340 to your computer and use it in GitHub Desktop.
Save Ahrengot/1676340 to your computer and use it in GitHub Desktop.
Class for managing items with parallax scroll
function ParallaxScroll() {
this.$window = $(window);
this.items = new Array();
this.init = function() {
this.$window.scroll(doParallax);
}
var doParallax = function() {
// Check we have any items before we do any scolling
var items = window.parallaxScroll.items;
if (items == undefined || items.length < 1) return false;
// Alright, we've got items. Continue:
var $w = window.parallaxScroll.$window,
amount = $w.scrollTop();
for (var i in items) {
var val = Math.round(amount * items[i].p);
items[i].el.css('top', (val > 0)? val : 0);
}
}
this.addItem = function($el, parallax) {
this.items.push({el: $el, p: parallax});
doParallax();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment