简体   繁体   English

根据jQuery中的维度调整每个图像的大小

[英]Resizing each image based on dimensions in jQuery

I've got a basic concept of how to achieve this result, however i can't seem to actually obtain the results i want. 我有一个如何实现这个结果的基本概念,但是我似乎无法真正获得我想要的结果。 I'm creating fixed containers of 100px (height) x 75px (width) in which images are stored. 我正在创建100px(高度)x 75px(宽度)的固定容器,其中存储图像。 I'm then attempting to use jQuery to run a check on whether or not the images height is bigger than it's width, visa versa or they are equal. 然后我尝试使用jQuery检查图像高度是否大于宽度,反之亦然或者它们是相等的。 If they are equal or the width is bigger, i set those to 100% each and set height as auto, whereas if the height is the greater value, i set that to 100% and width to auto. 如果它们相等或宽度更大,我将它们设置为100%并将高度设置为auto,而如果高度是更大的值,我将其设置为100%,宽度设置为auto。 Below is the current code I have which is resizing the images, but not to how i would like them too. 下面是我目前的代码,它正在调整图像大小,但不是我想要的方式。

HTML HTML

<div id="imagesContainer">
    <div style="height: 100px; width: 75px; border: 1px solid RGB(0, 0, 0);">
        <div class="thumbImage">
            <img src="INSERT 100 x 500 IMAGE HERE"></img>
        </div>
    </div>

    <div style="height: 100px; width: 75px; border: 1px solid RGB(0, 0, 0);">
        <div class="thumbImage">
            <img src="INSERT 250 x 280 IMAGE HERE"></img>
        </div>
    </div>

    <div style="height: 100px; width: 75px; border: 1px solid RGB(0, 0, 0);">
        <div class="thumbImage">
            <img src="INSERT 100 x 100 IMAGE HERE"></img>
        </div>
    </div>

    <div style="height: 100px; width: 75px; border: 1px solid RGB(0, 0, 0);">
        <div class="thumbImage">
            <img src="INSERT 1800x900 IMAGE HERE"></img>
        </div>
    </div>
</div>

jQuery jQuery的

$(".thumbImage").children("img").each(function(){
    if ($(this).height() > $(this).width()) {
        $(this).css({
            height: '100%',
            width: 'auto'
        });
    } else if ($(this).height() < $(this).width() || ($(this).height() == $(this).width()) {
        $(this).css({
            height: 'auto',
            width: '100%'
        });
    }
});

The problem is that you are testing on the difference between the picture's height and width... It would work flawlessly if the container was square, but since it is a rectangle, it is wrong. 问题是你正在测试图片的高度和宽度之间的差异......如果容器是方形的,它会完美地工作,但由于它是一个矩形,它是错误的。

Here: 这里:

$(".thumbImage").children("img").each(function(){
    imgRatio = $(this).height()/$(this).width();
    containerRatio = $(this).parent().height()/$(this).parent().width();
    if (imgRatio > containerRatio) {
        $(this).css({
            height: '100%',
            width: 'auto'
        });
    } else {
        $(this).css({
            height: 'auto',
            width: '100%'
        });
    }
});

Not sure if the code is 100% correct, did not test. 不确定代码是否100%正确,没有测试。 But basically we test on the RATIO of the height/width and compare it with the container's ratio to be able to decide what resize we do. 但基本上我们测试高度/宽度的RATIO并将其与容器的比率进行比较,以便能够决定我们的调整大小。

You have a syntax error in your script, and '.thumbImage' must be set to a height of 100%: 您的脚本中存在语法错误,并且“.thumbImage”必须设置为100%的高度:

Syntax error: 语法错误:

if( $(this).height() < $(this).width() || ($(this).height() == $(this).width() ) )

(You missed a closing bracket) (你错过了一个结束的支架)

CSS: CSS:

.thumbImage {
    height:100%;
} 

JS Fiddle: http://jsfiddle.net/fMhr2/1/ JS小提琴: http//jsfiddle.net/fMhr2/1/

Also, you should calculate the ratio to get it to work properly, like Salketer just mentioned. 此外,您应该计算比率以使其正常工作,例如Salketer刚才提到的。 I added that to this JSFiddle 我把它添加到这个JSFiddle

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

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