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

后端 未结 27 2899
我寻月下人不归
我寻月下人不归 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:47

    Not that this question needs another answer, but I found simply changing the color by a single bit forced a repaint in my particular situation.

    //Assuming black is the starting color, we tweak it by a single bit
    elem.style.color = '#000001';
    
    //Change back to black
    setTimeout(function() {
        elem.style.color = '#000000';
    }, 0);
    

    The setTimeout proved critical to move the second style change outside the current event loop.

提交回复
热议问题