Force DOM redraw/refresh on Chrome/Mac

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

    An approach that worked for me on IE (I couldn't use the display technique because there was an input that must not loose focus)

    It works if you have 0 margin (changing the padding works as well)

    if(div.style.marginLeft == '0px'){
        div.style.marginLeft = '';
        div.style.marginRight = '0px';
    } else {
        div.style.marginLeft = '0px';
        div.style.marginRight = '';
    }
    
    0 讨论(0)
  • 2020-11-22 02:47

    It helped me

    domNodeToRerender.style.opacity = 0.99;
    setTimeout(() => { domNodeToRerender.style.opacity = '' }, 0);
    
    0 讨论(0)
  • 2020-11-22 02:50

    Hiding an element and then showing it again within a setTimeout of 0 will force a redraw.

    $('#page').hide();
    setTimeout(function() {
        $('#page').show();
    }, 0);
    
    0 讨论(0)
  • 2020-11-22 02:52

    I wanted to return all the states to the previous state (without reloading) including the elements added by jquery. The above implementation not gonna works. and I did as follows.

    // Set initial HTML description
    var defaultHTML;
    function DefaultSave() {
      defaultHTML = document.body.innerHTML;
    }
    // Restore HTML description to initial state
    function HTMLRestore() {
      document.body.innerHTML = defaultHTML;
    }
    
    
    
    DefaultSave()
    <input type="button" value="Restore" onclick="HTMLRestore()">
    
    0 讨论(0)
  • 2020-11-22 02:55

    None of the above answers worked for me. I did notice that resizing my window did cause a redraw. So this did it for me:

    $(window).trigger('resize');
    
    0 讨论(0)
  • 2020-11-22 02:57

    Below css works for me on IE 11 and Edge, no JS needed. scaleY(1) does the trick here. Seems the simplest solution.

    .box {
        max-height: 360px;
        transition: all 0.3s ease-out;
        transform: scaleY(1);
    }
    .box.collapse {
        max-height: 0;
    }
    
    0 讨论(0)
提交回复
热议问题