DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Theme-Based Front-End Architecture Leveraging Tailwind CSS for White-Label Systems
  • Scrolling With Konva.js and React
  • Reasons to Use Tailwind CSS in React Native Development Projects
  • Practical Example of Using CSS Layer

Trending

  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  1. DZone
  2. Coding
  3. JavaScript
  4. Fade in Lazy Loaded Images With React and CSS: A Quick Guide

Fade in Lazy Loaded Images With React and CSS: A Quick Guide

A web developer and MVB shows how he created a component in React that allows you to fade-in lazy loaded images with CSS and React.

By 
Swizec Teller user avatar
Swizec Teller
·
Feb. 05, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

Say you want to lazy load some images on your website. You don't want them to just pop into existence and scare the user. A nice fade in effect works much better.

Here's the problem: There's no good pre-built React component for this. Or I suck at finding it.

There's a react-lazyload, which does the lazy loading part for you. It keeps components from mounting into the DOM until the user actually sees them.

This helps make your website faster to load. No need to load large images until a user can actually see them.

But the default experience can be jarring. When images finally load, they just pop into view.

react-lazyload offers a fade-in example, but it's outdated and doesn't work with modern libraries. And it's cumbersome to use.

So I built a general FadeIn component. I'll opensource it, but it needs some polish first, and I'd like your opinions on how to make it better.

The component is just 40 lines of code. Pretty simple.

class FadeIn extends React.Component {
  state = {
    loaded: false ,
  } ;
  onLoad = ( ) => this.setState ( { loaded: true } ) ;

  render( ) {
    const { height, children } = this.props ,
      { loaded } = this.state ;

    return (
      <LazyLoad height= {height} offset= { 150 } >
        <Transition in = {loaded} timeout= {duration} >
          {state =>
            <div style= { { ...defaultStyle , ...transitionStyles [state] } } >
              {children( this.onLoad ) }
            </div> }
        </Transition>
      </LazyLoad>
    ) ;
  }
}
FadeIn.propTypes = {
  height: PropTypes.number ,
  children: PropTypes.func ,
} ;

FadeIn keeps local loaded state to keep track of what to show. false means "stay transparent," true means "fade to full opacity."

It uses react-lazyload's LazyLoad component to handle the lazy loading part, and Transition from react-transition-group to drive the CSS transition for fading in. This is the part that's changed a lot since the official fadein lazyload example.

Using the children render function approach, you can ask FadeIn to render anything you want. It gets wrapped in a div that handles the fade effect.

All you have to do is trigger the onLoad callback once your content is ready. When your image is done loading for example.

You render <FadeIn>, give it a height so things don't jump around, and pass a children function that takes an onLoad callback. When you're ready to trigger the transition, you call onLoad and FadeIn does its thing.

Did you know onLoad was a built-in DOM callback that all browsers support? I had no idea.

onLoad is important because without it the FadeIn transition might end before the image has even come down the pipe. That's the issue I was facing at first when I livecoded this.

Recommendations?

I want to opensource this. I know it's simple, but it could save people like an hour of their time. That's worth it, right?

What else does it need to do? What should I name it? Would you use it?

Shout at me on Twitter.

You should follow me on twitter, here.

CSS Fade In (software) React (JavaScript library)

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Theme-Based Front-End Architecture Leveraging Tailwind CSS for White-Label Systems
  • Scrolling With Konva.js and React
  • Reasons to Use Tailwind CSS in React Native Development Projects
  • Practical Example of Using CSS Layer

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!