javascript - Function doesn't correctly perform unless it is called twice -
some background: using javascript vertically center child divs vary in height depending on conditions in parent div. centering function follows:
function autocenter() {var firstline = document.getelementbyid("ntext"); var secondline = document.getelementbyid("dtext"); var l1h = parseint(getcomputedstyle(firstline)["height"]); var l2h = parseint(getcomputedstyle(secondline)["height"]); var d1h = parseint(getcomputedstyle(document.getelementbyid("design"))["height"]); var d2h = parseint(getcomputedstyle(document.getelementbyid("design2"))["height"]); var totalh = l1h + l2h + d1h+ d2h; document.getelementbyid("desdiv").style.top = (212-totalh)/2 + "px"; firstline.style.top = (212-totalh)/2 + d1h +"px"; secondline.style.top = (212-totalh)/2 + l1h + d1h + "px"; document.getelementbyid("desdiv2").style.top = (212-totalh)/2 + l1h + l2h + d1h+ "px";}
ntext, dtext, desdiv, , desdiv2 divs. first 2 change based on size of text in them , second 2 change based on size of image in them (ids design & design 2)
this function works when things changing text divs. run problems when change image divs. appears first part of function involves getcomputedstyle doesn't new height value image until function called second time. after it's called second time centers should. ideally should work on first call. doing wrong here , how can divs center on first call?
edit: i'm not sure if relevant autocenter() called within function.
it's because images aren't loaded @ time call center function.
add event listener images center function fires when images loaded.
as semicolons. put them in, always. semicolon insertion.
Comments
Post a Comment