Offsets of elements once the animation is finished

Having issues with CSS animation.

I am trying to get the small plus icon to rotate/spin in the center and stop once it is completed. Currently, the animation works fine but after it finishes, the icon shifts to the bottom and right.

.link {
  position: relative;
  display: block;
  width: 150px;
  height: 150px;
}

.link:after {
  position: absolute;
  top: 0%;
  left: 0%;
  background: blue;
  content: '';
  visibility: hidden;
  height: 100%;
  opacity: 0;
  transition: opacity 280ms ease-in-out;
  width: 100%;
  
}

.link:hover:after {
  visibility: visible;
  opacity: .6;
}

.link:hover:before {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-animation: spin-plus 500ms 1;
  animation: spin-plus 500ms 1;
  color: white;
  content: '\f067';
  display: inline-block;
  font-family: 'FontAwesome';
  font-size: 1.750rem;
  -webkit-transform-origin: 50% 50%;
  transform-origin: 50% 50%;
  z-index: 10;
}

@keyframes spin-plus {
  0% {
    -webkit-transform: translate(-50%, -50%) rotate(0deg);
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    -webkit-transform: translate(-50%, -50%) rotate(180deg);
    transform: translate(-50%, -50%) rotate(180deg);
  }
}
<a href="#" class="link">
  <img src="http://via.placeholder.com/150x150" class="image" />
</a>

Tried various solutions without success. Out of ideas now

Answer №1

One reason for this behavior is that the transform property resets back to its original state after an animation completes. To ensure the desired functionality, you can set a default transform in your CSS.

-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);

.link {
  position: relative;
  display: block;
  width: 150px;
  height: 150px;
}

.link:after {
  position: absolute;
  top: 0%;
  left: 0%;
  background: blue;
  content: '';
  visibility: hidden;
  height: 100%;
  opacity: 0;
  transition: opacity 280ms ease-in-out;
  width: 100%;
  
}

.link:hover:after {
  visibility: visible;
  opacity: .6;
}

.link:hover:before {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-animation: spin-plus 500ms 1;
  animation: spin-plus 500ms 1;
  color: white;
  content: '\f067';
  display: inline-block;
  font-family: 'FontAwesome';
  font-size: 1.750rem;
  -webkit-transform-origin: 50% 50%;
  transform-origin: 50% 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  z-index: 10;
}

@keyframes spin-plus {
  0% {
    -webkit-transform: translate(-50%, -50%) rotate(0deg);
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    -webkit-transform: translate(-50%, -50%) rotate(180deg);
    transform: translate(-50%, -50%) rotate(180deg);
  }
}
<a href="#" class="link">
  <img src="http://via.placeholder.com/150x150" class="image" />
</a>

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

Tips for retrieving formatted text layout from the database

I recently encountered an issue when adding text to my MySQL database using a textarea. The format of the text, including newlines and spaces, was saved in the database but not displayed as such when viewed directly. When I retrieve this string from the da ...

React is encountering a problem with loading the image source and it is

<img src="play.jpg" alt="play"></img> This code works fine in standard HTML, but I'm having trouble getting it to work in React. Is adding it as a background the only solution? ...

Using jQuery to dynamically swap out <tr> tags and all the elements inside of them

In order to enhance user experience, I am looking to automatically fill in certain form elements within a table when a link is clicked. The intention is for the fields to populate with predefined values and then convert into HTML paragraph elements, preven ...

Ways to monitor and measure clicks effectively

Within my website, I have a table element with one column and numerous rows. Each row serves as a hyperlink to an external shared drive. <tr><td ><a href="file://xxx">Staff1</a></td></tr> <tr ><td ><a h ...

Customizing text inputs in three.js

Why are the HTML textboxes disabled for input when the "render" button is working properly? I have tried changing the render.domElement to different elements on the page such as container, info, parent, and scene, but I cannot figure out the reason for t ...

What could be causing the issue with my filter page not being able to retrieve any results from the

I am currently working on a filter page that is designed to display results in a table based on the selection made from a dropdown list. While I have written the necessary code and no errors are being detected, the results from the database are not showing ...

What is the best way to achieve a full width table in an HTML format on a smartphone browser?

Apologies for my limited English proficiency. I am currently working on creating a horizontal scrollable table in HTML. My goal is to make the width of the table span beyond the browser's viewing area, so that sticky cell functionality can be implem ...

Utilizing jQuery to display labels only for selected checkboxes while concealing the ones that are not checked

I currently have the following setup: <style>.selectit{display:none;}</style> <div id="foo"> <label class="selectit"> <input type="checkbox" name="one" id="one" checked> One </label> <label class="selectit"> <i ...

Is it possible for me to create an immersive HTML5 game that utilizes the entire screen

I'm currently designing an extraordinary HTML5 game. Is there a convenient way to offer the user a handy button for effortlessly switching to full-screen mode? This should work seamlessly across all popular browsers, without any compatibility limitat ...

What is the best way to create a border with an inside radius?

Looking to design a div with an inner circle in the corner similar to the image below. Seeking assistance to find a solution for this issue. ...

How to position Bootstrap 4 navbar to the right side

I recently started using the latest version of Bootstrap for a personal project. While working on creating a navbar, I encountered an issue with aligning the links to the right. Despite my efforts to find a solution, I have been unable to figure out how to ...

Use the matrix4() of a different object to alter the properties of another (THREE.js version 84)

My goal is to move a cube based on the movement of another cube that has TransformControls attached using the Matrix4() method exclusively. Unfortunately, all attempts to shift the follower cube have been unsuccessful. It's puzzling why the follower ...

Blank areas on a document

I've recently started learning HTML and CSS, and I've encountered a problem that has me stuck. HTML: <div class = "window"> <form> <input class="button" type="submit" value="Login" /> </form> ...

Height of columns in Bootstrap 4 grid layout using row-cols

I am faced with a challenge while using the bootstrap grid system with the row-col attribute. The issue is that when columns wrap, they do not maintain the height of the tallest columns from the previous rows. Is there a way to ensure that the wrapped colu ...

Refresh the Google Maps location using GPS coordinates

Currently, I am working with an Arduino that has a GPS chip and processing NMEA strings with Python. I have an HTML file set to auto-refresh every x seconds to update the marker's position. However, I would like to update the position information with ...

Discovering the true width of elements that have overflow hidden in IE7 using jQuery

I am dealing with a dynamic list that contains around 50 elements, but the exact number may vary. <div style="width:465px;"> <ul> <li>aaa</li> <li>aaa</li> <li>aaa</li> <li>aaa& ...

Is it possible to dynamically change HTML content by utilizing a JSON file?

Looking to implement a JavaScript loop (using jQuery) that can dynamically populate the HTML file with content from a JSON file based on matching <div> ids to the JSON "id" values. The solution should be scalable and able to handle any number of < ...

Tips for preventing menu flickering caused by overflow during CSS animations

I recently created a vertical menu with an interesting underline effect that appears when hovering over the menu items. Even though the snippet initially failed to execute, I discovered this cool hover effect (taken from here) which I wanted to implement: ...

Tips on assigning html actions/variables to a flask submit button

I am facing an issue with my Flask app where the form submission process takes a while for the backend to process. In order to prevent users from refreshing or resubmitting, I want to display a loading gif during this time. The current HTML code for my fl ...

Preventing the copying and pasting of HTML ruby tags

I am currently using ruby tags to include pinyin in my text. For instance: <p> <ruby>与<rt>yǔ</rt></ruby><ruby>摩<rt>mó</rt></ruby><ruby>拜<rt>bài</rt></ruby><ruby& ...