Search code examples
performancecsswebweb-platforms

How to properly use css will-change property?


Can anyone explain me how to use this property the right way by example? I made a simple demo: http://jsfiddle.net/zlaja/hnb1qbtv/1/ and I wonder where to use this property in CSS or JS ?

body {
    margin: 0;
}
.l-header {
    width: 300px;
    height: 1000px;
    background: red;
    transform: translate3d(-100%, 0, 0);
    transition: transform 0.3s ease-in-out;
    will-change: transform;
    /*Is this the right place ???*/
}
a {
    position: absolute;
    right: 10px;
}
.toggle-header {
    transform: translate3d(0, 0, 0);
}


<a href="#">Menu</a>
<header class="l-header"></header>


var menu = document.querySelector("a"),
    header = document.querySelector(".l-header");

var toogle = function () {
    header.classList.toggle("toggle-header");
}

menu.addEventListener("click", toogle, false);

Solution

  • The short answer: it's likely that you will often pair this with JS, to inject the will-change property when parts of the page are made interactive, to allow the browser to perf-optimize them before the user interacts with them.

    The "long" answer: read Sara Soueidan's article on dev.opera.com.