Rotating SVG graphics with a growth effect

Check out my CSS below:

/* --------------------------
  Base
--------------------------- */

body {
  padding-top: 60px;
    background: #0f4e7a;
}

svg {
    margin: auto;
  display: block;
    width: 28%;
}

.star{
  fill: #FFF;
}

/* --------------------------
  Keyframes
--------------------------- */

@keyframes grow2 {
    0% {
        transform: scale(0.5);
    opacity:0;
    }
    50% {
        transform: scale(1.5);
        opacity: 0.5;

    }
    100% {
        transform: scale(1);
    opacity:1;
    fill: #fdf097;
    }
}


@keyframes rotate2 {
    0% {
        transform: rotate(0deg);

    }
    50% {
        transform: rotate(180deg);


    }
    100% {
        transform: rotate(360deg);
    }
}

/* --------------------------
  SVG Styles
--------------------------- */
.star * {
    transform-origin: 50% 50%;
}




.star {
    animation: grow2 1s forwards, rotate2 1s forwards;
  transform-origin: center;
}

.star:hover {
    animation:  rotate2 1s ease-in forwards;
  transform-origin: center;
}

Take a look at the HTML snippet:

<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="218.8 226.8 258.4 258.3">
        <clippath id="bg">
            <circle cx="348" cy="356" r="129.2"/>
        </clippath>
        <g class="stars" clip-path="url(#bg)">
            <circle class="stars-bg" fill="#3079AB" stroke="#95C7E8" stroke-width="6" stroke-linecap="round" cx="348" cy="356" r="129.2"/>
            <path  class="star"  d="M409.7,337.4c-1.2-1-2.2-1.2-8.4-1c-3,0-6.9,0.2-11.9,0.5c-5.7,0.2-11.4,0.7-14.6,1
                c-1.2-2.5-3.7-6.9-6.4-11.6c-6.9-11.6-9.9-15.3-11.6-17.1c-1.5-2-3.2-1.7-3.7-1.7h-0.2h-0.2c-0.5,0-2.5,0.5-3.2,2.7
                c-1.2,2-3,6.4-6.2,19.5c-1.2,5.2-2.2,10.1-3...
                

I'm struggling to get the rotate and grow animations to work together properly, and when hovering over the element it seems to do the opposite of what I expect by growing instead of rotating. Any ideas on how to fix this issue would be greatly appreciated!

Answer №1

The issue arises when the animation on hover overrides the original animation, causing it to reset.

To solve this, you can merge both the grow and rotate animations into a single @keyframes rule that is utilized in the normal state.

Then, during hover, switch the new animation (in this instance, growAndRotate) to have `0` iterations and only apply the rotation animation.


Snippet of Code:

/* --------------------------
  Base
--------------------------- */

body {
  padding-top: 60px;
  background: #0f4e7a;
}
svg {
  margin: auto;
  display: block;
  width: 28%;
}
.star {
  fill: #FFF;
}
/* --------------------------
  Keyframes
--------------------------- */

@keyframes rotate2 {
  0% {
    transform: rotate(0deg);
  }
  50% {
    transform: rotate(180deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes growAndRotate {
  0% {
    opacity: 0;
    transform: scale(0.5) rotate(0deg);
  }
  50% {
    opacity: 0.5;
    transform: scale(1.5) rotate(180deg);
  }
  100% {
    opacity: 1;
    fill: #fdf097;
    transform: scale(1) rotate(360deg);
  }
}
/* --------------------------
  SVG Styles
--------------------------- */

.star * {
  transform-origin: 50% 50%;
}
.star {
  animation: growAndRotate 1s forwards;
  transform-origin: center;
}
.star:hover {
  animation: growAndRotate 0, rotate2 1s ease-in forwards;
  transform-origin: center;
}
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="218.8 226.8 258.4 258.3">
  <clippath id="bg">
    <circle cx="348" cy="356" r="129.2" />
  </clippath>
  <g class="stars" clip-path="url(#bg)">
    <circle class="stars-bg" fill="#3079AB" stroke="#95C7E8" stroke-width="6" stroke-linecap="round" cx="348" cy="356" r="129.2" />
    <path class="star" d="M409.7,337.4c-1.2-1-2.2-1.2-8.4-1c-3,0-6.9,0.2-11.9,0.5c-5.7,0.2-11.4,0.7-14.6,1
                c-1.2-2.5-3.7-6.9-6.4-11.6c-6.9-11.6-9.9-15.3-11.6-17.1c-1.5-2-3.2-1.7-3.7-1.7h-0.2h-0.2c-0.5,0-2.5,0.5-3.2,2.7
                c-1.2,2-3,6.4-6.2,19.5c-1.2,5.2-2.2,10.1-3...

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

Is an audio player/playlist necessary for showcasing a mix engineer's skills in their

As a newcomer to the world of web development with some background knowledge from school, I work as a mix engineer and have created a portfolio website. Previously, I utilized Soundcloud and Spotify's API to showcase my mixes/songs, but the external J ...

Display a video modal upon page load, allowing users the option to click a button to reopen the modal

Looking for a way to make a video modal automatically open on page load and allow users to reopen it by clicking a button? Here's a snippet of code that might help you achieve that: HTML <html> <head> <link rel="stylesheet ...

Is the text in the React chat application too lengthy causing a bug problem?

In my chat application built with React, I am facing an issue where if a user types more than 100 characters, the message gets cut off. How can I fix this problem? Please refer to the image below for reference. {Object.keys(messages).map((keyName) => ...

Resolve the issue of text overflow in PrimeNG Turbo Table cells

When utilizing Primeng table and enabling the scrollable feature, the table is expected to display a scrollbar while ensuring that the text within the cells does not overflow. Unfortunately, this expected behavior is not occurring. To see an example of th ...

Expand the <div> to match the complete height of its containing <div> element

There are multiple div blocks within a parent block: one for the menu on the left, and one for the text on the right. How can you make the right inner block stretch to the full height of the parent block like the left one? The parent block has a minimum he ...

Ways to include scrolling specifically for one template

As a newcomer to frontend development, I have recently started learning Vue and the Quasar Framework. I am currently working on a table and trying to set a fixed table name with only the table items scrollable. <template> <q-table virt ...

Using Jquery and CSS to create a sleek animation for opening a div box container

Designed an animation to reveal a div container box, but it did not meet the desired criteria. Expectations: Initiate animation horizontally from 0% to 100%. Then expand vertically to 100% height. The horizontal animation from 0% to 100% is not visible ...

Remove the scrollbar from the iframe and instead display a scrollbar on the page in its place

Currently, I am facing an issue with the vertical scrollbar on an iframe. I want to remove it so that the scrollbar appears on the main page instead. This way, I will be able to scroll the content of the iframe using the page's scrollbar. I managed to ...

Execute a task prior to invoking a different task

I'm currently working on an image slider that has some interactive features. Here's how it functions: When a user clicks on an image, it slides and comes to the front. On the next click, the image is flipped and enlarged. Then, on another click ...

Difficulty with alignment in the process of generating a vertical stepper using HTML and CSS

I'm currently working on developing a vertical stepper component using html & css with inspiration from this design. However, I've encountered some alignment issues in the css that I'm struggling to resolve. CSS .steps-container .step::b ...

Achieve the appearance of table-like alignment without the need for traditional tables

Any ideas on how to achieve the following layout: <table border="0"> <tbody> <tr> <td style="margin-right:5px;"> <h2 id="optiona" class="option optiona optiona2">a. aa </h2> </td> ...

Steps to center the search form in the navbar and make it expand in Bootstrap 5

I'm utilizing Bootstrap v5.1.3 and have included a navbar as shown below: navbar Here is the code for that implementation: <div class="container"> <nav class="navbar navbar-expand-lg navbar-light bg-light& ...

Only the initial external CSS ID is functional, while the second one is inactive

I am facing an issue with my table where I have applied an external CSS that uses alternating shaded rows. My aim is to have specific table cells with different background and font colors - one cell with a green background and red font, and another with a ...

Empty HTML elements

Is there a way to create empty HTML tags, meaning tags that have no predefined styling or effect on the enclosed content or its environment? For example, <p> creates a paragraph, <b> makes text bold, and <div> forms a container. I am in ...

What is the reason behind the vanishing of the input field while adjusting the

Why is the input field getting cut off when I resize my browser window, even though I'm using percentages? Here is a screenshot for reference: Shouldn't it resize automatically with percentages? Thank you! Below is my CSS code: body { margi ...

The jQuery function is returning an inaccurate value for the height of

In my Cocoa (Mac) application, I am utilizing a Webview and trying to determine the accurate height of a document. While the typical method webview.mainFrame.frameView.documentView.bounds.size.height works well in most cases, I encountered an issue with on ...

Customizing Background Color of Bottom Tab Content Container in React Native

Hey there, I need some help with changing the color of the bottom tabs screens. They are currently gray by default (as shown in the image) and I want them to be white. I've tried using tabBarStyle but it doesn't seem to work. If anyone has any i ...

How to remove previous and next buttons in product carousel using Bootstrap 3.3.5

Currently, I am tackling a small project and venturing into the world of bootstrap for the first time. I have hit a roadblock when it comes to disabling (not hiding) product carousel controls when the active state is on the item:first & item:last classes. ...

How can I align the text in a dropdown menu to the center on Safari?

How can I center the option text in a drop down menu using CSS? display: block; height: 60px; margin-left: 10%; margin-right: 10%; margin-top: 10px; min-height: 60px; text-align: center; The option text is centered in Firefox browser, but not in Safari. ...

What could be the reason that my submenus are not appearing as expected?

I'm currently working on refining the navigation bar design for a website, but I've encountered a problem with the submenu functionality. It appears that only one submenu is visible while the others remain hidden. For example, when navigating to ...