A while back I wrote about adding parallax effects to your HTML/JS experiences to make them feel a bit richer and closer to a native experience.  I’ve just added this subtle (key word *subtle*) effect to a new project and made a few changes I wanted to share here.

If you are wondering what I am talking about with “parallax effects” – Parallax movement is where objects in the background move at a different rate than objects in the foreground, thus causing the perception of depth.  Read more about it if you’re interested.

First, here’s a quick video of this latest app in action.  It’s a hybrid MobileFirst app, but this technique could be used in any Cordova/PhoneGap/MobileFirst/mobile web app experience.  The key is to keep it subtle and not too much “in your face”, and yes, it is very subtle in this video.  You have to watch closely.

The techniques that I wrote about in the previous post still apply – I’ve just added a bit more to cover more use cases.

First let’s look at the CSS.

[css]body {
background-image: url(‘../images/cloud_advisor_bkgd.png’);
background-attachment: fixed;
background-position: center;
background-size: auto 120%;
}[/css]

This sets the background image and default position.  The distinct change here is that I set the background size to “auto” width and 120% height.  In this case, you can have a huge image that shrinks down to just slightly larger than the window size, or a small image that scales up to a larger window size.  This way you don’t end up with seams in a repeated background or a background that is too big to highlight the parallax effect effectively.

Next let’s take a quick look at the JS involved.

[js]var position = "center";
var lastPosition = "center";
var contentCSS = "";
var body = $("body");
var content = $(".content");
window.suspendAnimation = false;

var xMovement = 15;
var yMovement = 30;
var halfX = xMovement/2;
var halfY = yMovement/2;

window.ondeviceorientation = function(event) {
var gamma = event.gamma/90;
var beta = event.beta/180;

var temp = 0;

//fix for holding the device upside down
if ( gamma >= 1 ) {
gamma = 2- gamma;
} else if ( gamma <= -1) {
gamma = -2 – gamma;
}

// shift values/motion based upon device orientation
switch (window.orientation) {
case 90:
temp = gamma;
gamma = beta;
beta = temp;
break;
case -90:
temp = -gamma;
gamma = beta;
beta = temp;
break;

}

// update positions to be used for CSS
var yPosition = -yMovement – (beta * yMovement);
var xPosition = -xMovement – (gamma * xMovement);
var contentX = (-xMovement – xPosition)/2;
var contentY = (-yMovement – yPosition)/2;

// generate css styles
position = xPosition.toFixed(1) + "px " + yPosition.toFixed(1) + "px";
contentCSS = "translate3d( " + (contentX.toFixed(1)) + "px, " + (contentY.toFixed(1)) + "px, " + " 0px)";
}

function updateBackground() {

if (!window.suspendAnimation) {
if ( position.valueOf() != lastPosition.valueOf() ) {

body.css( "background-position", position);
content.css( "-webkit-transform", contentCSS);
lastPosition = position;
}
} else {
lastPosition = "translate3d(0px, 0px, 0px)";;
}

window.requestAnimationFrame(updateBackground);
}

window.requestAnimationFrame(updateBackground);[/js]

The html where this would be used would be something like this:

[html]<body>
<div class="content">put your foreground stuff here.</div>
</body>[/html]

There are some subtle but important changes here:

  1. In the requestAnimationFrame loop, it only applies changes *if* there are changes to apply.  This prevents needless calls to apply CSS even if the CSS styles hadn’t changed.  In this, I also truncate the numeric CSS string so that it isn’t reapplying CSS if the position should shift by 0.01 pixels. Side note: If you aren’t using requestAnimationFrame for HTML animations, you should learn about it.
  2. If you used my old code and were holding the device upside down, it wouldn’t work.  Not even a little bit.  This has that fixed (see comments inline above).
  3. This moves the background in CSS, which doesn’t cause browser reflow operations, and moves the foreground content (inside of a div) using translate3d, which also doesn’t cause browser reflow operations.  This helps keep animations smooth and the UX performing optimally.
  4. I also added a global variable to turn parallax on and off very quickly, if you need it.

The result is a faster experience that is more efficient and less of a strain on CPU and battery.  Feel free to test this technique out on your own.

If you use the code above, you can modify the xMovement and yMovement variables to exaggerate the parallax effect.