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


相关推荐

  • 关于锚点

    关于锚点设置一个锚点文字关于锚点的一些示例 1 nbsp 链接一个空锚点 默认链接到网页顶部 2 nbsp 链接到本网页里 name meizz 的锚点 3 nbsp 链接到 test asp 里 meizz 的锚点

    2025年7月10日
    3
  • Docker 安装 RabbitMQ[通俗易懂]

    Docker 安装 RabbitMQ[通俗易懂]Docker安装RabbitMQ1RabbitMQ端口作用2RabbitMQ常用命令2.1用户管理2.2用户角色2.3用户权限2.4节点类型2.5启用插件3Docker安装RabbitMQ4Docker安装MySQL85Docker安装Redis63Docker安装Yapi1RabbitMQ端口作用RabbitMQ端口作用4369epmd,RabbitMQ节点和CLI工具使用的对等发现服务5672、5671由不带TLS和带

    2022年5月24日
    53
  • 哪些软件是用C++写的

    哪些软件是用C++写的http://www.cppblog.com/Chipset/archive/2008/12/17/69625.htmlC++的应用C++Applications2013年6月27日更新(Englishversion):http://www.stroustrup.com/applications.html这里有一个有关系统、应用程序和库的列表,列表中的

    2022年6月2日
    46
  • 一起谈.NET技术,关于大型ASP.NET应用系统的架构—如何做到高性能高可伸缩性…

    一起谈.NET技术,关于大型ASP.NET应用系统的架构—如何做到高性能高可伸缩性…

    2021年8月13日
    77
  • sinx的泰勒展开式

    sinx的泰勒展开式sinx 的泰勒展开式求解过程思路 sinx 可以如何 展开 写成式子就是 最后以省略号结束 代表 无穷 需要求的就是 a0 a1 a2 的值 准确地说就是通项公式 然后 我们就可以开始 微分 了 就是等式两边同时 不停地微分下去 左边的三角函数的微分 其实是四个一循环的 sinx cosx sinx cosx 再回到 sinx

    2025年6月25日
    0
  • 最近使用百度分享的api,引发 了一下问题,微博分享的页面跳转不成功

    最近使用百度分享的api,引发 了一下问题,微博分享的页面跳转不成功1、导致问题的原因有可能是百度分享的服务器有时候无法跳转到微博(个人分析)2、最后我的解决方式,点击事件,让浏览器直接跳转到微博分享的页面window.location.href="http://v.t.sina.com.cn/share/share.php?title=“自定义标题”&amp;pic=“自定义分享图片地址”&amp;searchPic=false"searchP…

    2022年10月8日
    4

发表回复

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

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