css - Native JavaScript style syntax -


i'm working on native javascript (not forte.. @ all) , not seeing result of function. i'm sure i've got syntax error in here somewhere. can me identify it? fyi- function dynamically center object on page.

this.style[left]= ((windowwidth - this.style[width])/2); this.style[top]= ((windowheight - this.style[height])/2); 

you have, @ least, 3 problems.

first: css height, width, left , top properties take lengths. passing them numbers.

you must include unit.

likewise, need account unit on values width , height.

second: need balance parentheses.

third: when using square bracket notation, need pass in strings. @ moment, assume left , top undefined.

this.style.left = (windowwidth  - parseint(this.style.width,10)) / 2  + 'px'; this.style.top  = (windowheight - parseint(this.style.height,10)) / 2 + 'px'; 

finally, work if element has width , height defined using inline style (or if properties have been set via javascript). otherwise values trying read undefined. in case need deal computed style.

also remember top , left have no effect unless element positioned.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -