选择,然后使用jQuery调整具有一定比例的图像大小

Select, then resize images that have a certain ratio with jQuery

本文关键字:图像 具有一 然后 调整 jQuery 选择      更新时间:2023-09-26

在我的Tumblr博客上,我正在尝试使用jQuery来选择具有特定比例的照片条目(imgs),并以某些方式调整它们的大小。
具体来说:如果图片的宽度大于 250 像素,请调整其大小,使高度为 250 像素,宽度为"自动"。 如果图像的高度大于 250px,请调整其大小,使宽度为 250px,高度为"自动"。 最后,如果图像是完美的正方形,请将其大小调整为 250px x 250px。这是我目前正在使用的。 如果它的编码很奇怪,请原谅我,老实说,我只是摆弄它,直到我得到一些想要的结果......

<script>
$(document).ready(function() {
    $('.photo_post img').each(function() {
        var maxWidth = 250; // Max width for the image
        var maxHeight = 250;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
    // Check if the current width is larger than the max
    if(height > maxHeight){
        ratio = maxWidth / width;   // get ratio for scaling image
        $(this).css("width", maxWidth); // Set new width
        $(this).css("height", height * ratio);  // Scale height based on ratio
        height = height * ratio;    // Reset height to match scaled image
        width = width * ratio;    // Reset width to match scaled image
    }
    // Check if current height is larger than max
    if(height < maxHeight){
        ratio = maxHeight / height; // get ratio for scaling image
        $(this).css("height", maxHeight);   // Set new height
        $(this).css("width", width * ratio);    // Scale width based on ratio
        width = width * ratio;    // Reset width to match scaled image
        height = height * ratio;    // Reset height to match scaled image
    }
});
});
</script>

我的问题是它无法在所有照片上正常工作。
我对jQuery不是很熟悉,所以深思熟虑和详细的答案是值得赞赏的。

您需要检查图像是纵向还是横向,然后根据此调整大小。

更新的 JS:

$(document).ready(function() {
$('.photo_post img').each(function() {
    var maxWidth = 250; // Max width for the image
    var maxHeight = 250;    // Max height for the image
    var ratio = 0;  // Used for aspect ratio
    var width = $(this).width();    // Current image width
    var height = $(this).height();  // Current image height

    // Portrait
    if (height > width) {
        // Check if the current height is larger than the max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
            height = height * ratio;    // Reset height to match scaled image
        }
    }
    // Landscape
    else {
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }
    }
});

});