I am developing a web that I have a horizontal menu with a position : fixed
, with a JS function that detects the scroll I change the color of the background to the menu so that it can be seen in a readable way, since by default the background is transparent but in some pages I need to start with a white background that I do with the same function that evaluates the scroll but my problem is that it takes a long time to execute the function as I put it in <body onload="calculaAlto()">
and the page takes about 2 seconds in loading, then the background color changes after 2 seconds which looks very bad. Does anyone know of any html or js event that helps me to run that function asynchronously to the rest of the DOM? . THANKS FROM ANTE HAND here is a part of the code.
function calculaAlto(bloque){
var box = document.getElementById(bloque);
var altoBox = box.scrollHeight;
var menu = document.getElementById('menuh');
var itemsMen = document.getElementsByClassName('titulosItemsMenu');
var logo = document.getElementById('logoimg');
var subMenu = document.getElementsByClassName('subMenu-li');
if(document.documentElement.scrollTop>=altoBox || document.body.scrollTop >= altoBox){
menu.style.backgroundColor ="white";
logo.src ="img/logo-praco-blue.png";
subMenu[0].style.backgroundColor ="white";
subMenu[1].style.backgroundColor ="white";
menu.style.boxShadow = " 0px 2px 1px 0px rgba(0,0,0,0.5)";
itemsMen[0].style.color ="#0E4B8E";
itemsMen[1].style.color ="#0E4B8E";
itemsMen[2].style.color ="#0E4B8E";
itemsMen[3].style.color ="#0E4B8E";
itemsMen[4].style.color ="#0E4B8E";
itemsMen[5].style.color ="#0E4B8E";
itemsMen[6].style.color ="#0E4B8E";
itemsMen[7].style.color ="#0E4B8E";
}else{
menu.style.backgroundColor ="transparent";
logo.src ="img/logo-praco-monochrome-white.png";
subMenu[0].style.backgroundColor ="transparent";
subMenu[1].style.backgroundColor ="transparent";
menu.style.boxShadow = " 0px 0px 0px 0px rgba(0,0,0,0.5)";
itemsMen[0].style.color ="white";
itemsMen[1].style.color ="white";
itemsMen[3].style.color ="white";
itemsMen[4].style.color ="white";
itemsMen[5].style.color ="white";
itemsMen[6].style.color ="white";
itemsMen[7].style.color ="white";
}
}
with that function I change the background color to the menu and some other things
<html>
<body onload="calculaAlto(n)">
<div id="menu"> contenido del menu(con position fixed) </div>
<div id="n"> mide 0px </div>
<div > mide 100px </div>
<div > mide 100px </div>
</body>
</html>
It's just an example of HTML since the real is much bigger but I hope you can understand my problem.
by default in this case the menu should start with a white background and if it does, but it takes a long time