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

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

    I found some complicated suggestions and many simple ones that didn’t work, but a comment to one of them by Vasil Dinkov provided a simple solution to force a redraw/repaint that works just fine:

    sel.style.display='none';
    sel.offsetHeight; // no need to store this anywhere, the reference is enough
    sel.style.display='';
    

    I’ll let someone else comment if it works for styles other than “block”.

    Thanks, Vasil!

提交回复
热议问题