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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 记录一次mysql 主从不同步的问题操作(hashlinux)

    记录一次mysql 主从不同步的问题操作(hashlinux)

    2021年9月16日
    47
  • 内存对齐宏的定义

    内存对齐宏的定义

    2021年8月20日
    47
  • cs架构和bs架构的应用_cs bs区别

    cs架构和bs架构的应用_cs bs区别悬赏园豆:200[已关闭问题]1.如何设计C/S和B/S混合结构?2.采用webservice,B/S端采用C#,C/S端采用C、delphi、VC++,如何进行通信?3.WebService的运行机理:首先客户端从服务器的到WebService的WSDL,同时在客户端声称一个代理类(ProxyClass),这个代理类负责与WebService服务器进行Request和Response,…

    2022年9月9日
    0
  • linux加静态路由命令,LINUX添加静态路由「建议收藏」

    linux加静态路由命令,LINUX添加静态路由「建议收藏」LINUX添加静态路由建议,先用命令添加,测试生效未有其他影响后,添加到配置文件中。以本次127前置添加静态路由为例用户需要添加路由如下,命令格式为windows添加格式。route-a-p10.113.70.0mask255.255.255.010.113.65.44route-a-p10.113.70.240mask255.255.255.24010.113.6…

    2022年10月4日
    0
  • C语言if语句的基本用法

    C语言if语句的基本用法C语言if语句的基本用法一、if…1.一般形式:if(表达式){语句;}表达式:a,用非0值表示真,用0表示真;b,if(flag)相当于if(1==flag)c,浮点数无法与0比较,只能用近似的值比较;例:(1e-6)相当于1×10的-6次方;2.用于单分支选择结构;3.如含有交叉关系,使用并列的if语句;例:输出两个整数中的最大值#inclu…

    2022年5月19日
    39
  • FinSH组件用法

    FinSH组件用法概述本文简单介绍rtthread的FinSH控制台组件的用法,在实际项目中,该组件对调试非常方便。正文首先,先按上篇搭建好工程。这里,我以正点原子战舰板工程来实验。FinSH组件的文件结构如下:shell.c、msh.c这两个文件实际项目中基本不用更改,所以我们在cmd.c中增加我们需要的命令即可。不带参数的命令带参数的命令总结到这里,就简单了解了FinSH组件的用法。…

    2022年5月11日
    64

发表回复

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

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