mirror of
https://github.com/pabloquablo/RTA-test.git
synced 2026-03-06 13:27:20 +01:00
refactor fade-in effect in script.js for smoother opacity transition
This commit is contained in:
15
script.js
15
script.js
@@ -2,25 +2,26 @@ window.onload = function() {
|
|||||||
// Select all paragraphs that immediately follow an h2 element
|
// Select all paragraphs that immediately follow an h2 element
|
||||||
const articles = document.querySelectorAll('h2 + p');
|
const articles = document.querySelectorAll('h2 + p');
|
||||||
|
|
||||||
articles.forEach(article => {
|
articles.forEach((article, index) => {
|
||||||
// Set initial opacity of each article to 0 (hidden)
|
// Set initial opacity of each article to 0 (hidden)
|
||||||
article.style.opacity = '0';
|
article.style.opacity = '0';
|
||||||
|
|
||||||
// Function to gradually increase the opacity of the article
|
// Function to gradually increase the opacity of the article
|
||||||
const fadeIn = () => {
|
const fadeIn = () => {
|
||||||
let opacity = 0;
|
let opacity = 0;
|
||||||
const interval = setInterval(() => {
|
const increaseOpacity = () => {
|
||||||
opacity += 0.05;
|
opacity += 0.05;
|
||||||
article.style.opacity = opacity;
|
article.style.opacity = opacity;
|
||||||
|
|
||||||
// Stop the interval when opacity reaches 1 (fully visible)
|
// Continue the animation until opacity reaches 1 (fully visible)
|
||||||
if (opacity >= 1) {
|
if (opacity < 1) {
|
||||||
clearInterval(interval);
|
requestAnimationFrame(increaseOpacity);
|
||||||
}
|
}
|
||||||
}, 30);
|
};
|
||||||
|
requestAnimationFrame(increaseOpacity);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delay the fade-in effect based on the article's index
|
// Delay the fade-in effect based on the article's index
|
||||||
setTimeout(fadeIn, 500 * articles.indexOf(article));
|
setTimeout(fadeIn, 500 * index);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user