Steps to invoke a function repeatedly for an animation

I found this code snippet while browsing a forum post about CSS animations. The question asked if it was possible to create a button that would restart the animation when clicked, even if it is in the middle of playing. They specifically requested no jQuery for the solution.


     <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
     <style type="text/css">
      .classname {
       -webkit-animation-name: cssAnimation;
       -webkit-animation-duration:3s;
       -webkit-animation-iteration-count: 1;
      -webkit-animation-timing-function: ease;
       -webkit-animation-fill-mode: forwards;
         }
         @-webkit-keyframes cssAnimation {
         from {
          -webkit-transform: rotate(0deg) scale(1) skew(0deg) 
        translate(100px);
            }
            to {
               -webkit-transform: rotate(0deg) scale(2) skew(0deg) 
           translate(100px);
       }
       }
      </style>
       <script type="text/javascript">
        function ani(){
           document.getElementById('img').className ='classname';
          }
          </script>
        <title>Untitled Document</title>
          </head>

      <body>
        <input name="" type="button" onclick="ani()" />
       <img id="img" src="clogo.png" width="328" height="328" />
        </body>
        </html>

Answer №1

One approach is to delete the existing class and then reapply it:

function updateClass() {
    document.getElementById('picture').className = '';
    document.getElementById('picture').className = 'new-class';
}

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 assign unique IDs to automatically generated buttons in Angular?

Displayed below is a snippet of source code from an IONIC page dedicated to shapes and their information. Each shape on the page has two buttons associated with it: shape-properties-button and material-information-button. Is it possible to assign different ...

Request made to Ajax fetching complete webpage content

I have a page full of random tips at http://www.javaexperience.com/tips To display these tips on other pages of the website, I am using an ajax call to add the content returned by the call to a specific div's HTML. The code for the div is: <div ...

Different tiers of log levels

I am trying to figure out how to log only "INFO" level messages to the console for users, and to a file store "DEBUG" level posts. Currently, I have come across a solution that involves using multiple "getLogger()" functions like so: log4js.getLogger(&ap ...

Get rid of the underlined anchors in your email signature

I'm currently in the process of creating personalized email signatures for one of my clients. At this point, I am testing them on various platforms such as GMail, Hotmail, Brinkster, and more. One issue I am facing is trying to remove the underline t ...

Tips for dividing the WordPress header navigation horizontally using separate PHP files

I am trying to customize my WordPress menu navigation by splitting it into two sections: left and right. I have added the menu function in the "functions.php" file to create the menu from the backend, and the frontend modifications will be done in the "hea ...

Tips for managing the onloadedmetadata event

If a video file cannot be played by the browser due to format or codec issues, I want to notify the user about it. When the browser is unable to play a video (because of unsupported format or codec), the onloadedmetadata event does not occur. I have some ...

Every time you attempt to download in Vue, you are met with the familiar sight

Encountering a peculiar issue while attempting to download an apk file using a Vue.js website: Referring to How to download a locally stored file in VueJS, I am trying to download a local file with this command: <a href="../../app-debug.apk" download= ...

Is it possible to remotely adjust JavaScript configurations on the client's side using JSON?

I have integrated my library into the client's website and need to set it up by providing a remote JSON file specific to the client's ID. What would be the most effective method for achieving this? Using ajax directly may not be ideal as we need ...

What are the best practices for sharing context in express and typescript?

Implementing a solution to expose a value to all request handlers using express and typescript is my goal. I am looking for a way to easily "inject" this value from a middleware or an alternative method that allows for simple mocking if needed. Here is th ...

Trimming Text with JQuery

My goal was to truncate a string and add an ellipsis at the end using jQuery, but my code doesn't seem to be working as intended. Below is the jQuery code I wrote: $(".link-content > h4").each(function(){ var len = $(this).text().length; ...

Traversing Through a Complicated JSON Structure

An application I developed can accept faxes in XML format and convert them into JSON objects to extract the necessary information, specifically the base64 string within the "file contents" variable of the document. Here is the code snippet: exports.recei ...

If I do not specify whether a variable is declared using var or let, what will be its scope?

As someone who is new to JavaScript, please forgive me if my question is not entirely valid. What will be the scope and type (var/let) of a variable if I do not specifically define it as var or let? For example: function f1(){ a="Sample" console.log(" ...

Text Alignment in a Responsive Design

Is there a way to create a responsive container that automatically centers all items inside it on the page, regardless of the screen size? Thanks :) Images are not included in this code snippet, but here's the code: HTML <!DOCTYPE html> <h ...

Add a variety of parameters to the URL in order to make an AJAX request

I want to dynamically add values from checked checkboxes to a URL for use in an AJAX call. I only want these values to be added to the URL if the checkbox is checked. If a user checks and then unchecks, I do not want that value included in the URL. Below ...

Listen for the load event during an AJAX request without using jQuery's add

I have four HTML files and four corresponding JavaScript files. Each JavaScript file is externally loaded by its respective HTML file. Specifically, index.html loads javascript.js, 1.html loads javascript1.js, 2.html loads javascript2.js, and 3.html loads ...

Having trouble altering the error response code in feathers.js

I'm utilizing an authentication service for login validation. In this case, I am looking to change the Unauthorized (401) response code to 200 while keeping the message the same. The authentication service setup is as follows: app.service('auth ...

PHP's 'include' function is now being ported into modern Javascript as a new method

As the library of JS frameworks continues to expand, I'm wondering if there is a simple JS replacement or alternative for PHP's 'include' function. Is PHP include still a relevant method for including chunks of code, or are there better ...

What causes the picturesArray to remain consistently void?

const fetch = require("node-fetch"); let images = []; fetch('http://www.vorohome.com//images/assets/159314_887955.png') .then(response => response.buffer()) .then(buffer => { const data = "data:" + response.headers.get ...

Express 4 : In order to pass a variable from app.js to my routes, I have it configured and ready to be sent over

Recently, I made some updates to my app.js file using Express generator. Below is a snippet of the changes: app.js var express = require('express'); var socket_io = require( "socket.io" ); var app = express(); // Socket.io var io = socket_io( ...

Tips for resolving the UNABLE_TO_GET_ISSUER_CERT_LOCALLY issue while attempting to install Sentry using npm or curl

https://i.stack.imgur.com/G8hfZ.png curl -sL -k https://sentry.io/get-cli/ | bash Even though I've specified not to verify the certificate with -k, I'm still facing issues trying to install it. The script is supposed to automatically install sen ...