What is the best way to elegantly finish a live CSS animation when hovering?

Currently, I am working on a planet orbit code where I want to enhance the animation speed upon hover. The goal is for the animation to complete one final cycle at the new speed and then come to a stop. I have been successful in increasing the speed on hover by applying a new animation to a parent class. However, I am struggling to figure out how to make it stop after a single cycle has finished and return to its initial position. The play-state property did not give the desired effect as it was too abrupt. Additionally, setting a new iteration-count directly also didn't work. I attempted using JavaScript but encountered some challenges. Any assistance would be greatly appreciated.

.wrapper{
    position:absolute;
    top:40%;
    left:50%;
    margin-left:-125px;
    margin-top:-65px;
    height:250px;
    width: 250px;
    animation:orbit 2s linear;  
    animation-iteration-count: infinite;
   animation-play-state: paused;
 
    
}

#orbit {
    height:250px;
    width: 250px;   
    z-index:1;
    border: 1px solid #989898;
    border-radius: 225px;
    animation:orbit 14s linear infinite;
  transform-origin:center center;
}
#planet {
    position:absolute;
    top:5%;
    left:5%;
    height: 50px;
    width: 50px;
    z-index:2;
    transform-origin:center center;
    background-color: orange;
  border-radius: 100%;
}
.wrapper:hover{
    animation-play-state: running;
}

@keyframes orbit {
    100% {
        transform:rotate(360deg);
    }
}
<div class="wrapper">
    <div id="orbit">
        <div id="planet"> 
        </div>
    </div>
</div>

Answer №1

To achieve a single orbit at a different speed when hovering over an element, simply update the animation-iteration-count in your :hover class. After that, you will need to implement an event listener for when the mouse exits the designated wrapper, which will halt the animation.

document.querySelector(".wrapper").addEventListener("mouseout", function(){
  // Adjust the wrapper to animate only once more
  this.style.animationIterationCount = "1";
    
  // Stop the animation on the orbit
  document.getElementById("orbit").style.animation = "none";
});
.wrapper{
    position:absolute;
    top:40%;
    left:50%;
    margin-left:-125px;
    margin-top:-65px;
    height:250px;
    width: 250px;
    animation:orbit 2s linear;  
    animation-iteration-count: infinite;
    animation-play-state: paused;   
}

#orbit {
    height:250px;
    width: 250px;   
    z-index:1;
    border: 1px solid #989898;
    border-radius: 225px;
    animation:orbit 14s linear infinite;
  transform-origin:center center;
}
#planet {
    position:absolute;
    top:5%;
    left:5%;
    height: 50px;
    width: 50px;
    z-index:2;
    transform-origin:center center;
    background-color: orange;
  border-radius: 100%;
}
.wrapper:hover{
    animation-play-state: running;
    animation-iteration-count: 1; /* Only overwrite the iteration count */
}

@keyframes orbit {
    100% {
        transform:rotate(360deg);
    }
}
<div class="wrapper">
    <div id="orbit">
        <div id="planet"> 
        </div>
    </div>
</div>

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

Focus on a separate webpage to access the Bootstrap accordion

Looking for a way to link to specific options within a Bootstrap Accordion from another page? It's possible to automatically open the correct option by including a hash tag in the href link. Here is an example of how to do it: <div id="accordi ...

NG6002 error: This error is showing up in the imports of AppModule, even though it has its own set of issues

Running Angular 12 locally is smooth with no errors in the project build. Local Configuration: Angular CLI: 12.0.5 Node: 12.16.3 Package Manager: npm 6.14.4 OS: win32 x64 Angular: 12.0.5 However, when attempting to build the project on a Linux se ...

How to locate the nearest parent link containing a specific attribute using jQuery

I am trying to locate the nearest link with the attribute findme from a child element .startpoint. Here is the structure: <ul> <li><a href="#">Point one</a></li> <li><a href="#" data-findme="yipi">Point tw ...

Anticipating the arrival of an external JavaScript file in the child component

I am facing an issue with my parent component that dynamically loads an external js file (similar to what is explained here). The child component needs a variable inside the js file, but it throws an error every time the page is loaded because the child c ...

How does the Express server collaborate with Webpack middlewares to facilitate live reloading?

As I delve into node, express, and webpack, I find myself grappling with the concept of middleware. Upon examining the code snippet below, my current understanding is that once the web server is up and running and I navigate to http://localhost:7770/, the ...

Pictures may become distorted when their width is smaller than the container

In my current project, I am facing an issue with maintaining the aspect ratios of images of different sizes. Most of them are looking good except for those whose width is smaller than the container's width of 285px. Even though some blurriness is acce ...

Encountering issues when trying to combine Sequelize with TypeScript

As I attempted to transition my NodeJs application to TypeScript, I encountered issues with Sequelize. Upon attempting to implement the code from a website, an error occurred: This expression is not constructable. Type 'typeof import("/home/de ...

Bypass domain restrictions on JavaScript in Firefox version 7.0

As I work on a personal HTML + JS app, the ability to access the content of iframes or opened windows with Javascript would be extremely beneficial in boosting my productivity. I am curious if there is a way to bypass the cross domain restrictions set by ...

Page Not Found: React and Express Route Not Found

I encountered an error while attempting to use the sign-up route: POST http://localhost:3001/sign-up 404 (Not Found). I searched for similar issues on StackOverflow but couldn't pinpoint the source of my problem... In the frontend, I am fetching the ...

What could be causing this ajax function to fail?

I referred to a solution in response to this question: My objective is to execute a query on the database when a user clicks on a specific table row, enabling me to retrieve and display their information. Below is the piece of code I am using: AJAX: $( ...

Exploring the process of setting up a datasource for Select2 in October CMS using the integrated Ajax Framework

Looking to utilize the extensive AJAX framework provided by October CMS to populate a Select2 box with data. The process of using a remote dataset in Select2 involves the following steps: $(".js-data-example-ajax").select2({ ajax: { url: "https://a ...

Implement a hover effect on a specific class targeting a nested element using JavaScript

Is there a method to apply a class that modifies rules for a child element using Javascript? I have successfully added it with CSS but am struggling to do the same with Javascript. Removing the class in JS is easy by referencing the classname before the ho ...

Having trouble getting gulp set up and running with npm?

I am currently in the process of setting up gulp using npm in order to execute my project. Based on my understanding, all I need to do is enter "npm install gulp" in the command line at the location of my project like this : https://i.stack.imgur.com/hPU ...

Completely different method for transmitting an array to NodeJS through AJAX

Recently, I've encountered difficulties when sending arrays to NodeJS using AJAX. It seems that whenever I try to send it with JSON, the error function is always triggered. Despite seeking explanations, I haven't found a satisfactory answer. The ...

What about a sliding element feature?

Is there a popular slider component that many startups are using, or are these types of sliders typically built from scratch? It seems like they are everywhere on startup websites. I would appreciate it if someone could point me in the right direction with ...

Creating a CSS animation to mimic the fading in and out effect of the Mac scrollbar when scrolling begins

My journey begins with this: *::-webkit-scrollbar { } *::-webkit-scrollbar-button { } *::-webkit-scrollbar-track { } *::-webkit-scrollbar-track-piece { } *::-webkit-scrollbar-thumb:active { width: 6px; background-color: red; } *::-webkit-scr ...

Tips for extracting a specific string from a JSON object

I am working with a JSON object { "data": [{ "user_id":"1", "user_name":"test", "user_phone":"2147483647", "user_email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4c0d1c7c0f4d1ccd5d9c4 ...

Struggling to align a div in the middle using react and tailwindcss

Struggling to center a div in React with Tailwind, no matter what method I try. Here is my code: "use client"; import Image from "next/image"; import React from "react"; const LoginPage = () => { return ( <div cla ...

Attempting to display numerous react components on various divs instead of just one ID

Currently, I am facing a challenge in creating multiple react comment boxes. The package I am using is based on #id instead of class, which resulted in only rendering one box instead of all. To resolve this issue, I attempted to switch from GetelemntbyID t ...

Persistent error caused by unresponsive Tailwind utility functions

Currently, I am working on a Next.js application and encountered a strange error while adjusting the styling. The error message points to a module that seems to be missing... User Import trace for requested module: ./src/app/globals.css GET /portraits 500 ...