Update 2:
Running the JSFiddle below, in Chrome 31.0.1650.34 beta now does not result in the described behaviour i.e. it does not "freeze as the JavaScript kicks in". I can only assume they have placed the CSS transitions on a separate thread from JavaScript, and the rest of the page—good news! The freezing/blocked transition still does appear in Firefox 25.0.
Update 1:
@IvanCastellanos mentioned that CSS transitions and animations are not blocked on Android Chrome. This is extremely useful information, and partially motivated this question.
Original Question:
This might be more of a question for the browser vendors, but here goes: Until now I was under the impression from this video (and others) that transitioning CSS opacity wouldn't really suffer from any performance issues.
In the video Paul Irish specifically gives the impression that transitioning opacity just isn't going to be a problem and "anyone that tells you otherwise is just ...wrong".
Well, if that's the case, this fiddle makes me wrong.
Why is the CSS transition being blocked by JavaScript, given Paul's extraordinary claims? This is also the case for animations, what's going on?
(For those of you either not seeing what I'm seeing, or too lazy to view the fiddle: I see a red square make it about 1/5 the way through a fade-in transition and then freeze as the JavaScript kicks in, then the squares jump to the end of the transition to full opacity, presumably because the duration has been reached during JavaScript execution.)
Given that stackoverflow is requiring I post some code because I'm linking to jsfiddle, here's the relavant JavaScript and CSS:
(function () {
"use strict";
var isVisible = false;
function handleClick() {
var fadingSquare = document.querySelector(".fadingSquare"),
i;
if (isVisible === false) {
fadingSquare.className = fadingSquare.className + " active";
// Do something intensive in JavaScript for a while
setTimeout(function () {
for(i = 0; i < 10000; i += 1) {
console.log(i);
}
}, 200); // Make it occur midway through the CSS transition
isVisible = true;
} else {
fadingSquare.className = fadingSquare.className.replace("active", "");
isVisible = false;
}
}
document.addEventListener("click", handleClick, false);
}());
And CSS:
.fadingSquare {
width: 200px;
height: 200px;
background: #F00;
opacity: 0;
-webkit-transition-property: opacity;
-webkit-transition-duration: 1s;
}
.fadingSquare.active {
opacity: 1;
}
The second most important factor for high-quality animations is to make sure that they do not block the JavaScript thread. To keep animations fluid and not interrupt UI interactions, the render loop has to render each frame in 16.67 ms, so that 60 FPS can be achieved.
CSS allows animation of HTML elements without using JavaScript or Flash! In this chapter you will learn about the following properties: @keyframes.
TL;DR # Use CSS animations for simpler "one-shot" transitions, like toggling UI element states. Use JavaScript animations when you want to have advanced effects like bouncing, stop, pause, rewind, or slow down.
CSS animations make it possible to do simple animations without JavaScript at all. JavaScript can be used to control CSS animations and make them even better, with little code.
Javascript runs on the browser's UI thread.
The entire page is blocked by Javascript; not just CSS transition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With