A brief discussion on whether CSS animation will be blocked by JS

A brief discussion on whether CSS animation will be blocked by JS

The animation part of CSS will be blocked by JS, but the animation of transform will not be affected.

Here is a performance chart of the margin-left moving animation with js blocking animation enabled.

<style>
.walkabout-old-school {
  animation: 3s slide-margin linear infinite;
}
 
@keyframes slide-margin {
  from {margin-left: 0;}
  50% {margin-left: 100%;}
  to {margin-left: 0;}
}
</style>
<script>
function kill() {
  var start = +new Date;
  while (+new Date - start < 2000){}
}
</script> 

In the above figure, it can be clearly seen that after running the kill method, the animation stagnates for 2 seconds before continuing.

From the above figure, we can see that within 2 seconds of enabling js, the rendering process will wait until the kill function is executed before executing. Looking at the figure below, we can see that changes in margin will cause the DOM to be re-layouted, and the layout will not start until the kill function is executed. will lead to

Then look at the elements that use transform as animation

.walkabout-new-school {
  animation: 3s slide-transform linear infinite;
}
@keyframes slide-transform {
  from {transform: translatex(0);}
  50% {transform: translatex(300px);}
  to {transform: translatex(0);}
}

The following figure is a running comparison chart (blue is margin, green is transform)

From the above figure, we can see that the use of transform is not affected by the kill method at all. Let's take a look at the debugging chart

It can be seen here that margin will frequently trigger page reflow, while transform will not, and js blocks page reflow. So the margin animation will get stuck.

Therefore, when using animation in daily life, using transform more often can achieve the best page performance and effects.

This is the end of this article on whether CSS animation will be blocked by JS. For more relevant content about CSS animation being blocked by JS, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  Detailed steps to install nginx on Apple M1 chip and deploy vue project

>>:  Practical record of solving MySQL deep paging problem

Recommend

A brief explanation of the reasonable application of table and div in page design

At the beginning of this article, I would like to ...

How to implement data persistence using the vuex third-party package

Purpose: Allow the state data managed in vuex to ...

How to set up the terminal to run applications after Ubuntu starts

1. Enter start in the menu bar and click startup ...

How complicated is the priority of CSS styles?

Last night, I was looking at an interview question...

CSS3 creates web animation to achieve bouncing ball effect

Basic preparation For this implementation, we nee...

25 Tools to Improve Website Usability and Conversion Rates

For a website, usability refers to whether users c...

Several ways to implement 0ms delay timer in js

Table of contents queueMicrotask async/await Mess...

Weird and interesting Docker commands you may not know

Intro Introduces and collects some simple and pra...

mysql5.6.zip format compressed version installation graphic tutorial

Preface: MySQL is a relational database managemen...

How to implement MySQL bidirectional backup

MySQL bidirectional backup is also called master-...

Detailed tutorial on installing MySQL 8.0.20 database on CentOS 7

Related reading: MySQL8.0.20 installation tutoria...

Detailed explanation of HTML area tag

The <area> tag defines an area in an image ...