Tips and tricks for Windows 8 JavaScript developer: How to create a cool parallax background

Parallax background is a really simple but extremely cool feature you can add to your application. You just need a div containing your background picture and a listview on top of it:

<div id="backgroundHolder">
<div class="blocksList" id="blocksList" data-win-control="WinJS.UI.ListView">

Then you have to use this CSS to display background picture larger than the current window:

#backgroundHolder {
    background-size: 120% 100%;
    height: 100%;
    width: 100%;


Next you have to add a cool picture as a background picture of your div.

Then with the little following function you can handle the scroll event of your list and move the position of your background:

 var attachParallax = function (listQuery) {
     var listViewDOM = document.querySelector(listQuery);
     var listView = listViewDOM.winControl;
     var backGroundHolder = document.querySelector("#backgroundHolder");
     document.querySelector(listQuery + " .win-viewport").addEventListener("scroll", function (e) {
         setImmediate(function() {
   ["background-position-x"] = 
-((listViewDOM.scrollLeft / listViewDOM.scrollWidth) *
listViewDOM.clientWidth * 0.2) +
"px"; }); }); };

Considering that the background image is 20% larger than the main window, the idea of this function is to get a scaling ratio based on the scroll bar position. The result is used to move the left position of the background (from 0 to 20% of the window’s width).

But we have an issue with the smoothness of the translation. To resolve this problem, you can use the wonderful power of CSS3 transitions Sourire

#backgroundHolder {
    background-size: 120% 100%;
    height: 100%;
    width: 100%;
    transition-property: background-position-x;
    transition-timing-function: ease;
    transition-duration: 0.8s;

So thanks to these CSS rules, the position of the background will change smoothly instead of jumping from a position to the next one!

Obviously the next version of UrzaGatherer will include this feature!

At the begining:


Then after you scroll a bit, you can see that the background (1) only moved a few pixels where the foreground (2) moved almost the entire screen:


Comments (8)

  1. philk says:

    Uhm…where  is scrollViewer coming from?

  2. Oups it was a typo. scrollViewer = listViewDOM

  3. JS says:


    thnx for this post. I'm trying to use it but my listviewdom.scrollLeft stays 0px always. Obviously i'm scrolling to the right , that's why there's the minus in front of it. But the event is just not triggered.

    Do you know what can cause this

    the background-position is always 0px;

    many thnx

  4. Sinthiya says:

    Whats up very nice <a href="">website</a&gt;

    !! Man .. Beautiful .. Wonderful .. I will bookmark your site and take the feeds also?I am satisfied to seek out so many helpful information right here within the put up, we need work out more strategies in this regard, thank you for sharing. . . . . .

  5. Ravi says:

    This is a great example for all beginners.

    Thanks for sharing this.

    Could you please tell me how to add scroll event to the ListView (here blockList)?


  6. philk says:

    Could this also be used for the Hub/Pivot controls of WinJS 2.x?

  7. David Catuhe says:

    I think so

  8. Awais Tariq says:

    Hi there,

    Just like Philk, I am wondering how can I use it with Pivot for windows phone using winjs2.0?

    Thanks a lot for your help.