I wrote the following script to display a hidden element, and then fixed it in the center in the center of the page.
function popUp(id,type) { var popUpBox = document.getElementById(id); popUpBox.style.position = "fixed"; popUpBox.style.display = "block"; popUpBox.style.zIndex = "6"; popUpBox.style.top = "50%"; popUpBox.style.left = "50%"; var height = popUpBox.offsetHeight; var width = popUpBox.offsetWidth; var marginTop = (height / 2) * -1; var marginLeft = (width / 2) * -1; popUpBox.style.marginTop = marginTop + "px"; popUpBox.style.marginLeft = marginLeft + "px"; }
When this function is called by the onclick event, offsetHeight and offsetWidth are not calculated correctly, therefore, they do not center the element correctly. If I clicked the onclick element a second time, the offset Height and offsetWidth will be correctly calculated.
I tried to change the order in every way, I can imagine, and it drives me crazy! Any help is much appreciated!
source share