【问题标题】:How to resize the images to fit in fixed size div?如何调整图像大小以适合固定大小的 div?
【发布时间】:2021-07-16 20:33:37
【问题描述】:

我正在开发项目(社交媒体网络应用程序),用户有权上传各种媒体。我将举例说明问题,假设我有 500px300px 的 div,并且我正在获取各种尺寸的图像,它可以是纵向或横向,也可以是 50px50px 等小图像。我想将这样的图像放入固定大小的 div 中,而不会丢失其纵横比,也不会被裁剪或拉伸。我尝试使用 css 属性,但它们似乎都不适用于所有类型的图像,我正在努力寻找解决方案。

渲染图像的组件:

import React, { useRef } from "react";

export default function Example(props) {
  const myRef = useRef(null);
  const [dimensionofImg, setDimensionofImg] = React.useState(null);
  const [dimensionsofDiv, setDimensionsofDiv] = React.useState(null);
  const [imagedimensions, setImagedimensions] = React.useState(null);

  React.useEffect(() => {
    setDimensionsofDiv({
      width: myRef.current.offsetWidth,
      height: myRef.current.offsetHeight
    });
  }, []);

  React.useEffect(() => {
    let dimensionTocompare;
    if (
      dimensionofImg &&
      dimensionofImg.width < dimensionofImg &&
      dimensionofImg.height
    ) {
      dimensionTocompare = dimensionsofDiv.width;
      let changedWidth =
        (dimensionTocompare * dimensionofImg.width) / dimensionofImg.height;
      setImagedimensions({
        maxWidth: "",
        maxHeight: "100%",
        width: changedWidth,
        height: ""
      });
    } else if (
      dimensionofImg &&
      dimensionofImg.width > dimensionofImg &&
      dimensionofImg.height
    ) {
      dimensionTocompare = dimensionsofDiv.height;
      let changedHeight =
        (dimensionTocompare * dimensionofImg.height) / dimensionofImg.width;
      setImagedimensions({
        maxWidth: "100%",
        maxHeight: "",
        width: "",
        height: changedHeight
      });
    }
    else if (
      dimensionofImg && dimensionofImg.width == dimensionofImg && dimensionofImg.height
    ) {
      
      setImagedimensions({
        maxWidth: "100%",
        maxHeight: "100%",
        width: "",
        height: ""
      });
    }
  }, [dimensionofImg, dimensionsofDiv]);

  function renderContent() {
    return (
      <div>
        width of div: {dimensionsofDiv.width}
        <br />
        height of div: {dimensionsofDiv.height}
      </div>
    );
  }

  return (
    <div>
      {dimensionsofDiv && renderContent()}
      dimensions of image width{dimensionofImg && dimensionofImg.width}, height
      {dimensionofImg && dimensionofImg.height}
      <div
        style={{ height: 500, width: 300, border: "solid 1px red" }}
        ref={myRef}
      >
        <img
          style={{
            maxWidth: `${imagedimensions && imagedimensions.maxWidth}`,
            maxHeight: `${imagedimensions && imagedimensions.maxHeight}`,
            width: `${imagedimensions && imagedimensions.width}`,
            height: `${imagedimensions && imagedimensions.height}`
          }}
          onLoad={(e) => {
            setDimensionofImg({
              width: e.currentTarget.offsetWidth,
              height: e.currentTarget.offsetHeight
            });
          }}
          alt=""
          src={props.src}
        />
      </div>
    </div>
  );
}

import "./styles.css";
import Example from "./component"
const src = "https://picsum.photos/100/800/?random";

export default function App() {
  return (
    <div className="App">
      <Example src={src}/>
    </div>
  );
}

【问题讨论】:

    标签: html css reactjs


    【解决方案1】:

    最接近您需要的是object-fit: contain 属性。 这将填充 div,具体取决于它是横向(将占据所有宽度)还是纵向(将占据所有高度),并且所有这些都保持纵横比。您只需提供div 的尺寸。

    div {
     width: 500px;
     height: 500px;
    }
    
    img {
     width: 100%;
     height: 100%;
     object-fit: contain;
    }
    

    【讨论】:

    【解决方案2】:

    将宽度限制为您的div 宽度并将height 保留为自动以保持ratio。

    .container { width: 500px }
    
    .container img { max-width: 100%; height : auto }
    

    【讨论】:

    • 我正在寻找类似的东西,当你得到图像时,你会根据 div 的大小计算图像的高度和宽度。然后动态设置图片的高度和宽度
    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 2013-02-27
    • 2013-12-21
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多