Can I use javascript to force the browser to “flush” any pending layout changes?

前端 未结 4 1467
庸人自扰
庸人自扰 2020-12-11 04:01

I have a library that build UI using Javascript, and because of the dynamic content involved I sometimes want to put content out to the browser, examine how the layout was c

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 04:34

    Yes, you can!!

    Most browsers optimize the reflow process by queuing changes and performing them in batches. Flushing the render tree changes requires you to retrieve some layout information ( offset calculations, getComputedStyle(), and scroll values ).

    var el = document.getElementById("my-element");
    var top = el.offsetTop;
    

    The above code will force the browser to execute changes in rendering queue in order to return the correct values.

    Simple!!

提交回复
热议问题