For what it's worth, I had the same question, but he wanted to have the height of the element, even if it is not set in style, and even if the element has a display: none. Therefore, my solution is to steal an element, make it invisible, place it somewhere invisible, make it display: block, measure its height with clientHeight, return it to its original location with the original settings, and then perform a sliding one. Here he is:
function slideElementIn(myid) { mydiv= document.getElementById(myid); if(mydiv.style.display!="none")return; // clientHeight is zero when display is none. So, prepare: inipos = mydiv.style.position; inivisibility = mydiv.style.visibility; iniz = mydiv.style.zIndex; initop = mydiv.style.top; inileft = mydiv.style.left; inioverflow = mydiv.style.overflow; mydiv.style.zIndex = "-999"; mydiv.style.top = "0"; mydiv.style.left = "0"; mydiv.style.visibility = "hidden"; mydiv.style.position = "fixed"; mydiv.style.display = "block"; finHeight = mydiv.clientHeight; // got it! // repair damage: mydiv.style.display = "none"; mydiv.style.position = inipos; mydiv.style.visibility = inivisibility; mydiv.style.zIndex = iniz; mydiv.style.top = initop; mydiv.style.left = inileft; iniHeight = mydiv.style.height; mydiv.style.height = 0; mydiv.style.display = "block"; mydiv.style.overflow="hidden"; var i = 1; // set your counter to 1 function myLoop2 (i,mydiv,finHeight,iniHeight,inioverflow) { // create a loop function setTimeout(function () { // call a 3s setTimeout when the loop is called mydiv.style.height = finHeight * easeIn(i * (1./fadeDuration)); i++; // increment the counter if (i < fadeDuration) { // if the counter < 10, call the loop function myLoop2(i,mydiv,finHeight,iniHeight,inioverflow); // .. again which will trigger another } else { mydiv.style.height = iniHeight; mydiv.style.overflow=inioverflow; } // .. setTimeout() }, 1) } myLoop2(i,mydiv,finHeight,iniHeight,inioverflow); } function slideElementOut(myid) { mydiv= document.getElementById(myid); itsdisplay = mydiv.style.display ; if ( itsdisplay != "none" ) { finHeight = mydiv.clientHeight; iniHeight = mydiv.style.height; inioverflow = mydiv.style.overflow; mydiv.style.overflow = "hidden"; var i = fadeDuration; // set your counter to 1 function myLoop (i,mydiv,finHeight,iniHeight,inioverflow) { // create a loop function setTimeout(function () { // call a 3s setTimeout when the loop is called mydiv.style.height = finHeight * easeOut(i * (1./fadeDuration)); i--; // increment the counter if (i > 0) { // if the counter < 10, call the loop function myLoop(i,mydiv,finHeight,iniHeight,inioverflow); // .. again which will trigger another } else { mydiv.style.display = "none"; mydiv.style.height = iniHeight; mydiv.style.overflow=inioverflow; } // .. setTimeout() }, 1) } myLoop(i,mydiv,finHeight,iniHeight,inioverflow); } }
I suppose it's worth publishing it here anyway, because when I was looking for my answer, I got to this question, but I only need a few steps. I hope this helps someone.