Created
January 25, 2012 13:50
-
-
Save Ahrengot/1676340 to your computer and use it in GitHub Desktop.
Class for managing items with parallax scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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