Halt a CSS spin animation gracefully

In order to create a smooth rotation effect for an image in my HTML page, I've created a CSS class named 'rotate' that rotates the image indefinitely. Here is the code:

.rotate{
    animation: rotation 2s infinite linear;
}

@keyframes rotation {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(359deg);
    }
}

However, I now need to stop the rotation smoothly at a specific keyframe, but I'm not sure how to achieve this with another CSS class. Can someone help me out?

.rotateToStop{
    ...
}

I plan to add the 'rotate' class initially and then switch to 'rotateToStop' when a particular event is triggered in my JavaScript code:

$('myelement').addClass('rotate');
// ... 
// Shortly after the event is triggered
$('myelement').removeClass('rotate');
$('myelement').addClass('rotateToStop');

If anyone has any suggestions on how to implement the 'rotateToStop' class in CSS, please let me know! =)

Answer №1

Unfortunately, simply removing the animation class from an element will not smoothly stop the animation.

Instead, you can halt the animation with this solution:

.rotateToStop {
  animation-play-state: paused; 
}

In order to achieve this, you must toggle a class that initiates or pauses the animation without removing the initial .rotate class.

However, one issue to note is that the animation will pause at the exact instant the necessary class is added to the element.

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

Exploring options beyond ng-model

When attempting to apply ng-model on a textarea, I encountered an error in IE 11. Here is the code snippet: <div ng-hide="DoctorDashboardCtrl.showEducation"> <textarea placeholder="2000 Characters Free flow text" width="832px" height="100px ...

What type of technology is typically utilized in creating functional platforms such as goDaddy, wix, and other web design websites?

I am currently working on a web development project that aims to allow users to edit webpages without needing any coding knowledge. Users with authorization will be able to modify not only the text but also various HTML tags on the page, similar to platfor ...

Turn off transition animations

How do I turn off animation in certain cases? https://jsfiddle.net/4b3nxv7n/ <div id="flip-list-demo" class="demo" :class="{'animate': animate}"> <button v-on:click="shuffle">Shuffle</button> <input type="checkbox" v-mo ...

Deactivate Date Field for Editing Orders in WooCommerce

Is there a way to deactivate the Date Created textbox on the Edit Orders page of Woocommerce? I attempted to use pointer-events: none; but unfortunately, it did not have any effect. I am seeking a method to disable the Date Created field. https://i.sstat ...

Warning: It is important for each child in a list to have a unique "key" prop in the render() function of React

After calling an API endpoint in my app.js, I save the data to a state and then render it. The data displays in the browser, but there's a warning on the console saying: Warning: Each child in a list should have a unique "key" prop.. Here's my a ...

Using Javascript's Array.Filter function to adjust the original array

I'm interested in utilizing Javascript's array.filter method to remove specific items from an array because of its elegant and readable syntax. However, I've noticed that instead of modifying the original array, filter simply returns a new a ...

The code written inside the <script> tag seems to be malfunctioning when placed in the component.html file within VScode while working with

I'm facing an issue where the script tag for jQuery included in index.html is not being executed within the component.html. There are no errors detected when inspected. Additionally, I have used another script for a toast message box. <script sr ...

Dynamic sliding effect in CSS for seamless showing and hiding of div elements

I stumbled upon a fantastic solution in these forums How to create sliding DIV on click? However, what I really wanted was for the content to fade in and out with just a click of a button. Here is the code snippet I am currently working with: <html> ...

Is it possible to indicate which mat-tab has been clicked? Especially when the mat-tabs are being generated dynamically

In my UI, I have a dynamic display of mat-tabs that change in number and data based on values from the backend. These tabs are generated dynamically, and when one is clicked, an ID is passed to a function for processing. I want to be able to identify the s ...

Revise every Express.js model

I am looking to implement a put request in Express to update all the models in my collection. Consider this example of a MongoDB collection: { '_id': '123' 'name' : 'john', 'last' : 'smith ...

Transferring information to a MySQL database via an AJAX POST operation

I'm trying to use AJAX to send a user-typed string and add it to a database: New manufacturer: <input type="text" id="new_mfg" size="20"> <button id="add_mfg">Add</button> <script> $("#add_mfg").click(function(e){ ...

Styling your Vuetify V-data-table with CSS

Struggling to add extra spacing between table rows, despite inspecting the page and verifying that my styles are not being overridden. Have read other Vuetify posts on applying CSS but still no luck. Any suggestions for why I can't style my table as d ...

The specified variable will not be visible in the window object

I recently created a JavaScript code snippet that looks like this: let var1 = 1; window.var2 = 2; After running the code in the Chrome console, I entered window to inspect the global window object. Surprisingly, only the second variable appeared and the ...

Adjusting the minimum value on a textfield with JQuery Validate plugin in real-time

I am attempting to dynamically update the minimum value on one field based on input from other fields. Here is a brief overview of my code: $("#new_project").on("click", function() { switch($('input:radio[name=quality-level]:checked').val() ...

Choose the text by clicking on a button

I currently have: <span class="description">Description 1</span> <button class="select">Select!</button> <span class="description">Description 2</span> <button class="select">Select!</button> <span clas ...

Concealing the rear navigation button within the material carousel

I have a material css carousel, and I am trying to hide the back button on the first slide. I attempted to use the code below from a previous post The following code snippet prevents the user from looping through the carousel indefinitely. Stop looping i ...

Printing Strings in JavaScript IF Conditional Block

Hello there! I am looking for assistance with a JavaScript concept. Recently, I created some code where in the HTML file, there is a div element with an id of "GetID" where the string outputs are meant to be displayed. The JavaScript code looks something ...

Using JQuery, you can make a $.get or $.ajax request to a PHP script on the server that needs to receive

A PHP function called functionexchangeRate($exchangeFrom, $exchangeTo) has been created with two parameters in mind. However, I am currently struggling to properly call this PHP script using Jquery's $.get function because I can't seem to figure ...

What is the approach for for loops to handle non-iterable streams in JavaScript?

In the realm of node programming, we have the ability to generate a read stream for a file by utilizing createReadStream. Following this, we can leverage readline.createInterface to create a new stream that emits data line by line. const fileStream = fs.cr ...

How can I create a web control or jQuery function that showcases images retrieved from a database?

Looking to create a dynamic product sales page that showcases product images along with their prices and names pulled from a SQL Server database. When a user clicks on a specific image, they should be directed to a new page with more images and details of ...