How to make faster scroll effects?

  • Avoid too many reflows (the browser to recalculate everything)
  • Use advanced CSS3 for graphic card rendering
  • Precalculate sizes and positions

Beware of reflows

The reflow appens as many times as there are frames per seconds. It recalculate all positions that change in order to diplay them. Basically, when you scroll you execute a function where you move things between two reflows. But there are functions that triggers reflows such as jQuery offset, scroll... So there are two things to take care about when you dynamically change objects in javascript to avoid too many reflows:

  • use window.pageYOffset to get the scroll position
  • prefer requestAnimationFrame than onScroll event.

Why is requestAnimationFrame (really) faster than the scroll event? Actually the scroll event popups whenever it wants, even if you haven't finished calculating the previous position, or between two reflows... While the requestAnimationFrame can only popup if the previous frame was calculated. So you save reflows out of your loop function, and you manage to never overlap two calculations.

// Detect request animation frame
var scroll = window.requestAnimationFrame ||
             window.webkitRequestAnimationFrame ||
             window.mozRequestAnimationFrame ||
             window.msRequestAnimationFrame ||
             window.oRequestAnimationFrame ||
             // IE Fallback, you can even fallback to onscroll
             function(callback){ window.setTimeout(callback, 1000/60) };

function loop(){

    var top = window.pageYOffset;

    // Where the magic goes
    // ...

    // Recall the loop
    scroll( loop )
}

// Call the loop for the first time
loop();

Why it's worth the effort?

Because in a lot of cases, we use some kind of concatenation programs to concat our code. Looking at these code separately, they are all fine, but sometimes... Look what will happen if the following two snippets get concatenated, note that there's no line break at the end of code1.js?

code1.js

//here is some comments on the end of the file

code2.js

/**
Some lic statement...
**/
... Some real code here

When they are concatenated, it becomes this

//here is some comments on the end of the file/**
Some lic statement...
**/
... Some real code here

The comment start symbol /** is commented out by the previous file, ORZ...

It is always hard to guess how users browse + how they perceive the content and flow of a website or web app.

There are tools to analyze these as much as possible and, using methods like A/B testing, improving the usability is always possible.

However, it is always a great idea to guide users whenever they want. Nothing to lose on that for sure.

There are awesome jQuery plugins that ease guiding users and creating site/page tours for them. They are stylish, customizable and simple to use. Check them out:

Intro.js

Intro.js jQuery Plugin

A lightweight plugin that uses data attributes and has keyboard navigation support.

It focuses on objects beautifully and pretty easy-to-setup.

Intro.js jQuery Plugin