How can I force WebKit to redraw/repaint to propagate style changes?

后端 未结 27 2872
我寻月下人不归
我寻月下人不归 2020-11-22 02:04

I have some trivial JavaScript to effect a style change:

sel = document.getElementById(\'my_id\');
sel.className = sel.className.replace(/item-[1-9]-selected         


        
27条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 02:28

    the "display/offsetHeight" hack didn't work in my case, at least when it was applied to the element being animated.

    i had a dropdown menu that was being open/closed over the page content. the artifacts were being left on the page content after the menu had closed (only in webkit browsers). the only way the "display/offsetHeight" hack worked is if i applied it to the body, which seems nasty.

    however, i did find another solution:

    1. before the element starts animating, add a class that defines "-webkit-backface-visibility: hidden;" on the element (you could also use inline style, i'd guess)
    2. when it's done animating, remove the class (or style)

    this is still pretty hacky (it uses a CSS3 property to force hardware rendering), but at least it only affects the element in question, and worked for me on both safari and chrome on PC and Mac.

提交回复
热议问题