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


相关推荐

  • [LeetCode] 046: Longest Substring Without Repeating Characters[通俗易懂]

    [LeetCode] 046: Longest Substring Without Repeating Characters[通俗易懂][Problem]Givenastring,findthelengthofthelongestsubstringwithoutrepeatingcharacters.Forexample,thelongestsubstringwithoutrepeatinglettersfor”abcabcbb”is”abc”,whichthelength

    2022年9月19日
  • hdu 1520Anniversary party(简单树形dp)

    hdu 1520Anniversary party(简单树形dp)

    2021年12月3日
  • @param注解的用法解析_getmapping注解用法

    @param注解的用法解析_getmapping注解用法1、概述首先明确这个注解是为SQL语句中参数赋值而服务的。@Param的作用就是给参数命名,比如在mapper里面某方法A(intid),当添加注解后A(@Param(“userId”)intid),也就是说外部想要取出传入的id值,只需要取它的参数名userId就可以了。将参数值传如SQL语句中,通过#{userId}进行取值给SQL的参数赋值。2、实例:实例一:@Param注解基本类型的参数mapper中的方法:publicUse…

    2022年10月21日
  • C++学习——继承与派生「建议收藏」

    C++学习——继承与派生「建议收藏」普通继承例题:定义一个名为Phone的手机类,有如下私有成员变量:品牌、型号、CPU、电池容量,如下成员函数:构造函数初始化手机的信息。在此基础上派生出NewPhone类,派生类增加了两个新的私有数据成员,分别用于表示颜色和价格,增加了一个成员函数用来输出手机信息。代码如下:#include <iostream>#include<string>#include&l…

    2022年8月18日
  • 数据仓库建模

    数据仓库建模

    2021年11月27日
  • mac配置vscodec语言环境_vscodejava环境变量配置

    mac配置vscodec语言环境_vscodejava环境变量配置登陆终端输入java-version查看java的版本输入java-verbose查看java的安装环境路径没有的话,自己去搜一下怎么安装java的jdk,记得选1.18版本安装路径在这里安装完jdk后,输入/usr/libexec/java_home-V查看jdk版本复习什么是JDKJavaDevelopmentKit开发工具包包含Java开发工具和JREjava开发工具:编译工具javac.exe,jar.exe打包工具等。JRE=JVM+javaS

    2022年10月3日

发表回复

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

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