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

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

    Since everyone seems to have their own problems and solutions, I figured I'd add something that works for me. On Android 4.1 with current Chrome, trying to drag a canvas around inside a div with overflow:hidden, I couldn't get a redraw unless I added an element to the parent div (where it wouldn't do any harm).

    var parelt = document.getElementById("parentid");
    var remElt = document.getElementById("removeMe");
    var addElt = document.createElement("div");
    addElt.innerHTML = " "; // Won't work if empty
    addElt.id="removeMe";
    if (remElt) {
        parelt.replaceChild(addElt, remElt);
    } else {
        parelt.appendChild(addElt);
    }
    

    No screen flicker or real update, and cleaning up after myself. No global or class scoped variables, just locals. Doesn't seem to hurt anything on Mobile Safari/iPad or desktop browsers.

提交回复
热议问题