How to make an animation trigger each time the page is scrolled?

Rachel

I have a very sublet keyframe animation. I'd like for it to play every single time the page has been scrolled - not just once when coming in the viewpoint. Each time the scroll is touched. All I can find is running once in viewport - which is fine, but it needs to play every time the scroll is touched in the viewport. Any ideas?

/* CSS */
.aha-section .pixels .light {
    margin-top: 4px;
    margin-left: 33px;
    animation: slide 2s 1;
}

@keyframes slide {
    from {
        margin-left: 33px;
    }
    50% {
        margin-left: 45px;
    }
    to {
        margin-left: 33px;
    }
}

caiovisk

You will need to use some JavaScript... then you will listen to the scroll event to do the desired function...

To trigger a animation set a class to it:

.scrolled .aha-section .pixels .light {
    animation: slide 2s 1;
} 

and let the javascript add this class when the event scroll happens:

document.body.className += ' scrolled';

hope it helps to show you the direction.

let animated = false;

window.addEventListener("scroll", (event) => {
    animateOnScroll();
});

function animateOnScroll(){
  if(animated == false){
    animated = !animated;
    document.body.className += ' scrolled';  
    setTimeout(function() {
        animated = false;
        document.body.classList.remove('scrolled');
    }, 2000); //match whatever time set on css animation 
  }
}
.aha-section {
  position: fixed;
}
.aha-section .pixels .light {
    margin-top: 4px;
    margin-left: 33px;
}

.scrolled .aha-section .pixels .light {
    animation: slide 2s 1;
}

@keyframes slide {
    from {
        margin-left: 33px;
    }
    50% {
        margin-left: 45px;
    }
    to {
        margin-left: 33px;
    }
}

.scroll-emulator {
  height: 2500px;
}
<div class="aha-section">
    <div class="pixels">
        <div class="light">Light</div>
    </div>
</div>
<div class="scroll-emulator"><div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make div modal scrolled to the top each time I open it?

How to make a page that can't be scrolled?

How to trigger related animation each time trigger click event in A-frame?

How to make the page load scrolled down to <div> when loaded?

How to Make the scrollview, scroll in specific page when scrolled iOS

How to make page cannot scrolled down with button toggle?

How to make a network call to an endpoint each time a page loads

How to make animation trigger in mouseenter in AFrame?

Trigger animation on page load

How to make CSS animation to run each time the subsequent class is added to the element

How to trigger different functions when the mouse is scrolled up and scrolled down

How can I make the the google chart animation start only when it comes into view when scrolled?

How to make such an animation of page transition? [Mobile]

how to the make animation happen at the same time?

How to make one-time animation?

How can I make a postion: absolute; dialog initially appear within the viewport no matter where the page is scrolled?

How to make ViewPager destroy items right after I've scrolled to next page

How to trigger a wpf animation only if the mouse hovers for a long time?

How do I make an html file include itself without refreshing the page each time a button or link is clicked?

How can I make a circular page? (when scrolled to the end of page content, page adds the beginning of conten to the bottom and so forth)

How to trigger CSS animation

How to correctly make a page for each category

how to tell if page is scrolled all the way to the side

How to have a directive update in the page is scrolled or resized

how to keep calculating each time the page is refreshed

how to make a header fixed while content is scrolled

How to trigger on change when page is loaded form each parent

How can I make a <Route> path trigger "componentDidMount()" for each parameter?

Centre Scroll View Objects To Each Horizontal Scrolled Page - Xamarin iOS