Force DOM redraw/refresh on Chrome/Mac

前端 未结 24 1935
轻奢々
轻奢々 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:04

    I ran into a similar issue and this simple line of JS helped to fix it:

    document.getElementsByTagName('body')[0].focus();
    

    In my case it was a bug with a Chrome extension not redrawing the page after changing its CSS from within the extension.

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

    Sample Html:

    <section id="parent">
      <article class="child"></article>
      <article class="child"></article>
    </section>
    

    Js:

      jQuery.fn.redraw = function() {
            return this.hide(0,function() {$(this).show(100);});
            // hide immediately and show with 100ms duration
    
        };
    

    call function:

    $('article.child').redraw(); //<==bad idea

    $('#parent').redraw();
    
    0 讨论(0)
  • 2020-11-22 03:06
    function resizeWindow(){
        var evt = document.createEvent('UIEvents');
        evt.initUIEvent('resize', true, false,window,0);
        window.dispatchEvent(evt); 
    }
    

    call this function after 500 milliseconds.

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

    This works for me. Kudos go here.

    jQuery.fn.redraw = function() {
        return this.hide(0, function() {
            $(this).show();
        });
    };
    
    $(el).redraw();
    
    0 讨论(0)
  • 2020-11-22 03:10

    My fix for IE10 + IE11. Basically what happens is that you add a DIV within an wrapping-element that has to be recalculated. Then just remove it and voila; works like a charm :)

        _initForceBrowserRepaint: function() {
            $('#wrapper').append('<div style="width=100%" id="dummydiv"></div>');
            $('#dummydiv').width(function() { return $(this).width() - 1; }).width(function() { return $(this).width() + 1; });
            $('#dummydiv').remove();
        },
    
    0 讨论(0)
  • 2020-11-22 03:10

    October 2020, the problem still persists with Chrome 85.

    morewry's solution of using transform:translateZ(0) works, actually, any transformation works, including translate(0,0) and scale(1), but if you must update the element again, then the trick is to toggle the transformation, and the best way is to directly remove it, after one frame, using requestAnimationFrame (setTimeout should always be avoided because it will be slower so it can cause glitches).

    So, the update one element:

        function refresh_element(node) {
            // you can use scale(1) or translate(0, 0), etc
            node.style.setProperty('transform', 'translateZ(0)');
            // this will remove the property 1 frame later
            requestAnimationFrame(() => {
                node.style.removeProperty('transform');
            });
        }
    
    0 讨论(0)
提交回复
热议问题