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

Detailed explanation of React component communication

Table of contents Component Communication Introdu...

Introduction to keyword design methods in web design

Many times, we ignore the setting of the web page ...

Some details about semicolons in JavaScript

Preface Semicolons in JavaScript are optional, an...

Vue button permission control introduction

Table of contents 1. Steps 1. Define buttom permi...

How to modify the default storage location of Docker images (solution)

Due to the initial partitioning of the system, th...

Perfect solution for vertical centering of form elements

Copy code The code is as follows: <!DOCTYPE ht...

How to use worm replication in Mysql data table

To put it simply, MySQL worm replication is to co...

JavaScript to add and delete messages on the message board

This article shares a small example of adding and...

Detailed explanation of CSS sticky positioning position: sticky problem pit

Preface: position:sticky is a new attribute of CS...

Embed player in web page embed element autostart false invalid

Recently, I encountered the need to embed a player...

Solve the MySQL 5.7.9 version sql_mode=only_full_group_by problem

MySQL 5.7.9 version sql_mode=only_full_group_by i...