Force DOM redraw/refresh on Chrome/Mac

前端 未结 24 2203
轻奢々
轻奢々 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条回答
  •  不要未来只要你来
    2020-11-22 02:59

    If you want to preserve the styles declared into your stylesheets, better to use something like:

    jQuery.fn.redraw = function() {
        this.css('display', 'none'); 
        var temp = this[0].offsetHeight;
        this.css('display', '');
        temp = this[0].offsetHeight;
    };
    
    $('.layer-to-repaint').redraw();
    

    NB: with latest webkit/blink, storing the value of offsetHeight is mandatory in order to trigger the repaint, otherwise the VM (for optimizations purposes) will probably skip that instruction.

    Update: added the second offsetHeight reading, it is necessary to prevent browser from queueing/caching a following CSS property/class change with the restore of the display value (this way a CSS transition that can follow should be rendered)

提交回复
热议问题