Is there a way to apply multiple CSS styles in a batch to avoid multiple reflows?

前端 未结 5 948
别跟我提以往
别跟我提以往 2020-12-30 05:56

I know that altering element\'s style via JavaScript directly will cause a reflow. However, I was wondering if it is possible to alter multiple style values in a batch with

5条回答
  •  忘掉有多难
    2020-12-30 06:42

    You could put all the styles in a CSS class

    .foo { background:#000; color:#fff; ... }
    

    and then assign it to the className property

    // javascript
    var your_node = document.getElementById('node_id');
    your_node.className = 'foo'
    

    That should trigger only one repaint/reflow

提交回复
热议问题