162
11:04 pm, October 22, 2024
fade in and out elements on scroll - css js html
HTML
<div class="container">
<div class="fade fadeOut">Element 1</div>
<div class="fade fadeOut">Element 2</div>
<div class="fade fadeOut">Element 3</div>
<div class="fade fadeOut">Element 4</div>
<div class="fade fadeOut">Element 5</div>
<div class="fade fadeOut">Element 6</div>
<div class="fade fadeOut">Element 7</div>
<div class="fade fadeOut">Element 8</div>
<div class="fade fadeOut">Element 9</div>
<div class="fade fadeOut">Element 10</div>
</div>
CSS
.fade {
margin: 50px;
padding: 50px;
background-color: lightgreen;
transition: opacity 0.7s ease-in;
}
.fadeOut { opacity: 0; }
.fadeIn { opacity: 1; }
Javascript
const observerOptions = {
root: null,
rootMargin: "0px",
threshold: 0.7
};
function observerCallback(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
// fade in observed elements that are in view
entry.target.classList.replace('fadeOut', 'fadeIn');
} else {
// fade out observed elements that are not in view
entry.target.classList.replace('fadeIn', 'fadeOut');
}
});
}
const observer = new IntersectionObserver(observerCallback, observerOptions);
const fadeElms = document.querySelectorAll('.fade');
fadeElms.forEach(el => observer.observe(el));
Element 1
Element 2
Element 3
Element 4
Element 5
Element 6
Element 7
Element 8
Element 9
Element 10
External Link for fade in and out elements on scroll - css js html
View Statistics
This Week
41
This Month
116
This Year
164
Add Comment
Related Search Terms
Other Categories in Code
alpine js apps c css factorio font awesome images linux quick modals sqlite site bugs site updates slick slider sliders testing windows apps apache api apps asp bat bootstrap bootstrap templates charts cookies core css css filters css grid design elements docker domains emoji fancybox fonts foundation framework gimp git html icons ideas image formatting images javascript javascript functions jquery js linux mac misc modals mysql nginx node php php errors php function php functions php simple html dom pi400 python react regex sections simple_html_dom simplepie php site bugs site documentation slick slider sql sqlite ssh sublime svg svg css templates tools virtual box vscode vue webdev windows windows 11 windows commands wordpress