What is the best way to incorporate a button for toggling CSS animations?

I need help adding a button to toggle the animation on this JSFiddle. I tried adding the code below but can't seem to start and stop the animation.

body .a [class^="b"] {
      width: 200px;
      height: 142.8571428571px;
      margin: 0 auto;
      position: relative;
      left: 71.4285714286px;
      -webkit-transform-origin: bottom left;
      transform-origin: bottom left;
      border-radius: 100% 300%;
      background: radial-gradient(bottom left, transparent 41.4285714286px, #ff0c0c 41.4285714286px, #3f0000 -15px, #ff0c0c 71.4285714286px);
      /*  -webkit-box-shadow: 71.4285714286px 71.4285714286px 142.8571428571px darkred;*/
      box-shadow: 71.4285714286px 71.4285714286px 142.8571428571px rgb(164, 69, 14);
      -webkit-animation-duration: 5s;
      animation-duration: 0.5s;
      -webkit-animation-delay: 1s;
      animation-delay: 1s;
      -webkit-animation-timing-function: linear;
      animation-timing-function: linear;
      -webkit-animation-iteration-count: infinite;
      animation-iteration-count: infinite;
    }
    

Answer №1

Explore the demo here: https://jsfiddle.net/xianshenglu/bjwhm3bf/3/

To begin, I extracted this code into an HTML format and altered its value to "stop":

<input type="button" id="button" value="stop"></input>

Next, I applied CSS styling to the button:

body #button {
  z-index:1;
  position:absolute;
  left:0;
  top:0;
}

Then, I introduced the following JavaScript code:

document.getElementById('button').addEventListener('click', function() {
  var animationPlayStateEle = document.querySelectorAll('.a [class^="b"]');

  if (this.value === 'start') {
      Array.from(animationPlayStateEle, function(ele) {
          ele.style.animationPlayState = 'running';
      });
      this.value = 'stop';
  } else if (this.value === 'stop') {
      Array.from(animationPlayStateEle, function(ele) {
          ele.style.animationPlayState = 'paused';
      });
      this.value = 'start';
  }

}, false);

Upon completion, the task is done; The key takeaway is that when you click the button and its value is 'stop', I modify the following:

.a [class^="b"] {
    animation-play-state: play;
}

to

.a [class^="b"] {
    animation-play-state: paused;
}

within the JavaScript code by adjusting the element's style; Similarly, when you click the button and its value is 'start', I update:

.a [class^="b"] {
    animation-play-state: paused;
}

to

.a [class^="b"] {
    animation-play-state: running;
}

Comprehend?

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 display values from a Localstorage array in a tabular format using a looping structure

I have set up a local storage key 'fsubs' to store form submissions as an array. Here is how I am doing it: var fsubs = JSON.parse(localStorage.getItem('fsubs') || "[]"); var fcodes = {"barcodeno" : this.form.value.barcode, "reelno" : ...

Validation is not being applied to the radio button

Link: Issue with Form I've encountered a problem with my script that is meant to validate and submit a form, similar to this working example: Form in Working Order Unfortunately, the script is not functioning as intended on the first link. The Java ...

What is the best way to retrieve and save the titles of checked boxes in the Autocomplete using state with hooks?

I've implemented the React Material-UI Autocomplete feature with checkboxes in my project. Here is what I have so far in my code (check out demo.js for details): https://codesandbox.io/s/material-demo-rxbhz?fontsize=14&hidenavigation=1&theme=d ...

Customize the delete icon margin styles in Material-UI Chip component

Currently, I have implemented the V4 MUI Chip component with a small size and outlined variant, complete with a specified deleteIcon. However, I am encountering difficulties when attempting to override the margin-right property of the deleteIcon due to MUI ...

Utilizing jQuery to make AJAX requests to multiple URLs

I am experiencing some difficulty running an Ajax call due to complications in locating the script.php file within my folder. This issue is a result of the RewriteRule in my htaccess file, which changes the formatting of URLs from "domain.com/index.php?bla ...

Using ngFor results in duplicate instances of ng-template

I'm facing a challenge with the ngFor directive and I'm struggling to find a solution: <ng-container *ngIf="user.images.length > 0"> <div *ngFor="let image of images"> <img *ngIf="i ...

Do you know where I can locate the HTML and CSS pertaining to the search results page

I'm looking for a way to present search results on my website that resembles the Google search results, creating a familiar interface for users. Is there anyone who knows where I can obtain the HTML and CSS code that produces the same style as Google& ...

How can you implement automatic suggestion based on the selected country by utilizing the Google API?

Is it possible to generate a list of states based on the country selected? Below is a sample code snippet: $("#state").keypress(function(){ var input = document.getElementById('state'); //var c1=$("#country").val(); ...

Can someone suggest a method for deciphering hexadecimal code used in JavaScript?

How can I gain an understanding of this code and convert it into simple javascript? Can someone assist me in deciphering the code or transforming it back to its original script? If someone obfuscated the javascript, what type of method should we use to de ...

Acquiring the specific checkbox value using JQuery

I am encountering an issue with my JQuery function that is supposed to print out the unique value assigned to each checkbox when clicked. Instead of displaying the correct values, it only prints out '1'. Each checkbox is assigned an item.id based ...

Sliding off the canvas - concealed navigation

I have implemented CSS to hide a menu on mobile: #filter-column { position:absolute; left:-400px; } However, I want the menu to slide in from the left when the user clicks a link, and everything else should be hidden. When the layer is closed, th ...

What is the reason for Jquery AJAX appending additional path to the relative path?

I am encountering an issue with the following code snippet $.ajax({ url: "search/prefetch", success: function (data) { $(".result").html(data); alert("Load was performed."); }, dataType: "json" } ...

What adjustments need to be made on either the client-side or server-side in order to enable the functionality of getJSON()?

I recently encountered a challenge while working with web services that are hosted on a different domain from the site I am developing. This led me to explore calling these services using ajax, which introduced me to the complexities of the same-origin pol ...

Example of expanding comment fields in MySql by querying JSON data

I am in the process of designing a webpage for collecting comments from users. The structure will allow for an initial comment, followed by replies to that comment, and further replies to those replies, continuing infinitely. Due to this nested nature of ...

Ways to retrieve a specific item from a constantly changing knockout observableArray without employing a foreach loop

Why can I only access one property ('member_A') of an Element in an observableArray using an HTML <input>? I am attempting to add a new object of type abc() to the observableArray "list_of_abc" when the button "ADD To List of abc" is click ...

What is the method for embedding a concealed form within a table?

I am a beginner in HTML and RoR. I am trying to include a button inside a table that will submit a hidden form. However, I am facing an issue where the button is not showing up in the generated HTML. <table class="centerBox"> <h3>Search Resu ...

Struggling with configuring a 'post' endpoint in an express server problem

My goal is to validate that my client is able to successfully post information to its server. I have configured a specific 'route' on my Express server for this purpose. // server.js this is the server for the PvdEnroll application. // var ex ...

AngularJS Expression Manipulation

I am looking to implement a functionality similar to the one below: HTML <mydir> {{config = {type: 'X', options: 'y'}} </mydir> <mydir> {{config = {type: 'a', options: 'b'}} </mydir> JS Dir ...

The linking process in AngularJS is encountering difficulties when trying to interact with

I've already looked at similar questions, and my code seems correct based on the answers provided. It's a very simple beginner code. <html ng-app=""> <head> <title>Assignment</title> <script src=" ...

The code encountered an error because it was unable to access the property 'style' of an undefined element on line 13 of the script

Why is it not recognizing styles and showing an error? All paths seem correct, styles and scripts are connected, but it's either not reading them at all (styles) or displaying an error. Here is the html, javascript, css code. How can this error be fix ...