Scale different width images to fit a row and maintain equal height

前端 未结 5 862
一向
一向 2021-02-03 10:09

I have three images which are all different widths, but each the same height.

I want these images to be in a responsive row so that the

5条回答
  •  粉色の甜心
    2021-02-03 10:33

    You might want to use this a basis for refining a solution:

    https://jsfiddle.net/kb4gg35f/

    For some reason this does not work as a snippet. It works as a fiddle, though.

    var images = $('img');
    var accumulatedWidth = 0;
    var widthResizeFactor;
    var screenWidth = $('div').width();
    var originalSizeArray = [];
    $(document).ready(function() {
      for (var i = 0; i < images.length; i++) {
        var theImage = new Image();
        theImage.src = images[i].src;
        accumulatedWidth += theImage.width;
        originalSizeArray.push(theImage.width);
      }
      widthResizeFactor = screenWidth / accumulatedWidth;
      for (var i = 0; i < images.length; i++) {
        images[i].width = originalSizeArray[i] * widthResizeFactor;
      }
    });
    body {
      margin: 0;
    }
    div:after {
      content: "";
      display: table;
      clear: left;
    }
    img {
      float: left;
    }
    
    

提交回复
热议问题