Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

a custom element of lazy-load-image based on web-components

NotificationsYou must be signed in to change notification settings

likuner/LAZY-IMG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lazy-img

  • a custom element of lazy-load-image based on web-components.
  • it can be used in bothvue &react projects.
  • it can also be used in nativehtml.

Installing 🛠

CDN

  • you can getlazy-img through CDN.
  • you can add@+version after/lazy-img if you need to load the specified version.
<scriptsrc="https://unpkg.com/@likun./lazy-img/dist/index.js"></script>

npm

npm install @likun./lazy-img

yarn

yarn add @likun./lazy-img

Usage

Use in HTML

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>lazy-img</title><scriptsrc="https://unpkg.com/@likun./lazy-img/dist/index.js"></script></head><body><lazy-imgsrc="image.png"width="500"height="300"alt="lazy image"/></body></html>

Use in Vue and React

Importing package

import '@likun./lazy-img'

Use in Vue

<lazy-img  :src="imgSrc"  width="50%"  height="200px"  @lazyload="handleLoad"  @lazyerror="handleError"/>

Use in React

  • learn more:Using Web Components in React and🇨🇳中文.
  • className will be resolved toclassname, but you can useclass directly.
  • events triggered by web components may not be delivered correctly through the react rendering tree, you need to manually add event handlers in the react component to handle these events, the following example:
import{useState,useRef,useEffect}from'react'importstylesfrom'./LazyDemo.module.less'functionLazyDemo(){constlazyRef=useRef()const[imgSrc]=useState('image.png')useEffect(()=>{consthandleLoad=()=>{}consthandleError=(e)=>{// e.target.setAttribute('src', 'image1.png')}lazyRef.current.addEventListener('lazyload',handleLoad)lazyRef.current.addEventListener('lazyerror',handleError)return()=>{lazyRef.current.removeEventListener('lazyload',handleLoad)lazyRef.current.removeEventListener('lazyerror',handleError)}},[])return(<lazy-imgref={lazyRef}src={imgSrc}width="300px"height="200px"// class can be used hereclass={styles.lazyImage}/>)}exportdefaultLazyDemo

Attributes

ParameterExplanationTypeDefalut value
srcurl of imagestring
presrcurl of space occupying image when lazystring
widthwidth of imagenumber | string | percentage300px
heightheight of imagenumber | string | percentage200px
altalternate text description of the image when failedstring

Events

NameExplanationCallback arguments
lazyloadtriggered when image is loaded successfullyevent
lazyerrortriggered when image loading failedevent

[8]ページ先頭

©2009-2025 Movatter.jp