简体   繁体   English

div长宽比(按父级宽度)

[英]div aspect ratio by parent width

I have an image which is 723 in width and 425 in height and I need the div.col-75-liquid element width to have that aspect ratio of the width and height of my images so when the browser is resized my slider will keep it aspect ratio I have to do this in jquery not in css. 我有一个图像,宽度为723,高度为425,我需要div.col-75-liquid元素的宽度,以具有图像的宽度和高度的纵横比,因此在调整浏览器大小时,滑块将保持该宽度长宽比我必须在jQuery中而不是在CSS中做到这一点。 How do I do this in jquery? 如何在jquery中做到这一点? can someone make me an example of a div element that has that aspect ratio, the col-75-liquid is elastic so it will grow and shrink 谁能给我一个具有该长宽比的div元素的示例,col-75-liquid是有弹性的,因此它会增长和收缩

When I have the aspect ratio I can then recreate my control on browser resize and set the new width and height for my control 当我拥有宽高比时,我可以在调整浏览器大小时重新创建控件,并为控件设置新的宽度和高度

<div class="col-75-liquid">
    <div class="img">
    </div>
</div>

Well, without your CSS and whatnot , this will resize the parent div to 150% of the image's dimensions when the window is resized. 好吧,如果没有CSS和诸如此类的东西 ,则在调整窗口大小时,这会将父div的大小调整为图像尺寸的150%。 This will maintain aspect, because you are comparing the parent to the image itself. 这将保持外观,因为您正在将父对象与图像本身进行比较。 Let me know if you'd like me to expand further. 让我知道您是否希望我进一步扩展。

HTML: HTML:

<div id="wrapper">
    <img id="slide1" src="whatever.jpg" />
</div>

jQuery: jQuery的:

$(window).on('resize', function() {
    $('#wrapper').height($('#slide1').height() * 1.5);
    $('#wrapper').width($('#slide1').width() * 1.5);
});

http://jsfiddle.net/SinisterSystems/fJpwf/2/ http://jsfiddle.net/SinisterSystems/fJpwf/2/

I didn't do anything responsive, but it would act responsively if your other CSS (or even other jQuery functions) are controlling that. 我没有做任何响应式的操作,但是如果您的其他CSS (甚至其他jQuery函数)在控制该操作,它就会做出响应。 I can throw another example if you'd like. 如果您愿意,我可以举另一个例子。

EDIT: 编辑:

Didn't understand the question. 不明白这个问题。

Try something like this: 尝试这样的事情:

http://jsfiddle.net/SinisterSystems/fJpwf/3/ http://jsfiddle.net/SinisterSystems/fJpwf/3/

<div class="wrapper">
    <img class="slide1" src="image.jpg" />
</div>

$(window).on('resize', function() {
    $('.wrapper').height($('.wrapper').width() * .58);    
});

This will make the wrapper maintain the aspect ratio of your image despite what size it is. 无论大小如何,包装器都可以保持图像的高宽比。

In other words, if you know the width of your parent element, do something like: 换句话说,如果您知道父元素的width ,请执行以下操作:

$('.parent').height($('.parent').width() * .58);  

If you know the height and need the width, use: 如果您知道高度并需要宽度,请使用:

$('.parent').width($('.parent').height() * 1.7); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM