Manipulating CSS animations through JQuery

Currently, my animation is triggered by a :hover event. However, I would like to change it so that the animation starts when a button is clicked. Can someone guide me on how to manipulate the CSS using JQuery?

//CSS

.mouth{
  top: 268px;
  left: 273px;
  position: relative;
}

.frame:hover .mouth {
  animation-name: talk;
  animation-duration: 0.75s;
  animation-iteration-count: 5;
}

@keyframes talk {
  0%, 100%{
    top: 268px;
    left: 273px;
  }

  50% {
    top: 308px;
    left: 273px;
  }
}

//JQuery

  $('#getQuote').click(function() {

  });

Answer №1

Swap out the :hover pseudo-class with a different class, then use jQuery to apply this new class to the element that was clicked.
By doing this, we ensure that when an item is clicked on, the extra class is included and the necessary conditions are met to activate the animation.

Answer №2

It's a simple process to achieve this effect. You will have to make some adjustments to your CSS code.

Instead of relying on the :hover selector, create a new class such as .StartAnimation. Update your CSS accordingly:

.StartAnimation .mouth{
    animation-name: talk;
    animation-duration: 0.75s;
    animation-iteration-count: 5;
 }

To apply or remove this class using JQuery, you can do the following:

$('#getQuote').click(function() {
    $('.frame').addClass('StartAnimation');
    // Toggle it if a second click should undo the animation
    // $('.frame').toggleClass('StartAnimation');
});

If you still want the hover effect but also need to trigger the animation with a button click, you can use the .trigger() method in JQuery. More information can be found here: http://api.jquery.com/trigger/

$('#getQuote').click(function(){
    $('.frame').trigger('mouseenter');
});

Answer №3

To initiate the animation, add a class and then use the animationend event to remove the added class so the animation can be triggered again.

$('#getQuote').click(function() {
  $('.mouth').addClass('animate');
});
$('.mouth').one('animationend', function(ev) {
  $('.mouth').removeClass('animate');
});
.mouth{
  top: 268px;
  left: 273px;
  position: relative;
  
  width: 100px;
  height: 100px;
  background-color: red;
}

.mouth.animate {
  animation-name: talk;
  animation-duration: 0.75s;
  animation-iteration-count: 5;
}

@keyframes talk {
  0%, 100%{
    top: 268px;
    left: 273px;
  }

  50% {
    top: 308px;
    left: 273px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mouth"></div>
<button id="getQuote">Get Quote</button>

Answer №4

Here's an easy way to do it:

$('.mouth').style({
 '<property>':'<value>',
 '<property>':'<value>',
 '<property>':'<value>'...
});

Answer №5

To implement the talking animation, I recommend using a CSS class like this:

.smile.animated {
    animation-name: chatter;
    animation-duration: 0.75s;
    animation-iteration-count: 5;
}

For the JavaScript portion:

$( "#generateSmile" ).click(function() {
    $(this).prop('disabled', true); // Disabling to prevent animation from being restarted
    $(".smile").addClass("animated");
    setTimeout(function(){
        $( "#generateSmile" ).prop('disabled', false);
        $(".smile").removeClass("animated");
    }, 750*5)
});

Answer №6

My typical approach to this situation is as follows:

//CSS
.animation{
   ......
}

//JavaScript (using jQuery)
var animationTimer;
$('#getQuote').click(function() {
    clearTimeout(animationTimer);
    $('.mouth').addClass('animation');
    animationTimer = setTimeout(function(){
        $('.mouth').removeClass('animation');
    }, 'duration of animation in milliseconds')
});

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

Click twice on the editable AngularJS table to wrap each item

As a new learner of Angularjs, I found an existing code example and decided to customize it. My goal was to enable double-click editing for each li item. However, after adding one more li, the functionality did not work as expected. <li ng-dblclick="st ...

Troubleshooting: Issue with Ajax Post Request in Django Version 1.10.5

For the past week, I've been facing difficulties in successfully executing a jQuery ajax request to transfer data from a JavaScript function within my annualgoals.html file to a Django view for database storage. Currently, I'm just trying to veri ...

Turn off images using Selenium Python

In order to speed up the process, I believe that disabling images, CSS, and JavaScript can help since Webdriver waits for the entire page to load before moving on. from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import Firef ...

The Jquery calculation is giving unexpected results by returning NaN instead of an integer

Hello, I'm attempting to create a calculation that will add up the values from multiple text inputs and then display the total in another text input field. Below is the code that I have put together for this purpose. However, when I test it out, I see ...

Load data into data tables through AJAX request

I'm currently working on implementing datatables and I'm facing an issue with populating my data table using an AJAX call. Here is the snippet of my AJAX call: $('#call_analysis_basic_table').DataTable ({ "ajax": { "url": " ...

Issue: Module '@angular/compiler' not found

After downloading an angular template, I encountered an issue while running "ng serve": Cannot find module '@angular/compiler' Error: Cannot find module '@angular/compiler' ... I tried various solutions found on the internet, incl ...

Can the value in a JavaScript object be updated dynamically when a button is clicked?

In my JavaScript code, there is an object named annualPlan. Whenever a user submits the HTML form for a specific month, I aim to update the value in the object for that particular month accordingly. For instance, if someone submits August 21 and 200, I w ...

Challenges Associated with Promises in JavaScript

I'm having trouble with the last line of code in my program and I need some help figuring out how to fix it. Specifically, I know that the second "then" statement needs to return resolve() but I'm not sure how to go about implementing this. Any t ...

Error in MEAN CRUD operation cannot be determined

{ text: undefined, done: false, _id: 529e16025f5222dc36000002, __v: 0 } PUT /api/todos/529e16025f5222dc36000002 200 142ms - 68b Encountering an issue while updating my CRUD todo list. Despite receiving a successful status code of 200 after submittin ...

Can Vuejs functions be triggered using a jQuery event trigger?

I am currently attempting to trigger a change event within a Vue component. Within the component template, there is a select element. When I try to use jQuery to trigger a change event on this element, the Vue component does not seem to detect it. Here i ...

Display upon hovering, conceal with a button located within a popup container

There seems to be an issue with the code below. Even though it works perfectly in jsfiddle, it breaks in my Chrome and other browsers right after displaying the ".popup" div. Can anyone point out what I might be doing wrong? I found similar code on this si ...

Leaflet Alert: The number of child elements is not the same as the total number of markers

Encountering a problem with Leaflet clustering using v-marker-cluster. Within the icon createFunction of the cluster, the className of children is used to determine the cluster style. Here is a snippet of this function : const childCount = marker_cluster._ ...

Sending AJAX data from VIEW to CONTROLLER in PHP (MVC) using AJAX: A step-by-step guide

I have a page at http://visiting/blog. The Controller contains two methods: action_index and add_index. When Action_index() executes, it returns pages with indexes. On the other hand, Add_index() invokes a model's method called add_data(), which inse ...

What's the method for positioning the footer at the bottom of the grid without impacting the layout of the other rows?

I am trying to achieve the placement of the footer at the bottom of the layout page when the height is shorter than the window. While it is often suggested to use min-height:100vh, this approach ends up increasing the height of other elements as well. I am ...

Hide other dropdown when one dropdown is clicked

I am currently working with a dropdown data-filter in combination with the isotope plugin. My goal is to have the ability to close an open dropdown when another list item is clicked, and also have the arrow twirl down when the dropdown is open. I am seek ...

Unlocking the potential: Clicking on all ng-if elements with matching text using Chrome console

I am currently trying to determine how to automatically click on all elements that have a specific state. The page appears to be built using Angular, although I am unsure of the exact version being used. My approach involves using the console in Chrome t ...

Automatically changing the color of the navigation bar text

I am experiencing an issue with the navigation bar on my webpage. Currently, it is styled with white text against a dark background color using the following CSS code snippet: a{ color: white; text-decoration:none; font-weight:bold; font-s ...

Cannot extract the 'name' property from 'e.target' because it is not defined

I encountered an error message stating that I cannot destructure the property 'name' of 'e.target' because it is undefined within the createform() method. Despite highlighting the line causing the error, I am still unable to comprehend ...

HashId plugin for Angular

Currently, I'm attempting to integrate into my Angular project built on the latest version. After discovering this definition file: // Type definitions for Hashids.js 1.x // Project: https://github.com/ivanakimov/hashids.node.js // Definitions by: ...

What could be causing the jQueryUI dialog to malfunction in IE9?

The code provided above successfully creates a jQueryUI modal popup dialog in various browsers such as Firefox, Chrome, and Opera. However, it encounters issues when running on Internet Explorer 9: <html><head> <link rel="stylesheet" href= ...