This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
2015-08-13 13:43:42 +01:00
|
|
|
var Parallax = {
|
|
|
|
initialise: function() {
|
|
|
|
|
|
|
|
},
|
|
|
|
calculate: function() {
|
|
|
|
|
|
|
|
},
|
|
|
|
events: function() {
|
|
|
|
|
|
|
|
},
|
2015-08-13 13:44:14 +01:00
|
|
|
ident: '.parallax'
|
2015-08-13 13:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function is_on_screen(elm) {
|
|
|
|
|
|
|
|
var vpH = $(window).height(), // Viewport Height
|
|
|
|
st = $(window).scrollTop(), // Scroll Top
|
|
|
|
y = $(elm).offset().top,
|
|
|
|
elementHeight = $(elm).height();
|
|
|
|
return ((y < (vpH + st)) && (y > (st - elementHeight)));
|
|
|
|
}
|
2015-08-13 13:36:19 +01:00
|
|
|
function init_parallax() {
|
|
|
|
$('.parallax').each(function(){
|
|
|
|
// Calculate Offset
|
|
|
|
inset = $(this).attr('parallax-inset');
|
|
|
|
image_height = $(this).height();
|
|
|
|
range = image_height / inset;
|
|
|
|
$(this).attr('parallax-offset', range / 2);
|
|
|
|
});
|
|
|
|
console.log("Parallax Initialised.");
|
|
|
|
parallax();
|
|
|
|
}
|
|
|
|
function parallax() {
|
|
|
|
window_top = $(window).scrollTop();
|
|
|
|
window_bottom = window_top + $(window).height();
|
|
|
|
$('.parallax').each(function(){
|
|
|
|
if (!is_on_screen(this) || !$(this).attr('parallax-offset') != false) { return; }
|
|
|
|
element = $(this);
|
|
|
|
center_pos = (element.offset().top - $(window).scrollTop()) + (element.height() / 2);
|
|
|
|
perc = -(0.5 - ((window_top - center_pos) / window_bottom));
|
|
|
|
|
|
|
|
pos = Math.round((perc * element.attr('parallax-offset')) * 100) / 100;
|
|
|
|
element.css('background-position',"0px " + pos + "px");
|
|
|
|
});
|
|
|
|
}
|