Icon rotation returns to original position once animation finishes

I am trying to add animation to my icon so that it rotates 45 degrees when clicked, and then returns to its original position when clicked again. I have successfully achieved the initial rotation upon clicking, but after the animation finishes, it snaps back to its original state without another click.

Snippet of HTML Code

<div class="trip-detail--icon-plus">
  <span class="transport--plus spin" ng-class="{active:showActionOptions}"></span>
</div>

Snippet of CSS Code

.spin::before {
  -moz-transition: transform 1s ease;
  -webkit-transition: transform 1s ease;
  -o-transition: transform 1s ease;
  transition: transform 1s ease;
}


.spin.active::before {
  display: inline-block;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

Answer №1

To achieve what you're attempting, a CSS hack would be necessary to simulate a click event.
For more information, refer to CSS Click events

You can also view my solution on JSFiddle

<input type="checkbox" id="toggle-1">
<label for="toggle-1">
  <div>Control me</div>
</label>

Below is the CSS code snippet:

/* Checkbox Hack */
input[type=checkbox] {   
  position: absolute;   
  top: -9999px;   
  left: -9999px;
}

label {   
  display: inline-block;  
  margin: 60px 0 10px 0;  
  cursor: pointer;
}

/* Default State */
div {   
  background: green;   
  width: 400px;   height: 100px;   
  line-height: 100px;   
  color: white;   
  text-align: center;   
  transition: transform 2s ease; 
}

/* Toggled State */
input[type=checkbox]:checked ~ label div {   
  background: red;   
  transform: rotate(45deg);
}      

The key sections in the CSS are the toggled state, checkbox hack, and the transition property within the default state of the div.

UPDATED ANSWER

If your JavaScript handles click events and toggles classes, consider using animation instead of transition. Here's an example based on your description:

Here is the corresponding CSS code:

@keyframes spin-forward {
  from {
    transform: rotate(0deg);
  } to {
    transform: rotate(45deg);
  }
}

@keyframes spin-backword {
  from {
    transform: rotate(45deg);
  } to {
    transform: rotate(0deg);
  }
}

.spin-right {
  animation: spin-forward 2s ease forwards;
}

.spin-left {
  animation: spin-backword 2s ease forwards;
}

And here is the accompanying JavaScript code:

$(function() {
  $('div').on('click', function() {
    var el = $(this);
    var klass = this.className;
    if (klass === "") {
      el.addClass('spin-right');
    } else if (klass === 'spin-right') {
      el.attr('class', 'spin-left');
    } else {
      el.attr('class', 'spin-right');
    }
  });
});

The advantage of using animation lies in its enhanced control. The use of forwards enables an element to remain in the new state post-animation completion.

See the live demonstration on JSFiddle

I hope this resolves your issue.

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

Restricting data display in AngularJS with $http and get()

Utilizing the $http service and itsget() method, I am able to access data from the database. Within the database there are 100 records organized in an array of objects; however, I am only interested in obtaining the first 10 records rather than all 100. H ...

Stuck with the Same Theme in the AppBar of material-UI?

Currently, I am attempting to modify the theme of a material-UI AppBar by utilizing states. Strangely enough, although the icons are changing, the theme itself is not. If you'd like to take a look at the code, you can find it here: https://codesandbo ...

Issue with displaying Font Awesome icons in Bootstrap 4 navbar brand

Recently, I've been setting up a local Laravel website to explore the latest features and changes in Laravel 5.6. However, I've encountered an issue while trying to integrate a Font Awesome icon into the navbar-brand of Bootstrap 4. The icon does ...

Using jQuery to show and hide elements on a webpage

One issue I'm facing is changing the content on a page depending on the clicked link. The problem arises when the displayed content for one link persists even after clicking another link, despite setting it to not display when another link is clicked. ...

Customizing the header logo placement on a Wordpress website using CSS

I'm stumped by what should be an easy fix: Check out this site: My goal is to move the logo down by 20px from the top of the page. I've been using Chrome and experimenting with CSS in the developer tools to find the problem, but so far no luck. ...

What are the steps to incorporate @svgr/webpack into turbopack within a next.js project?

I'm encountering an issue with turbopack and @svgr/webpack in my project. According to the documentation, they should work together but it's not cooperating. When I run the project without using --turbo (turbopack), everything functions as expec ...

Effective methods for importing components in VueJS 2.0

As a newcomer to VueJs, I have a question regarding the best practice for importing components in a Vue Template Project. I currently have some components that are used in multiple views. After downloading an admin template, I noticed that the samples alwa ...

Sort through a collection of objects using various criteria

The information is structured as follows: export const data = [ { id: 1, company: "Photosnap", logo: "./images/photosnap.svg", new: true, featured: true, position: "Senior Frontend Developer", ...

In the world of web design, blank areas can sometimes appear on a page as you

My website has a fixed header with a height of 44 pixels, content that takes up 89% of the screen, and a footer that occupies 6% of the space. Everything looks fine when the browser is at 100% zoom, but as I zoom out, white spaces start appearing before th ...

Having trouble setting a default value for an angularjs select element while using a watch function

Explore <select ng-options="department.name for department in departmentsList track by department.departmentID" ng-model="editAllocate.department"> </select> <select ng-options="position.name for position in positionsList track ...

Guide on how to retrieve additional data from the API by pressing the "Load More" button

Hello, I am working on a project where I aim to display user data from an API called https://reqres.in/api/users?page=(the page number can be 1,2 or more) and present it in an HTML table using JavaScript with promises. Currently, I have successfully popula ...

Remove specific data regardless of letter case from the row

I've developed a Google Apps Script that scans specific columns for certain keywords and deletes rows that contain them: function filterRows() { var sheet = SpreadsheetApp.getActiveSheet(); var rows = sheet.getDataRange(); var values = rows.g ...

Modifying input type using Angular

After reading various posts discussing the use of ng-switch or directives for changing input types, I have created this code. However, it seems to be quite redundant. <input ng-change="change();" ng-if="! item.data && item.mapping == 'pass ...

What sets $vm.user apart from $vm.$data.user in Vuejs?

When you need to retrieve component data, there are two ways to do so: $vm.user and $vm.$data.user. Both methods achieve the same result in terms of setting and retrieving data. However, the question arises as to why there are two separate ways to access ...

Is it possible to utilize react for constructing a static HTML, CSS, and JavaScript document?

As a front end content developer, my main focus is on creating content assets. I typically use vscode to build my html, css, and js files, and then upload these static files to the content management system. However, this approach doesn't give me the ...

What is the best way to test an external Data Transfer Object (DTO

I am looking to test an external Data Transfer Object (DTO) that undergoes frequent changes. For example: Here is a sample JavaScript (JSON) file below: // JavaScript const type User = { id: Number, name: String } // JSON user: { id: Number, ...

React function component often uses the "this" keyword in order to

When looking at the code below, I encountered an error stating that handleClick is not defined. I attempted to use onClick={this.handleClick} but it did not resolve the issue. How can I properly access the handleClick function for the onClick event of th ...

Guide on injecting a module into an app and ensuring its accessibility to all modules and submodules, while resolving any unknown provider errors

Kindly note that this is a unique question and not a duplicate of: How to inject module and make it accessible to entire angular app I have a specific module (app.config) that I want to inject into my entire application. This module should be easily acce ...

Just a single page on my website extends beyond the footer's reach

Hey there, I'm pretty new to this whole HTML/CSS thing and need some help. I have a website called cme.horse and everything seems to be running smoothly except for the Home page (main/index page). The issue is that it scrolls past the footer, while al ...

Adding the file URL to the model for saving when submitting

After successfully uploading an image with a custom field template that calls a function to handle the upload, I am facing a challenge in inserting the return URL back into the field for saving with the form. Any assistance on this matter would be highly ...