最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 纯css实现图片在固定大小下的最大展示--img-fit实现

    正文概述 掘金(无中生根)   2021-03-27   722

    一般来说我们需要这样做(获取img大小,判断是width还是height比较大,根据比较大的一方设置最大值,其他的设置自适应)

    export function AutoStretchBaseWidthOrHeightImg ({ src, width, height }) {
      const [imgWidth, setImgWidth] = useState('auto')
      const [imgHeight, setImgHeight] = useState('auto')
      useEffect(() => {
        const img = document.createElement('img')
        img.src = src
        document.body.appendChild(img)
        const imgRect = img.getBoundingClientRect()
        if (imgRect.width > imgRect.height) {
          setImgWidth(width + 'px')
        }
        if (imgRect.width <= imgRect.height) {
          setImgHeight(height + 'px')
        }
        document.body.removeChild(img)
      }, [width, height, src])
      return <div style={{ width: `${width}px`, height: `${height}px` }} className="flex flex-center-y flex-center-x">
        <img src={src} width={imgWidth} height={imgHeight}  />
      </div>
    }
    

    但是这样会有一个问题,就是需要发起两个请求,这样导致服务器压力增大,或者对象存储多收钱,或者展示慢的问题。

    这时候如果不依赖JS,那就很完美,比如backgroundImage实现,实现代码

    const AutoStretchBaseWidthOrHeightImgStyled = styled.div`
    div{
      background-repeat: no-repeat;
      background-size: contain;
      background-position: center;
    }
    `
    export function AutoStretchBaseWidthOrHeightImg ({ src, width, height }) {
      return <AutoStretchBaseWidthOrHeightImgStyled>
        <div style={{ width: `${width}px`, height: `${height}px`, backgroundColor: 'rgba(0,0,0,.03)', backgroundImage: `url(${src})` }}></div>
      </AutoStretchBaseWidthOrHeightImgStyled>
    }
    

    这时候是完成了,但是假如我们需要给图片不展示的时候出现一个默认图,怎么办?backgroundImage可没有imgonError.

    于是这时候object-fit出场了

    object-fit是什么?

    MDN这样说的object-fit CSS 属性指定可替换元素的内容应该如何适应到其使用的高度和宽度确定的框

    我们也不管啥意思,反正可以解决目前问题,具体查看MDN objecr-fit

    于是backgroundImage+object-fit+onError 就可以实现不可控的图片以最合适的位置展示在方框内,并可以设置黑边颜色,并可以设置图片加载异常的展示,下面看下代码吧。

    const AutoStretchBaseWidthOrHeightImgStyled = styled.div`
    div{
      background-repeat: no-repeat;
      background-size: 63px;
      background-position: center;
      background-color: rgba(0, 0, 0, 1);
      img{
        background-color: rgb(247, 247, 247);
        object-fit: contain;
      }
    }
    `
    export function AutoStretchBaseWidthOrHeightImg ({ src, width, height }) {
      const [imgShow, setImgShow] = useState(true)
      return <AutoStretchBaseWidthOrHeightImgStyled>
        <div style={{ width: `${width}px`, height: `${height}px`, backgroundImage: `url(${errorImg})` }}>
          {imgShow && <img width={width} height={height} src={src}  onError={() => setImgShow(false)} />}
        </div>
      </AutoStretchBaseWidthOrHeightImgStyled>
    }
    
    

    --完--


    起源地下载网 » 纯css实现图片在固定大小下的最大展示--img-fit实现

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元