Force DOM redraw/refresh on Chrome/Mac

前端 未结 24 1901
轻奢々
轻奢々 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:14

    2020: Lighter and stronger

    The previous solutions don't work anymore for me.

    I guess browsers optimize the drawing process by detecting more and more "useless" changes.

    This solution makes the browser draw a clone to replace the original element. It works and is probably more sustainable:

    const element = document.querySelector('selector');
    if (element ) {
      const clone = element.cloneNode(true);
      element.replaceWith(clone);
    }
    

    tested on Chrome 80 / Edge 80 / Firefox 75

提交回复
热议问题