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

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

    I tried morewry answer but it does not work for me. I had trouble to have the same clientWidth with safari comparing to others browsers and this code solved the problem:

    var get_safe_value = function(elm,callback){
        var sty = elm.style
        sty.transform = "translateZ(1px)";
        var ret = callback(elm)//you can get here the value you want
        sty.transform = "";
        return ret
    }
    // for safari to have the good clientWidth
    var $fBody = document.body //the element you need to fix
    var clientW = get_safe_value($fBody,function(elm){return $fBody.clientWidth})
    

    It is really strange because if I try again to get the clientWidth after get_safe_value, I obtain a bad value with safari, the getter has to be between sty.transform = "translateZ(1px)"; and sty.transform = "";

    The other solution that works definitively is

    var $fBody = document.body //the element you need to fix
    $fBody.style.display = 'none';
    var temp = $.body.offsetHeight;
    $fBody.style.display = ""
    temp = $.body.offsetHeight;
    
    var clientW = $fBody.clientWidth
    

    The problem is that you lose focus and scroll states.

提交回复
热议问题