archive
/
simparallax
Archived
1
Fork 0

Added feature

This commit is contained in:
Jake Howard 2015-08-14 11:34:08 +01:00
parent db5afc91f9
commit 271efb8565
1 changed files with 19 additions and 8 deletions

View File

@ -2,10 +2,12 @@ var Parallax = {
initialise: function(createEvents) {
$(this.ident).each(function(){
// Calculate Offset
inset = $(this).attr('parallax-inset');
image_height = $(this).height();
range = image_height / inset;
$(this).attr('parallax-offset', range / 2);
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);
}
});
console.log("Parallax Initialised.");
if (createEvents) {this.createEvents();}
@ -19,14 +21,23 @@ var Parallax = {
$(this.ident).each(function(){
// If the element isn't on the screen, or wasn't initialised, ignore it.
if (!is_on_screen(this) || !$(this).attr('parallax-offset')) { return; }
if (!is_on_screen(this)) { return; }
element = $(this);
//Calculate the center position of the image
center_pos = (element.offset().top - $(window).scrollTop()) + (element.height() / 2);
center_pos = (element.offset().top - window_top) + (element.height() / 2);
// Calculate the position of the center point, relative to the center.
perc = -(0.5 - ((window_top - center_pos) / window_bottom));
// Calculate the position of the center of the image from the percentage through the screen.
pos = Math.round((perc * element.attr('parallax-offset')) * 100) / 100;
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;
}
// Apply the calculated value.
element.css('background-position',"0px " + pos + "px");
});