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

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

    I stumbled upon this today: Element.redraw() for prototype.js

    Using:

    Element.addMethods({
      redraw: function(element){
        element = $(element);
        var n = document.createTextNode(' ');
        element.appendChild(n);
        (function(){n.parentNode.removeChild(n)}).defer();
        return element;
      }
    });
    

    However, I've noticed sometimes that you must call redraw() on the problematic element directly. Sometimes redrawing the parent element won't solve the problem the child is experiencing.

    Good article about the way browsers render elements: Rendering: repaint, reflow/relayout, restyle

提交回复
热议问题