Animating SVG elements using CSS

After learning how to use the <animate> attribute, I successfully created a captivating animation featuring an elastic line. However, I am now determined to transform this into CSS for better control: my goal is to activate the animation only upon hovering over the line element. Unfortunately, I am facing difficulties in achieving this.

If you want to see a demonstration of what I have described, please check out this Live Example.

Here you can find my HTML and SVG code:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" class="cntr" id="grid">
      <defs>
        <style>
          path {
            stroke: red;
            stroke-width: .2;
            stroke-linecap: round;
            fill: none;
          }
        </style>
      </defs> 
      <g class="drawing">
        <g class="path-wrap">
          <path d="M 30,40 Q 50,40 70,40">
            <animate
              attributeName="d"
              dur="1.5s"
              begin="1s"
              repeatCount="indefinite"
              values="M 30,40
                      Q 50,40 70,40; 

                      M 30,40
                      Q 50,50 70,40;
                      
                      M 30,40
                      Q 50,35 70,40;
                      
                      M 30,40
                      Q 50,45 70,40;
                      
                      M 30,40
                      Q 50,38 70,40;
                      
                      M 30,40
                      Q 50,42 70,40;
                      
                      M 30,40
                      Q 50,39 70,40;
                      
                      M 30,40
                      Q 50,41 70,40;
                      
                      M 30,40
                      Q 50,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;
                      
                      M 30,40
                      Q 60,40 70,40;
                      
                      M 30,40
                      Q 40,40 70,40;"
              fill="freeze"
            />          
          </path>
        </g>
      </g>
</svg>

Is there a straightforward method for converting <animate> attributes to CSS? Or is this task deemed impossible?

Answer №1

According to @Robert Longson, it has been noted that

Only Chrome supports CSS animation of path d attributes

If you aim to effectively manage SVG animations, you can utilize a robust set of attributes and parameters similar to those in CSS animations. For instance, if you wish to create the start and end points of an animation triggered by hover events, you would need to specify:

begin="grid.mouseover"
end="grid.mouseout"

The animation within the SVG canvas will continue as long as the cursor is hovering over it, but will cease when the cursor moves outside the canvas.

<svg id="grid" xmlns="http://www.w3.org/2000/svg" width="30%" height="30%" viewBox="0 0 100 100" class="cntr" style="border:1px solid gray;">
      <defs>
        <style>
          path {
            stroke: red;
            stroke-width: 1;
            stroke-linecap: round;
            fill: none;
          }
        </style>
      </defs> 
      <g class="drawing">
        <g class="path-wrap">
          <path d="M 30,40 Q 50,40 70,40">
            <animate
              attributeName="d"
              dur="1.5s"
              begin="grid.mouseover"
              end="grid.mouseout"
              repeatCount="indefinite"
              values="M 30,40
                      Q 50,40 70,40; 

                      // additional animated values...

                      M 30,40
                      Q 60,40 70,40;

                      M 30,40
                      Q 40,40 70,40;"
              fill="freeze"
            />          
          </path>
        </g>
      </g>
</svg>

Answer №2

For the "begin" attribute, simply switching it to begin="mouseover" will make it function properly.

                 attributeName="d"
                 dur="1.5s"
                 begin="mouseover"
...

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 utilize overflow with TailwindCSS?

1. The Home Component: import React from "react"; import Title from "./Title"; import TaskInput from "./TaskInput"; import TaskList from "./TaskList"; function Home() { return ( <div className="h-scree ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

Assign the two values to variables in the CSS script

My challenge lies in passing two values to the CSS within the <style> tags. The values are: (background-color: --placeholder; color: --placeholdtext;). I am able to pass either one of the values successfully, but not both at the same time. When I cop ...

What is the reason behind LESS displaying arithmetic operations as text instead of performing them?

Whilst composing the subsequent operations @a: 2px @variable: @a + 5; .margin-style{ margin-left: @variable; } The code above compiles into .margin-style{ margin-left: 2px + 5; } Rather than margin-left:7px; What could be causing this issue? ...

Is there a method to retrieve all Class elements without using a for-loop?

I am trying to retrieve all elements with a specific class using document.getElementsByClassName(); <body> <div class="circle" id="red"></div> <div class="circle" id="blue"></div> <div class="circle" id="yell ...

The div is struggling to contain the image, can anyone assist with this issue?

Greetings all! I am a beginner with CSS and currently working on my website. You can check it out at this link. I am facing an issue where the image on my site is not fitting properly and it keeps repeating. Can anyone guide me on how to fix this using CS ...

Personalizing CSS classes in ASP.NET

In the project I'm currently developing, users will have the ability to customize the font color of header titles. These header titles are styled using values from a cssClass, which includes properties such as font-size, font-color, font-weight, and ...

What is the best way to make the children of a parent div focusable without including the grandchildren divs in the focus?

I want to ensure that only the children of the main div are able to receive focus, not the grandchildren. Here is an example: <div class="parent" > <div class="child1" > <!-- should be focused--> <div class="g ...

The CSS3 Transform feature kicks in only after the window is resized in Mozilla Firefox

I have created a pure CSS3 parallax webpage by following the method outlined by Keith Clark and using a sample made by Carl Henderson (I can't provide a direct link to his codepen due to my lack of reputation). You can find the code in the main page, ...

Positioning a button at the center-right

I'm currently in the process of creating a coming soon website, which you can find hosted here. However, I am having some trouble aligning the button correctly. My goal is to have it positioned to the right of the text field. How can I achieve this? ...

Ways to retrieve a JSON element and incorporate it into a CSS styling

Can someone assist me in converting a JSON object for use in CSS? I've attempted to do this using Vue.js, creating a map legend with v-for to generate the legend. However, if there is an alternative method that allows me to take a JSON object and ins ...

Is there a way to deactivate a clickable div immediately after it has been clicked on?

I have been searching for a solution to this particular question on different platforms, including Stack Overflow. However, I am looking for an answer using pure JavaScript only, so please avoid jQuery solutions. In order to provide context, I have includ ...

What about a Material UI-inspired homepage design?

Looking at the demo image on the main page for Material UI has me really impressed. I am interested in using a similar theme with MUI for my web application. Can you advise me on how I can achieve this look, or do I have to start from scratch? https://i.s ...

What is the best method to determine the accurate height of a window that works across all browsers and platforms?

Is there a way to accurately determine the visible height of the browser window, taking into consideration any floating navigation bars or bottom buttons that may interfere with the actual viewing area? For example, mobile browsers often have floating bar ...

Create a dynamic image positioned alongside text specifically designed for mobile devices

One of my challenges involves implementing a responsive image in Bootstrap (3.3.7): <div class="col-sm-2 text-center"> <img class="img-responsive center-block " src="img/1.png" style="max-width: <p>I would like to include some gua ...

What is the method to make a CSS selector avoid selecting a specific element?

Let's say I have a CSS style that looks like this: input.validation-passed {border: 1px solid #00CC00; color : #000;} The JavaScript validation framework I am using injects every input tag with a class="validation-passed". While this works fine ...

What is the best way to position text below an image icon in a menu?

I am having trouble creating a menu that has the same style as shown in this image: . Unfortunately, my menu ends up looking like this instead: . Can someone please help me fix this issue? Here is the HTML code I am working with: <header> ...

Is there a way to obtain the coordinates of an SVG element by simply clicking on a specific point?

I'm still learning about SVG and I'm trying to trigger an event that captures the click coordinates when clicking on the SVG. I have a few questions: Since I'm using Angular, I'm unsure if it's possible to keep my function in th ...

Ensure that clicking on an element closes any currently visible elements before opening a new element

Take a look at my code snippet below. I am working on creating multiple clickable divs that reveal different content when clicked. However, the issue I am facing is that currently, both content blocks can be displayed simultaneously. My goal is to have onl ...

The Step-by-Step Guide to Adding a Function Style to Wordpress

Typically I would use enqueue in this manner wp_enqueue_style( 'mystyle', get_stylesheet_directory_uri() . '/css/style.css',false,'1.1','all'); but now I have created a custom field and need to enqueue a style that ...