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 extract data from a JSON object in JavaScript?

let result = JSON.parse(data.d); The current code doesn't seem to be working.. Here is the structure of my JSON object: [{"ItemId":1,"ItemName":"Sizzler"},{"ItemId":2,"ItemName":"Starter"},{"ItemId":3,"ItemName":"Salad"}] Any suggestions on how I ...

Record client's real-time decision in the database

Is it possible to use jQuery to update a specific cell in phpMyAdmin after adding a div to a particular group? Take a look at the code below: <div id="item1">Item 1 <input type="button" value="put me in Group1" name ...

What is the best way to pass multiple parameters along with the context to a URL in Vuex?

Link to StackBlitz I am currently working on how to send multiple action parameters with context to a URL. I have been encountering a 400 error in my attempts. The selectedPoint and departurePoint are coming from child components and stored as variables i ...

Creating a basic live data visualization chart

Can anyone help me with fetching data from the database and plotting it into a real-time graph? I found an example here: The JSON structure is as follows: "networks": { "eth0": { "rx_bytes": 5338, "rx_dropped": 0, "rx_err ...

Is there a different method for looping in JSX besides .map()?

In my JSX code, I am attempting to display a specific value based on certain conditions. The current code snippet checks if the 'item.result' exists and has a length greater than 0. If so, it maps through the 'item.result' array and dis ...

Trouble with the datepicker

I tried using the datepicker from and followed its demo, but unfortunately, the datepicker is not showing up on my page. I have gone through everything carefully, but I am unable to identify the reason behind this issue. Could someone please assist me i ...

When creating a FlipCard, you may encounter the error message "The object may be null" while using the document.querySelector() method

Having trouble creating a flipcard on Next.js with tsx. The querySelector('.cardflipper') doesn't seem to locate the object and I keep getting this error: "Possibly the object is null". Does anyone know a solution to help my method recognize ...

Having trouble setting the focus on a text box following a custom popup

Presenting a stylish message box and attempting to focus on a textbox afterward, however, it's not functioning as expected. Below is the HTML code: <a class="fancyTrigger" href="#TheFancybox"></a> <hr> <div id="TheFancybox">& ...

Eliminating an element from an array based on a single criterion

Here's a question that might seem simple to some: Let's say I have an array like this... var array = [ {id: 1, item: "something", description: "something something"}, {id: 2, item: "something else", description: "something different" ...

Conceal the .dropdown-backdrop from bootstrap using solely CSS styling techniques

Is there a way to hide the .dropdown-backdrop element from Bootstrap for a specific dropdown on a webpage using only CSS? I found a solution that involves Javascript, you can view it on JSFiddle here. However, I am hoping to achieve this without relying o ...

Automating Internet Explorer using VBA to click a button

I'm currently working on automating tasks on a website using VBA, specifically an online banking system where I am trying to export Transaction History data into a .csv file. Everything seems to be running smoothly until I reach the final Export butto ...

Error 404 encountered while attempting to access dist/js/login in Node.JS bootstrap

I currently have a web application running on my local machine. Within an HTML file, I am referencing a script src as /node_modules/bootstrap/dist/js/bootstrap.js. I have configured a route in my Routes.js file to handle this request and serve the appropri ...

What are the steps to develop a progress bar using PHP and JavaScript?

In the application I'm developing, PHP and JavaScript are being used extensively. One of my tasks involves deleting entries from the database, which is a time-consuming process. To keep the end-user informed, I would like to provide updates on the pr ...

Unable to Retrieve Response from jQuery AJAX in a Function

Having some trouble with a jQuery AJAX function that is supposed to retrieve data from a file. I can't seem to get the function to return the actual value. Any suggestions? $(document).ready(function () { function fetchData() { ...

I am facing an issue where the URL generated in the Controller in Laravel is not functioning properly when

After escaping the single quote, I included a URL link inside the Controller and passed it through json_encode. However, when I clicked on the URL link, it did not work and showed this: The URL appeared like this: http://localhost/BSProject/public/%7B% ...

Triggering the Bootstrap modal multiple times with each increment

I am experiencing an issue with Bootstrap Modal where the action fires up multiple times upon clicking. <div id="loginModal" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> ... & ...

Smooth transition of an arrow moving in a continuous loop using CSS animations

I am attempting to create a seamless loop of a single arrow within a div. My approach involves using two SVG arrows in such a way that when the top part of the first arrow is hidden, the corresponding section of the second arrow should be displayed. This ...

Utilize images inputted via an HTML DOM file uploader within p5.js

I'm facing a challenge with allowing users to upload their image file through a DOM file uploader (<input type="file"></input>). Once the image is uploaded, I'm unsure of how to transfer it to JavaScript and process it using p5.js. Ho ...

Video HTML autoplay feature malfunctioning

I have been attempting to use the <embed> tag in order to showcase a video on my webpage. However, I have encountered an issue where the video begins playing automatically when the page loads. In an effort to remedy this situation, I included autost ...

Update the content of a div and refresh it when a key on the keyboard is pressed

I need to hide the images in a div when I press a key on the keyboard. How can I achieve this? <div> <span role="checkbox" aria-checked="true" tabindex="0"> <img src="checked.gif" role="presentation" alt="" /> ...