On click, triggering the class "animate__animated animate__bounce" to activate

Just starting to learn html and css and came across this awesome library:

Here's the code snippet I have so far:

<head>
  <link
    rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
  />
</head>

You can see how it looks on my CodePen here: https://codepen.io/wmfznuiy-the-looper/pen/jOaMXXg

NUESTROS SERVICIOS

I'm trying to make the effect trigger on click. I tried using this code but didn't get the desired outcome:

$("#hola").click(function() {
  $('.animate__animated .animate__bounce-active').toggleClass('.animate__animated .animate__bounce-active');
});

I've gone through multiple posts and guides but still couldn't figure it out. Any help would be greatly appreciated as I'm new to this. Thank you!

Answer №1

# 1: jQuery Troubleshooting.

If you're encountering the error ReferenceError: $ is not defined in your CodePen console, it indicates a missing jQuery import. Ensure to import jQuery either through CodePen's settings or manually from jQuery's CDN.

# 2: Code Adjustment.

The initial code snippet attempts to toggle the class on elements with the specified selector when a particular element is clicked. Here's an updated version that adds and removes classes more effectively:

$('#hola').click(function () {
  $(this).addClass('animate__animated animate__bounce');
  setTimeout(() => {
    $(this).removeClass('animate__animated animate__bounce');
  }, 1000);
});

# 3: Thoughts.

Upon experimenting, it seems that applying animations directly to anchor tags like <a> may not be fully supported. Consider using other elements such as <h1>. Feel free to correct any oversight or share insights.


Result (in Code Snippet)

P/s: The demonstration below uses <h1> instead of <a> tag.

$('#hola').click(function () {
  $(this).addClass('animate__animated animate__bounce');
  setTimeout(() => {
    $(this).removeClass('animate__animated animate__bounce');
  }, 1000);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<head>
  <link
    rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
  />
</head>

<h1 id="hola" class="nav-link">NUESTROS SERVICIOS</h1>

Answer №2

Oh my goodness, thank you immensely! I am overjoyed because I thought nobody would help me since this is my first post. After countless attempts, I finally discovered the reason why none of the other codes were functioning - as you pointed out, I was using the anchor tag. Animate.style provides documentation stating that the best approach for achieving my goal is by utilizing this code snippet:

const animateCSS = (element, animation, prefix = 'animate__') =>
  new Promise((resolve, reject) => {
    const animationName = `${prefix}${animation}`;
    const node = document.querySelector(element);
    
    node.classList.add(`${prefix}animated`, animationName);

    function handleAnimationEnd(event) {
      event.stopPropagation();
      node.classList.remove(`${prefix}animated`, animationName);
      resolve('Animation ended');
    }

    node.addEventListener('animationend', handleAnimationEnd, {once: true});
  });

However, what they failed to mention is that this method does not work with the anchor tag, which explained why it wasn't working for me. Your solution worked perfectly, although it required a switch to H1 instead. I hoped to use it with the anchor tag, but alas, that's just how it goes. Thank you once again for your invaluable assistance!

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

What is the best way to ensure a DOM element exists before you can start manipulating it?

In my AngularJS application, I am utilizing Angular Material and md-select. When a user clicks on the md-select, a dropdown list appears and an overlay with the tag Name is added to the DOM. My goal is to add a class or style as soon as the user clicks on ...

Receiving a 200 response code, however the controller's store() method is not being accessed

Recently, I made the decision to switch from using a form in a blade file to a Vue-based form that utilizes Axios for posting data. After making these changes, I encountered an issue where I receive a 200 response, but my controller's store() method i ...

Could you explain the purpose of the app.use(cors()) function call?

I'm aware that accessing an API with a different domain than our own is not allowed. Nonetheless, I often observe individuals incorporating the cors module into their Express.js applications in order to interact with APIs and then utilizing it in this ...

Setting the default date in angular-bootstrap-datetimepicker: a step-by-step guide

I am currently utilizing the angular-bootstrap-datetimepicker module for allowing users to choose a date and time. How can I set a default date and time when the view initially renders? Is there an attribute available that allows us to assign a boolean val ...

Enhance the current menu item in WordPress by assigning a class to the anchor tag

function updateActiveClassOnMenu($item_output, $item, $depth, $args) { $menu_locations = get_nav_menu_locations(); if ($item->menu_order == 1){ $item_output = preg_replace('/<a /', '<a class="active" ', $item_o ...

What could be the reason my page is not refreshing after submitting a form?

After creating a quiz application that submits user answers to a MySQL database and visualizes the data using a D3 script, I encountered an issue. Despite updating the database correctly upon form submission, the page does not refresh automatically, causin ...

JavaScript: The most efficient method for locating a Regular Expression within an Object's properties

Suppose I have a scenario where I need to validate if the words in an array exist in a dictionary and take certain actions accordingly. Here's a snippet of the code that achieves this: let dictionary = { aaa : 'value1', bbb : 'val ...

When the enter key is pressed in the input box, the page resets instead of triggering

After extensive research, I have found that none of the available resources have been able to resolve the specific issue I am facing. Objective: My goal is to trigger the function $("#search").click(); when text is entered into the box and th ...

What is the best method for dynamically loading CSS using JavaScript?

Due to a certain condition I have, I am unable to load some CSS and JS in my HTML. As a solution, I've decided to dynamically pull them using $.get(). While I have successfully tested pulling and executing js remotely, I am facing some challenges when ...

Issue with CKEditor within a ColorBox dialog box

Could someone please assist me? I am facing an issue with my HTML page where I have incorporated a ColorBox modal popup. Inside the ColorBox popup, there is a CKEditor. The problem is as described below: In Internet Explorer, the CKEditor functions proper ...

Tips for Retrieving Information from Firebase in an Angular Application

I am facing an issue with my code where the ngFor directive is not working as expected when I use read_CheckOuts to return data from the database. Below are snippets of my code: crud.service.ts import { AngularFireDatabase} from '@angular/fire/datab ...

What could be causing the browser.get method to fail to redirect to the specified URL?

I have organized my files in a folder structure that looks like this: project structure Currently, I am following the steps outlined in this particular tutorial The contents of my package.json file are as follows: { "name": "node_cucumber_e2e", "ver ...

Typescript Class Getting Vacant

When working with a specific setup in TypeScript, I encountered a situation where at runtime in JavaScript, I ended up with empty class objects. This occurred when two classes referenced each other, leading to one of them being empty within the scope of th ...

Animate custom scrollbars with jQuery for a unique browsing experience

I am experimenting with creating a scrolling animation using mCustomScrollbar Currently, I have managed to obtain the current scrollTop and animate it to a specific height using the code below: $("#content").mCustomScrollbar({ scrollButtons:{ ...

Transfer data from a div to JavaScript

Within a php file, there is a div in which an id and some data are returned. Take a look at the code: <div id="dom-target" style="display: none;" data-stuff="2017, 8, 20, 0, 0"></div> So, I am trying to retrieve the value of data-stuff and pa ...

Adding Quasar to Your Nuxt App: A Step-by-Step Guide

I'm looking to incorporate Quasar into my current Nuxt project. As I delved into the Quasar documentation, I noticed that the installation guide only highlights their own CLI tool, which doesn't offer support for Nuxt integration. Additionally, I ...

Retrieve the session variables in a controller while making an ajax request

In my controller, I am populating certain fields with data. public string AjaxLogin() { //some code to check admin or not Session["UserName"] = "Smith"; if(type="Admin") { Session["UserRo ...

Error in Internet Explorer when using SyntaxHighlighter and jQuery - "Property 'slice' cannot be accessed: object is null or undefined"

I have a strong liking for SyntaxHighter - it presents code in a very stylish manner. However, I have encountered a frustrating JS error in IE7 and IE8 that is hindering me from using this wonderful plugin to its full potential. Let's run a test case ...

Influence of Asynchronous State on the Conditional Visibility of an Element

I am encountering an issue on a screen (parent) that contains a FlatList with Child elements in the renderItem. Each Child element has a Pressable component, which should display a Checked Icon and change its background color when clicked by the user. Th ...

Is there a way to maintain formdata between components in Angular?

With only the tools of @Input, @Output, routing, and activatedRoute at my disposal, I set out to create a dynamic application. In this project, there are two crucial components: addbook and showbook. The addbook component features a form with a submit bu ...