From 271efb8565247436a1c37ef456ede246728fc9bd Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 14 Aug 2015 11:34:08 +0100 Subject: [PATCH] Added feature --- parallax.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/parallax.js b/parallax.js index 341083f..4719a29 100644 --- a/parallax.js +++ b/parallax.js @@ -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"); });