When embedding jquery, it is crucial to follow the correct approach:
$(document).ready(function(){
...
}
By using this method, jQuery will only be executed once the entire page has finished loading, ensuring optimal performance.
To prevent flickering, you can also incorporate additional JavaScript code outside of the function:
// Adding a class to indicate that JS is enabled
$('html').addClass('js');
Next, identify the closest CSS class to elements that should not flicker and apply custom CSS rules:
/**
* Use this CSS rule to hide elements on page load when JS is enabled,
* allowing JS to control visibility and prevent flickering.
*/
html.js .myClass {
display: none;
}
Finally, manage the visibility of ".myClass" using JavaScript to display it according to our defined CSS rule.
Following these steps will effectively eliminate flickering by instantly hiding the specified elements with display: none
.