Skip to content

Commit

Permalink
Merge pull request #1828 from rtvenge/fix/scroll-perf
Browse files Browse the repository at this point in the history
Adding requestAnimationFrame() to scroll sniffing for better perf

Props to @rtvenge for his contribution!
  • Loading branch information
HardeepAsrani authored Aug 31, 2023
2 parents 997b4f5 + c42b33e commit d12d6c5
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/animation/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,26 @@ window.addEventListener( 'load', () => {
}

window.addEventListener( 'scroll', () => {
for ( const element of elements ) {
if (
element.getBoundingClientRect().top <=
window.innerHeight * 0.95 &&
0 < element.getBoundingClientRect().top
) {
requestAnimationFrame( () => {
for ( const element of elements ) {
if (
element.animationClasses &&
0 < element.animationClasses.length
element.getBoundingClientRect().top <=
window.innerHeight * 0.95 &&
0 < element.getBoundingClientRect().top
) {
const classes = element.animationClasses;
classes.forEach( ( i ) => element.classList.add( i ) );

element.classList.remove( 'hidden-animated' );
delete element.animationClasses;
if (
element.animationClasses &&
0 < element.animationClasses.length
) {
const classes = element.animationClasses;
classes.forEach( ( i ) => element.classList.add( i ) );

element.classList.remove( 'hidden-animated' );
delete element.animationClasses;
}
}
}
}
});
});
});

Expand Down

0 comments on commit d12d6c5

Please sign in to comment.