Last active
June 25, 2016 04:29
-
-
Save matteocaberlotto/5ca7cb6bd738c9f98ff7 to your computer and use it in GitHub Desktop.
Parallax js class
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
/** | |
* Simple parallax helper: calls all registered callbacks with current scroll top as parameter. | |
*/ | |
var Parallax = (function () { | |
var windowWidth = null; | |
var windowHeight = null; | |
var y = null; | |
var y_norm = null; | |
var y_norm_to_current = null; | |
var current_page = null; | |
var max_y = null; | |
var max_scroll = null; | |
var callbacks = []; | |
var bounds = []; | |
var resizeTimeout = null; | |
var container = null; | |
var disabled = null; | |
var setup = function (config) { | |
var defaults = { | |
selector: '.parallax-page' | |
}; | |
config = $.extend(defaults, config); | |
if (!config.hasOwnProperty('container')) { | |
config.container = window; | |
} | |
container = config.container; | |
windowWidth = $(window).width(); | |
windowHeight = $(window).height(); | |
max_y = $(config.container).outerHeight(); | |
max_scroll = $(config.container).outerHeight() - $(window).height(); | |
bounds[0] = 0; | |
$(config.selector) | |
.each(function (i) { | |
bounds[i+1] = bounds[i] + $(this).outerHeight(); | |
}); | |
// console.log('setup done'); | |
}; | |
var runCallbacks = function () { | |
$.each(callbacks, function (e) { | |
this(y, y_norm, y_norm_to_current, windowWidth, windowHeight); | |
}); | |
}; | |
var update = function (yp) { | |
if (disabled) { | |
return; | |
} | |
if (typeof yp === 'undefined') { | |
y = $(container).scrollTop(); | |
} else { | |
y = yp; | |
} | |
current_page = Parallax.getPage(); | |
y_norm = y / max_scroll; | |
y_norm_to_current = (y - bounds[current_page - 1]) / (bounds[current_page] - bounds[current_page - 1]); | |
runCallbacks(); | |
}; | |
return { | |
init: function (config) { | |
// no callbacks => no parallax | |
if (callbacks.length === 0) { | |
return; | |
} | |
setup(config); | |
update(); | |
if (config.hasOwnProperty('setupCallback')) { | |
config.setupCallback(); | |
} else { | |
$(container) | |
.scroll(function (e) { | |
update(); | |
}); | |
} | |
$(window) | |
.resize(function () { | |
clearTimeout(resizeTimeout); | |
resizeTimeout = setTimeout(function () { | |
setup(config); | |
}, 300); | |
}); | |
}, | |
update: function (y) { | |
update(y); | |
}, | |
isPage: function (query) { | |
if (y >= bounds[query - 1] && y < bounds[query]) { | |
return true; | |
} | |
return false; | |
}, | |
getPage: function () { | |
var j; | |
for (var j = 1; j <= bounds.length; j++) { | |
if (Parallax.isPage(j)) { | |
return j; | |
} | |
} | |
}, | |
addCallback: function (callback) { | |
callbacks.push(callback); | |
}, | |
disable: function () { | |
disabled = true; | |
}, | |
enable: function () { | |
disabled = false; | |
} | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment