简体   繁体   English

调整所有图像的大小,同时保持每个图像的纵横比

[英]resize all images while maintaining the aspect ratio of each

I'm fairly new to this stuff and I've got a small problem — what I'm trying to do is resize all of the images on a web page (more specifically, the images within each element with the id #post) and scale them down a bit 我对这些东西很新,我遇到了一个小问题 - 我正在尝试做的是调整网页上的所有图像(更具体地说,每个元素中带有id #post的图像)和把它们缩小一点

var currentWidth = $("#post img").width();
$("#post img").each(function () {
    $(this).css({'width': currentWidth / 1.5 + "px"});
});

The problem is that I think it's grabbing the width of the first image and using that for all of them, so they all end up being the same size as the first scaled down image. 问题是我认为它抓住了第一个图像的宽度并将其用于所有图像,因此它们最终都与第一个缩小图像的尺寸相同。 Any way to fix this so that each image's width grabbed and properly scaled down? 有没有办法解决这个问题,以便每个图像的宽度都被抓住并适当缩小?

You're correct - you're getting a single width and using it throughout the loop. 你是对的 - 你得到一个宽度并在整个循环中使用它。 You're already halfway there, using this within the loop. 你已经在那里,在循环中使用this Just follow that through: 请遵循以下内容:

$("#post img").each(function () {
    var currentWidth = $(this).width();

    if (currentWidth > 900) {
      $(this).css({'width': currentWidth / 1.5 + "px"});
    }
});

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

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