archive
/
simparallax
Archived
1
Fork 0
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.
simparallax/parallax.js

61 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2015-08-13 13:43:42 +01:00
var Parallax = {
2015-08-13 14:04:18 +01:00
initialise: function(createEvents) {
$(this.ident).each(function(){
// Calculate Offset
2015-08-14 11:34:08 +01:00
if ($(this).attr('parallax-inset') && $(this).attr('parallax-mode') == 'inset') {
inset = $(this).attr('parallax-inset');
image_height = $(this).height();
range = image_height / inset;
$(this).attr('parallax-offset', range / 2);
}
2015-08-13 14:04:18 +01:00
});
console.log("Parallax Initialised.");
if (createEvents) {this.createEvents();}
2015-08-13 15:19:41 +01:00
//Calculate the initial positions of the images.
2015-08-13 14:04:18 +01:00
this.calculate();
2015-08-13 13:43:42 +01:00
},
calculate: function() {
2015-08-13 15:19:41 +01:00
// Get the window dimentions.
2015-08-13 14:04:18 +01:00
window_top = $(window).scrollTop();
window_bottom = window_top + $(window).height();
2015-08-13 15:19:41 +01:00
2015-08-13 14:04:18 +01:00
$(this.ident).each(function(){
2015-08-13 15:19:41 +01:00
// If the element isn't on the screen, or wasn't initialised, ignore it.
2015-08-14 11:34:08 +01:00
if (!is_on_screen(this)) { return; }
2015-08-13 14:04:18 +01:00
element = $(this);
2015-08-13 15:19:41 +01:00
//Calculate the center position of the image
2015-08-14 11:34:08 +01:00
center_pos = (element.offset().top - window_top) + (element.height() / 2);
2015-08-13 15:19:41 +01:00
// Calculate the position of the center point, relative to the center.
2015-08-13 14:04:18 +01:00
perc = -(0.5 - ((window_top - center_pos) / window_bottom));
2015-08-14 11:34:08 +01:00
switch(element.attr('parallax-mode')) {
case 'inset':
pos = Math.round((perc * element.attr('parallax-offset')) * 100) / 100;
console.log(pos)
break;
case 'linear':
default:
pos = perc*(element.height()/2);
break;
}
2015-08-13 15:19:41 +01:00
// Apply the calculated value.
2015-08-13 14:04:18 +01:00
element.css('background-position',"0px " + pos + "px");
});
2015-08-13 13:43:42 +01:00
},
2015-08-13 14:04:18 +01:00
createEvents: function() {
2015-08-13 15:19:41 +01:00
// Create scroll and resize events for Parallax
2015-08-13 14:04:18 +01:00
$(window).scroll(function() { Parallax.calculate(); });
2015-08-13 14:10:00 +01:00
$(window).resize(function() { Parallax.initialise(false); })
2015-08-13 13:43:42 +01:00
},
2015-08-13 15:19:41 +01:00
ident: '.parallax' //The identifier for Parallax elements
2015-08-13 13:43:42 +01:00
}
2015-08-13 15:19:41 +01:00
// Check if the element is on the screen, not necessarily all of it.
2015-08-13 13:43:42 +01:00
function is_on_screen(elm) {
2015-08-13 14:04:18 +01:00
var vpH = $(window).height(),
st = $(window).scrollTop(),
2015-08-13 13:43:42 +01:00
y = $(elm).offset().top,
elementHeight = $(elm).height();
return ((y < (vpH + st)) && (y > (st - elementHeight)));
}