From 0a3cf31b37b50965fc8c4a7d53dfd8d54aa9b0f7 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 13 Aug 2015 13:36:19 +0100 Subject: [PATCH] Uploaded initial code --- parallax.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 parallax.js diff --git a/parallax.js b/parallax.js new file mode 100644 index 0000000..2d8b64b --- /dev/null +++ b/parallax.js @@ -0,0 +1,24 @@ +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"); + }); +}