【问题标题】:Select, then resize images that have a certain ratio with jQuery选择,然后用 jQuery 调整具有一定比例的图像
【发布时间】:2016-02-10 05:40:38
【问题描述】:

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

<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 不是很熟悉,因此非常感谢您提供经过深思熟虑和详细的回答。

【问题讨论】:

  • 我们可以看看你的博客或者至少是 jsfiddle 上的一个 html 示例吗!?

标签: javascript jquery html css image


【解决方案1】:

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

更新的 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
        }
    }
});

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2021-10-21
    • 2012-02-28
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多