开发者

Centering DIV in javascript

开发者 https://www.devze.com 2023-02-10 15:19 出处:网络
Guys i\'m having a problem with the javascript in my website. I cant center the div to properly show the image i click. It only works at the second try, the first try of any image it allways show t开发

Guys i'm having a problem with the javascript in my website. I cant center the div to properly show the image i click. It only works at the second try, the first try of any image it allways show t开发者_运维技巧he div to the bottom right.

I've tried messing around with the javascript but can't get my head around it.

Check it out here


It happens because you apply the CSS properties before setting the image src.

When you first load an image, there is no previous image and it does nothing to center. When you click another picture, it centers the image according to the previous image's dimensions.


It happens becouse your div doesn't have any offsetWidth and height yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消