javascript - 在 React/TS 上调整窗口大小时调整图像大小、居中并保持纵横比

标签 javascript css reactjs

我想在调整窗口大小时调整图像大小。我想将此图像置于容器 div 元素的中心。我还想在 4 个边上填充。 (上/左/下/右:例如 15%)

现场演示:https://react-ts-f4jemk.stackblitz.io

现场编辑:https://stackblitz.com/edit/react-ts-f4jemk

它应该是这样的:

sample

当窗口调整大小时,图像应该根据窗口大小变大或变小。在这种情况下,我想保持纵横比和顶部、左侧、底部和右侧的空白,以便图像可见并在 div 上居中。

到目前为止我尝试了什么:

 updateDimensions() {

    let imgHeight = this.state.imgHeight
    let imgWidth = this.state.imgWidthMax

    let w = window,
      d = document,
      documentElement = d.documentElement,
      body = d.getElementsByTagName('body')[0],
      width = w.innerWidth || documentElement.clientWidth || body.clientWidth,
      height = w.innerHeight || documentElement.clientHeight || body.clientHeight

    if (imgWidth && imgWidth > 0) {
      imgWidth = imgWidth + width - this.state.width
      imgHeight = imgHeight + height - this.state.height
      const ratioW = width / imgWidth
      const ratioH = height / imgHeight
      this.setState({ width: width, height: height, imgHeight, imgWidth })
      return
    }

    this.setState({ width: width, height: height, imgHeight, imgWidth })

  }
  componentWillMount() {
    this.updateDimensions()
  }
  componentDidMount() {
    window.addEventListener("resize", this.updateDimensions)
    setTimeout(() => {
      const imgHeight: number = this.imgEl.clientHeight
      const imgWidth: number = this.imgEl.clientWidth
      this.setState({ imgHeight, imgWidth, imgHeightMax: imgHeight, imgWidthMax: imgWidth })

    }, 1000)
  }
  componentWillUnmount() {
    window.removeEventListener("resize", this.updateDimensions)
  }

我也尝试通过纯 CSS 来实现。但是,宽度或高度变得大于浏览器高度/宽度。

.img {
   width: auto; // or 100%
   height: 100%; // or auto
}

我的 updateDimensions() 有效,但是我的计算有误。我该如何正确处理这种情况?我怎样才能正确计算?

最佳答案

我更新了你的功能以根据图像的纵横比调整图像的大小

  updateDimensions() {
    let w = window,
      d = document,
      documentElement = d.documentElement,
      body = d.getElementsByTagName("body")[0],
      inner = d.querySelector(".inner");

    //this calculates the padding %
    const height = inner.clientHeight - inner.clientHeight * 0.15;
    const width = inner.clientWidth - inner.clientWidth * 0.15;

    let imgWidth = width;
    //calculates hight base os aspect ratio
    let imgHeight = (imgWidth * height) / width;

    //if height is greater than the inner container, set the maximun size and recalculate width base on max-height
    if (imgHeight > height) {
      imgHeight = height;
      imgWidth = (imgHeight * width) / height;
    }

    this.setState({ width, height, imgWidth, imgHeight });
  }

这是在线编辑器上的更新代码:https://stackblitz.com/edit/react-ts-jbzynn?file=index.tsx

关于javascript - 在 React/TS 上调整窗口大小时调整图像大小、居中并保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56201733/

相关文章:

html - 如何对齐我的文本元素?

html - 带有文本的翻转图像上的 CSS 不透明度,但文本上的不透明度没有变化

javascript - 直接监听组件中的 Reflux Actions

javascript - 在 where 子句上对 Access 父表的字段进行 Sequelize

javascript - 如何在鼠标滚轮滚动发生之前控制它?

javascript - css 在图像上显示按钮

javascript - 如何更新 React Hooks 中对象数组中的状态 `onChange`

reactjs - 如何使用 Axios 和 React 传递 Header JWT Token?

javascript - jQuery输入类型文件上传验证IE问题

javascript - 将变量从服务器上的外部文件加载到 HTML 文档中?