I have the following DOM element changing function using plain JavaScript, in which I use the page load event to change a span element containing some text string of the DOM.
With the following code, the DOM elements will change as expected. However, variables desktop
and mobile
will still see the minimal portion of the flickering before their DOM elements change.
P粉1954022922024-03-27 17:50:03
There are many reasons why it might flicker, but there are two precautions you can take:
defer
on script tags <script defer>
as this allows the browser to handle the order in which scripts are run instead of using DOMContentLoaded
. You can also avoid changes
wrapper functions. P粉4781887862024-03-27 14:35:48
In any case, you need to wait until the DOM is loaded before you can operate on it. Using an even listener with DOMContentLoaded
would be the way to go. So, three things need to happen:
visibility:hidden
or display:none
. The difference is that