Force DOM redraw/refresh on Chrome/Mac

前端 未结 24 1946
轻奢々
轻奢々 2020-11-22 02:11

Every once in a while, Chrome will render perfectly valid HTML/CSS incorrectly or not at all. Digging in through the DOM inspector is often enough to get it to realize the

24条回答
  •  灰色年华
    2020-11-22 03:10

    October 2020, the problem still persists with Chrome 85.

    morewry's solution of using transform:translateZ(0) works, actually, any transformation works, including translate(0,0) and scale(1), but if you must update the element again, then the trick is to toggle the transformation, and the best way is to directly remove it, after one frame, using requestAnimationFrame (setTimeout should always be avoided because it will be slower so it can cause glitches).

    So, the update one element:

        function refresh_element(node) {
            // you can use scale(1) or translate(0, 0), etc
            node.style.setProperty('transform', 'translateZ(0)');
            // this will remove the property 1 frame later
            requestAnimationFrame(() => {
                node.style.removeProperty('transform');
            });
        }
    

提交回复
热议问题