king
2021-11-18 c98e45bfac25e9110ad0383faac54a54d98ea9d5
src/components/Image/index.jsx
@@ -6,6 +6,8 @@
    let Img = new Image()
    Img.src = this.props.url
    
    if (!this.ImageWrapDom) return
    if (Img.complete) {
      this.setSize(Img.width, Img.height)
    } else {
@@ -20,13 +22,18 @@
  }
  setSize = (width, height) => {
    if (!this.ImageWrapDom) return
    const { clientWidth, clientHeight } = this.ImageWrapDom
    if (!clientWidth || !clientHeight || !width || !height) return
    let ratio = (width / height) / (clientWidth / clientHeight)
    if (ratio > 1) {
    if (ratio > 1.5) {
      let _height = Math.floor(height * (clientWidth / width))
      this.ImageDom.style.height = 'auto'
      this.ImageDom.style.top = ((clientHeight - _height) / 2) + 'px'
    } else if (ratio > 1 && ratio < 1.5) {
      let _width = Math.floor(width / (height / clientHeight))
      this.ImageDom.style.width = _width + 'px'
      this.ImageDom.style.left = '-' + ((_width - clientWidth) / 2) + 'px'