DEV Community

Kay Gosho
Kay Gosho

Posted on

2 1

Fade in Image without jQuery

I don't know how many people have wrote this kind of articles, but wrote fade in image or text without jQuery.

.fadein {
  opacity: 0;
}
.fadein.is-active {
  opacity: 1;
  transition: all ease 0.65s;
}
Enter fullscreen mode Exit fullscreen mode
// Fade in Threshold
const screenOffset = window.innerHeight / 2 

const elements = document.getElementsByClassName('fadein')
for (let element of elements) {
  window.addEventListener('scroll', () => {
    if (window.scrollY + screenOffset > element.offsetTop) {
      element.classList.add('is-active')
    }
  })
}
Enter fullscreen mode Exit fullscreen mode
  <p style="height: 100vh">Prints out this usage information.</p>
  <img class="fadein" src="https://www.google.co.jp/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
Enter fullscreen mode Exit fullscreen mode

If you use React, you should write the above code in componentDidMount.

Ref:

http://brian.hatenablog.jp/entry/floating-fade-in

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (3)

Collapse
 
equinusocio profile image
Mattia Astorino • Edited

You should use data attributes to show Ui state changes

Collapse
 
acro5piano profile image
Kay Gosho • Edited

Thank you for your comment.
Did you mean attribute like <img src="..." data-active />?
I will try it though I actually do not know the advantage.

Collapse
 
equinusocio profile image
Mattia Astorino

Because you can make it readable by accesibility technologies

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay