Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

alireza valizade
alireza valizade

Posted on • Edited on

     

Background-Image-On-Load

Background-Image-On-Load

A simple way to checkbackground-image is loaded.

Installation

npm:

npminstallbackground-image-on-load--save
Enter fullscreen modeExit fullscreen mode

yarn:

yarn add background-image-on-load
Enter fullscreen modeExit fullscreen mode

Usage

importReact,{Component}from'react';importBackgroundImageOnLoadfrom'background-image-on-load';classAppextendsComponent{state={bgIsLoaded:false};render(){const{bgIsLoaded}=this.state;return(<divstyle={{height:300,backgroundPosition:'center',backgroundSize:'cover',backgroundImage:`url(${!bgIsLoaded?'https://picsum.photos/310/310?blur':'https://picsum.photos/1600/310'})`}}><BackgroundImageOnLoadsrc={'https://unsplash.it/1200/310?random'}onLoadBg={()=>this.setState({bgIsLoaded:true})}onError={err=>console.log('error',err)}/></div>);}}exportdefaultApp;
Enter fullscreen modeExit fullscreen mode

If you like this library please click on the start button on github.

https://github.com/alirezavalizade/background-image-on-load

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
notrealdev profile image
notrealdev
  • Joined

Hi, do you have valilla js version for this?

CollapseExpand
 
alirezavalizade profile image
alireza valizade
  • Location
    NL
  • Work
    Frontend engineer at Frontmen
  • Joined

it's only 3 lines. the rest is customizable

const img = new Image(src);img.onload = () => {  .....};
CollapseExpand
 
notrealdev profile image
notrealdev
  • Joined

Not works for me.
backgroundImage does not work completely in my opinion.
I must use dataURL for it, thank you.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    NL
  • Work
    Frontend engineer at Frontmen
  • Joined

More fromalireza valizade

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp