0

I'm trying to get animate.css to fire up a CSS animation when the CSS selector comes into view when scrolling down a page.

The animations work fine at the top of the page - so that bit works okay. It's just pausing or stopping the animation until they come into view.

I've found several different solutions (none that have worked so far, unfortunately) most of which use an IntersectionObserver JavaScript. I'm using WordPress, so I don't know if this is causing an issue?

I was hopeful this code (below) would work - which I adapted from this site - https://coolcssanimation.com/how-to-trigger-a-css-animation-on-scroll/

// Create the observer like the examples above
const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      entry.target.classList.add('animateThis');
      return;
    }

    entry.target.classList.remove('animateThis');
  });
});

// Get multiple elements instead of a single one using "querySelectorAll"
const squares = document.querySelectorAll('.onScroll');

// Loop over the elements and add each one to the observer
squares.forEach((element) => observer.observe(element));

I've given an element I'd like to animate an '.onScroll' CSS selector. I was hoping the above JavaScript code would change this to '.animateThis' if it came into view, and the animation would run. But so far, nothing is happening.

I don't know if the JavaScript code is actually running, I've tried manually placing the code in the header, footer using and via the WPCode plugin?

Or if my adapted JavaScript code is incorrect? Apologies if these are silly questions

I thought it would be easy to simply get a JavaScript that worked nicely with animate.css (or any CSS animation) in WordPress to trigger when in view on scrolling.

19
  • "I don't know if the JavaScript code is actually running" - then add some logging to the browser console, or put the debugger keyword in there. Where did you put this code, and when is it supposed to execute?
    – C3roe
    Commented Apr 15 at 7:18
  • Hi, I'm not sure how to test if the JavaScript code is running or not. The test site is here - 120project.co.uk/web/landmark/about-us - It's currently in the header section, I can see it when I view the page source.
    – Brian
    Commented Apr 15 at 7:30
  • "It's currently in the header section" - the elements you are trying to select don't exist yet at this point. You need to delay execution of this until DOMContentLoaded at least.
    – C3roe
    Commented Apr 15 at 7:34
  • Hi, I've moved the JavaScript into the footer section just before the closing body tag (which makes sense - thank you) But it's not changing any of the selectors, so I don't know if it's the actual JavaScript code that's the problem or an issue with the JavaScript not working?
    – Brian
    Commented Apr 15 at 7:54
  • You'll have to do a bit more debugging then ... I'd start by checking if this finds any of the target elements to begin with, console.log("Number of selected .onScroll elements:", squares.length);
    – C3roe
    Commented Apr 15 at 8:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.