Image with declared width and height renders square before load

后端 未结 6 2355
不思量自难忘°
不思量自难忘° 2021-02-13 11:15

I have images with declared widths and heights, e.g.:

\"bar\"

They are within

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-13 11:43

    The following is the only solution that worked 100% of the time, and is much simpler: https://css-tricks.com/preventing-content-reflow-from-lazy-loaded-images/

    In short you can use an SVG placeholder image that contains the dimenions:

    Lazy loading test image
    

    Here is an React example:

    const placeholderSrc = (width, height) => `data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}"%3E%3C/svg%3E`
    
    const lazyImage = ({url, width, height, alt}) => {
      return (
        {alt}
      )
    }
    

提交回复
热议问题