Force DOM redraw/refresh on Chrome/Mac

前端 未结 24 1804
轻奢々
轻奢々 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 03:11

    This seems to do the trick for me. Plus, it really doesn't show at all.

    $(el).css("opacity", .99);
    setTimeout(function(){
       $(el).css("opacity", 1);
    },20);
    
    0 讨论(0)
  • 2020-11-22 03:12

    This solution without timeouts! Real force redraw! For Android and iOS.

    var forceRedraw = function(element){
      var disp = element.style.display;
      element.style.display = 'none';
      var trick = element.offsetHeight;
      element.style.display = disp;
    };
    
    0 讨论(0)
  • 2020-11-22 03:12

    call window.getComputedStyle() should force a reflow

    0 讨论(0)
  • 2020-11-22 03:13

    This is my solution that worked for disappearing content...

    <script type = 'text/javascript'>
        var trash_div;
    
        setInterval(function()
        {
            if (document.body)
            {
                if (!trash_div)
                    trash_div = document.createElement('div');
    
                document.body.appendChild(trash_div);
                document.body.removeChild(trash_div);
            }
        }, 1000 / 25); //25 fps...
    </script>
    
    0 讨论(0)
  • 2020-11-22 03:14

    Not sure exactly what you're trying to achieve but this is a method I have used in the past with success to force the browser to redraw, maybe it will work for you.

    // in jquery
    $('#parentOfElementToBeRedrawn').hide().show(0);
    
    // in plain js
    document.getElementById('parentOfElementToBeRedrawn').style.display = 'none';
    document.getElementById('parentOfElementToBeRedrawn').style.display = 'block';
    

    If this simple redraw doesn't work you can try this one. It inserts an empty text node into the element which guarantees a redraw.

    var forceRedraw = function(element){
    
        if (!element) { return; }
    
        var n = document.createTextNode(' ');
        var disp = element.style.display;  // don't worry about previous display style
    
        element.appendChild(n);
        element.style.display = 'none';
    
        setTimeout(function(){
            element.style.display = disp;
            n.parentNode.removeChild(n);
        },20); // you can play with this timeout to make it as short as possible
    }
    

    EDIT: In response to Šime Vidas what we are achieving here would be a forced reflow. You can find out more from the master himself http://paulirish.com/2011/dom-html5-css3-performance/

    0 讨论(0)
  • 2020-11-22 03:14

    2020: Lighter and stronger

    The previous solutions don't work anymore for me.

    I guess browsers optimize the drawing process by detecting more and more "useless" changes.

    This solution makes the browser draw a clone to replace the original element. It works and is probably more sustainable:

    const element = document.querySelector('selector');
    if (element ) {
      const clone = element.cloneNode(true);
      element.replaceWith(clone);
    }
    

    tested on Chrome 80 / Edge 80 / Firefox 75

    0 讨论(0)
提交回复
热议问题