开发者

Calculate proportional width of object (proportion: 1600x1080)

开发者 https://www.devze.com 2022-12-23 22:44 出处:网络
This is a simple jquery question: when my div #wrapper has a specific height (=browser height - 129px), i want to set the width of a inner div #object automatically too (cause I need a width for th开发

This is a simple jquery question: when my div #wrapper has a specific height (=browser height - 129px), i want to set the width of a inner div #object automatically too (cause I need a width for th开发者_Python百科e object to read it out)

example of the html:

<div id="wrapper">

 <div id="object"> </div>

</div>

example (jquery):

$("#wrapper").css({height:(($(window).height())-129)+'px'});
$("#object").css({width: [ CALCULATION ], height: ($("#wrapper").height())+'px'})

the original proportion of the image is: 1600x1080

here's the link to the attachment, take a look at it (tinypic): http://www.imgplace.com/...age1.png

additonal information:

the heights 500px, 600px and 700px you can see in the attachment are just examples, the heigth could also be 711px, 623px, 998px etc.

My math skills aren't really good, would be great if someone could help me out

Kind Regards,

Hans :-)


I think the calculation you want is Math.round($("#wrapper").height() * 1600 / 1080).


You can use directly the jQuery.width and jQuery.height functions instead of the jQuery.css function. Also, you could define the div#object's height to 100% so you don't have to specifically set it...

$("#wrapper").height($(window).height()-129);
$("#object").width(Math.floor($("#wrapper").height() * 1600 / 1080))
            .height($("#wrapper").height());
0

精彩评论

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