React图片懒加载

React图片懒加载话不多说了,创建一个LazyLoad.js的React组件,然后将下面的代码复制过去:importReactfrom’react’//import’./lazyload.css’//thresholdconstthreshold=[0.01]classLazyLoadextendsReact.Component{construc…

大家好,又见面了,我是你们的朋友全栈君。

React图片懒加载
  • 话不多说了,创建一个LazyLoad.js的React组件,然后将下面的代码复制过去:
import React from 'react'
// import './lazyload.css'
// threshold
const threshold = [0.01]
class LazyLoad extends React.Component{ 
   
    constructor(props){ 
   
        super(props)
        this.state = { 
   
            io: null,
            refs: null,
            images: null,
            loading: true
        }
        this.handleonload = this.handleonload.bind(this)
    }
    UNSAFE_componentWillMount(){ 
   
        var { 
   ImgClassName, src, alt, ImgStyle } = this.props.state
        ImgClassName = ImgClassName ? ImgClassName : 'lazyload-img'
        alt = alt ? alt : 'antd-lazyload'
        var images = []
        var refs = []
        const ref = React.createRef()
        refs.push(ref)
        images.push(
            <img className={ 
   ImgClassName} ref={ 
   ref} data-src={ 
   src} alt={ 
   alt} style={ 
   { 
   ...ImgStyle}} />
        )
        this.setState({ 
   
            refs,
            images
        })
    }
    componentDidMount(){ 
   
        const io = new IntersectionObserver(entries=>{ 
   
            entries.forEach(item=>{ 
   
                if(item.intersectionRatio <= 0) return
                var { 
    src } = this.props.state
                const { 
    target } = item
                var image = new Image()
                image.src = src
                image.onload = ()=>{ 
   
                    this.setState({ 
    loading: false })
                    target.src = target.dataset.src
                }
            })
        },{ 
   
            threshold
        })
        this.setState({ 
    io })
    }
    handleonload(){ 
   
        var { 
    io, refs } = this.state
        refs.forEach(item=>{ 
   
            io.observe(item.current)
        })
    }
    render(){ 
   
        var { 
    BoxClassName, width, height, BoxStyle } = this.props.state
        BoxClassName = BoxClassName ? BoxClassName : 'lazyload-img-box'
        var { 
    images } = this.state
        return (
            <div className={ 
   BoxClassName} style={ 
   { 
   width, height, ...BoxStyle}}>
                { 
   images}
                <img onError={ 
   this.handleonload} src='' alt='antd-lazyload' style={ 
   { 
   display: 'none'}} />
            </div>
        )
    }
}
export default LazyLoad
  • 使用的时候,像这样:
<LazyLoad 
	state={ 
   { 
   
		src: 'http://example.com/1.jpg',
		alt; '1.jpg',
		BoxClassName: 'lazyload-box', // 这是容器的类名
		ImgClassName: 'lazyload-img' // 这是img的类名
	}}
></LazyLoad>
  • 想知道更多使用请点击这里
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125903.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月6日 下午6:40
下一篇 2022年4月6日 下午6:40


相关推荐

  • robots.txt_Robots.txt:互联网幕后的一瞥「建议收藏」

    robots.txt_Robots.txt:互联网幕后的一瞥「建议收藏」robots.txtHere’sanexercise:openanewtabandtypeintheURLofyourfavoritewebsite.Add/robots.txttotheendandhitenter.这是一个练习:打开一个新选项卡,然后输入您喜欢的网站的URL。将/robots.txt添加到末尾,然后按Enter。There’…

    2022年5月20日
    43
  • thinkphp3.2.3中设置路由,优化url

    thinkphp3.2.3中设置路由,优化url

    2021年10月23日
    103
  • install scim to ubuntu

    install scim to ubuntu

    2021年4月23日
    184
  • 嵌入式实时操作系统UCOSII[通俗易懂]

    嵌入式实时操作系统UCOSII[通俗易懂]何谓操作系统1.什么是操作系统?操作系统是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行。介于APP和硬件之间。2. 为什么要用操作系统?1)相比裸机,可以实现更加复杂的功能。2)屏蔽硬件。使得上层应用APP的移植性更好。常见操作系统常见操作系统安卓、IOS、Windows、Linux、塞班、V…

    2022年5月4日
    169
  • 【OpenClaw】——小龙虾极简接入Discord教程

    【OpenClaw】——小龙虾极简接入Discord教程

    2026年3月14日
    2
  • JAVA之父—-James Gosling(詹姆斯·高斯林)

    JAVA之父—-James Gosling(詹姆斯·高斯林)JAVA之父詹姆斯·高斯林(JamesGosling)是一名软件专家,1955年5月19日出生于加拿大,Java编程语言的共同创始人之一,一般公认他为“Java之父”。(百度百科)有些人注定是要出名的,比如微软创始人比尔盖茨,有事没事你都能看到他,但也有一些人,做事不比盖茨差,却注定要泯然人海。如果不是学过Java恐怕没有几个人知道詹姆斯.高斯林大叔。如果没有Java人类就像不会说话的婴儿。人们”爱死了”盖茨,因为他给世界带来了看得见的操作系统;可是没有人会说“我爱死了高斯林”,尽管他所创

    2022年7月7日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号