Animate.css: activate animations upon entering the viewport during scrolling

I'm currently testing out animate.css for my project.

Here's a snippet of my code:

<h2 class="lazyload animate__animated animate__rotateInDownLeft" data-link="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">Lorem ipsum</h2>

Don't worry about animate.css being loaded lazily in this case.

You can check out my page here:

The h2 element we are focusing on is the one at the bottom with the text "Lorem ipsum".

The Issue

Everything works fine when I reload the screen with the h2 already visible.

However, what I want to achieve is an animation effect while scrolling - the h2 should start moving only when it comes into view as you scroll down.

Is there a way to make this happen?

Answer №1

@Mohammed came up with an interactive solution using jQuery.

On the other hand, here is a straightforward approach using vanilla JS and incorporating Animate.css, Waypoints, along with a few lines of CSS:

Javascript:

// Link to helpful solutions: https://stackoverflow.com/a/74810616/3929620, https://stackoverflow.com/a/32677672/3929620, https://stackoverflow.com/a/69614783/3929620
const animatedElements = [].slice.call(document.querySelectorAll('.__animate__animated'))
animatedElements.map(function (element) {
    new Waypoint({
        element: element,
        offset: 'bottom-in-view',
        handler: function(direction) {
            // Further reference: https://stackoverflow.com/a/56914528/3929620
            element.className = element.className.replace(/__animate__/g, 'animate__')
            this.destroy()
        },
    })
})

CSS:

/* Utilizing attribute substrings per W3C recommendations */
[class*="__animate__"] {
    opacity: 0;
}

HTML:

<div class="__animate__animated __animate__fadeIn">
    <!-- Insert your code here -->
</div>

Feel free to suggest any enhancements! ;)

Answer №2

It appears that animate.css does not offer built-in scroll animations, typically requiring JavaScript. To achieve this effect, consider implementing a window.scroll event listener to detect when your h2 element enters the viewport and then apply the class animate__rotateInDownLeft.

Answer №3

Unfortunately, the animate.css library does not have this specific feature built in. You will need to utilize JavaScript/jQuery to implement any custom animations or effects you desire.

I've been experimenting with creating my own animation library recently and have managed to develop a more robust solution.

To see my dynamic animations in action, check out mycustomanimations.com

You can also find the code on Github at MyCustomAnimations

If you have any questions, please leave a comment below.

If you encounter any issues while using the library, don't hesitate to open an issue on Github for support.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I need help with this Jquery code - trying to target an element with a specific attribute value. What am I missing here?

I am attempting to selectively add a border around the .L1Cell element where the attribute name parent is equal to Workstation. However, it appears to be applying the border to all .L1Cell elements. Here is the link to the JSFIDDLE $(document).ready(func ...

Eliminating the excess space at the beginning of dialogues

I'm struggling with removing the unwanted whitespace above the title in my Material UI dialog popup. Even after applying a background color to the title and content area, I still have a white margin at the top that I can't seem to eliminate. Doe ...

Enhancing the appearance of h3 headings using CSS without changing the HTML code

I'm looking to add some style to the h2 elements on my website. Here is an example of a website where I like the styling: Another reference site for comparison is: If you look at the first h3 heading that reads "Head To Head Comparison Between Merge ...

Hovering over an element will change its background color along with adjusting the background

I encountered a situation with the following code: CSS: <style> div.testy { border:1px solid black; } div.testy:hover { background-color:red; } </style> HTML: <div class="testy" style="height:100px; back ...

Effective method for centering a navigation bar vertically within a section element without relying on absolute positioning

Let's explore a unique way to vertically center content within a div while steering away from fixed values. Interestingly, the typical solutions don't always work as expected in a section environment. The challenge now is: How can we center the ...

Positioning a div inside another div using CSS

I'm having issues with a nested div causing trouble for me. <div style="border:solid 20px #ccff33;border-radius:10px;height:300px;width:300px;margin:20px;display: inline-block"> <img src="images/f35.jpg" style="margin-right:10px" /> ...

Step-by-step guide to showing a div upon clicking an element using jQuery

I am facing a challenge with an invisible div that I need to make visible upon clicking an element. However, my current approach does not seem to be working. Can you help me figure out what I am missing? I attempted to target <a class=".demo"> using ...

Adjusting the width of the responsive design is causing additional white space to appear in

I have a website that uses responsive CSS, including media queries at 768px, 480px, and 320px. However, when I resize the screen below 1040px, I notice some extra space appearing on the right margin of the entire page and I'm struggling to identify th ...

Improve the design of the email newsletter

Having some trouble coding a particular element in my email newsletter layout. This is the desired outcome: View Screenshot Here's what I currently have: View Screenshot Any idea what could be going wrong here? Check out the code snippet below: &l ...

Split the text on the left side before disrupting the flow on the following line

I recently encountered an issue with my code: https://jsfiddle.net/qchtngzf/1/ When the browser window is too narrow, the div.right text gets pushed onto a new line. Ideally, I would like the div.left text to shrink and break onto a new line instead, whil ...

Alter the class following modifications to the list

https://i.stack.imgur.com/QZob0.pngIn my dual list, the data is displayed in a ul li format fetched from a JSON file. Users can move items between the two lists. However, I am facing an issue where I want to apply a property that only displays content with ...

Issue with custom font not appearing correctly within SVG pattern background image

I am currently utilizing an SVG design featuring a unique font in order to apply that design as a background image on an HTML page. While everything displays correctly in Chrome and Safari, Firefox seems to be giving me some trouble: Firefox properly re ...

The functionality of jQuery click events seems to be disabled on the webpage, however, it is working perfectly fine on jsFiddle

After thoroughly checking the js tags and ensuring everything is properly closed, it's frustrating when the JavaScript suddenly stops working. The code functions perfectly in JSFiddle, leading me to believe that the issue may lie with something I&apos ...

How to eliminate vertical spacing between rows in Bootstrap?

I am looking to eliminate the unsightly vertical spacing between the bootstrap rows displayed below: https://i.sstatic.net/dgR2z.png The code appears as follows: <div class="outerbox container-fluid vw-100"> <div class="row vw-100 justify-co ...

Move the DIV element to a static section within the DOM

In my Vue app, I have implemented methods to dynamically move a DIV called 'toolbox' to different sections of the DOM. Currently, the DIV is positioned on the bottom right of the screen and remains static even when scrolling. My goal is to use t ...

The Transition Division Is Malfunctioning

I've encountered an issue where I am trying to move a div by adjusting its left property, but no transition is taking place. Regardless of the changes I make to my code, the result remains the same. Below is the current state of the code. Can anyone i ...

Issue with Google Maps: undesired change in map size

Welcome to my app! Check out the link to my app here: http://jsbin.com/axeWOwAN/1/edit If you prefer a full-screen view, click here: http://jsbin.com/axeWOwAN/1 I'm encountering an issue with the map on the second page of my app. The map works perf ...

Modify the Embed Audio Player in Google Drive

I am looking to integrate audio files from Google Drive into my website. While I have successfully embedded them, the issue lies in not being able to customize the player (as shown in the image below). Is there a way to modify the player? <iframe ...

Is it practical to extract the page type/name from the CSS of the selected tab?

My website has multiple tabs on a single page and I want to integrate Google Analytics code. However, since all the tabs are part of the same page, I need a way to track which tab the user is interacting with. Would using CSS to check for the selected tab ...

Learn how to separate two SCSS files and compile them into individual CSS files

Currently, I have two SCSS files that need to be compiled into separate CSS files. For one of the SCSS files, my script looks like this: "watch:sass": "node-sass assets/stylesheets/custom.scss assets/stylesheets/custom.css -w", "compile:sass": "node-sass ...