Animation for the image inside the button

I am new to this coding field, I think my problem is simple for experienced people like you, all guys. I am trying to animate an image that is inside a button. When I click on the button, I need the image to be rotated 360deg. I don't know what I'm doing here, this is my HTML and CSS code. Please, help

.syncRotate { -webkit-transition: 500ms ease all !important; -moz-transition: 500ms ease all !important; -o-transition: 500ms ease all !important; transition: 500ms ease all !important; -webkit-transform: rotate(360deg) !important; } 
 <button class="iconBtn" mdTooltipPosition="above" mdTooltip="Sync User" (click)="sync()"> <img class="syncRotate" style="width:20px;background-color: #a6a6a6;border-radius: 13px;padding:2px;" src="../../assets/images/icons/sync.svg" alt="Sync"> </button> 
+5
source share
3 answers

This will work for you if you want to use it only with a transition .!

 .syncRotate { -webkit-transition: 500ms ease all; -moz-transition: 500ms ease all; -o-transition: 500ms ease all; transition: 500ms ease all; -webkit-transform: rotate(0deg); } .syncRotate:active { -webkit-transform: rotate(360deg); transform: rotate(360deg); } 
 <button class="iconBtn" mdTooltipPosition="above" mdTooltip="Sync User" (click)="sync()"> <img class="syncRotate" style="width:20px;background-color: #a6a6a6;border-radius: 13px;padding:2px;" src="https://lh6.ggpht.com/bE_u2kFoX28G428YH1jpWTMoaZ3B9cokstrKxD82zj7hoBNL-U1wiAVddau3mvvUpPoD=w300" alt="Sync"> </button> 

IF you want to make nice animations using CSS animations

 @keyframes rotation { 0% { -webkit-transform: rotate(0deg); } 50% { -webkit-transform: rotate(360deg); } 100% { -webkit-transform: rotate(0deg); } } .syncRotate { -webkit-transform: rotate(0deg); } .syncRotate:active { animation: rotation 500ms ease-in-out forwards; } 
 <button class="iconBtn" mdTooltipPosition="above" mdTooltip="Sync User" (click)="sync()"> <img class="syncRotate" style="width:20px;background-color: #a6a6a6;border-radius: 13px;padding:2px;" src="https://lh6.ggpht.com/bE_u2kFoX28G428YH1jpWTMoaZ3B9cokstrKxD82zj7hoBNL-U1wiAVddau3mvvUpPoD=w300" alt="Sync"> </button> 

Changing from 500 ms to 400 ms, we can speed it up , and changing it higher will make the animation and transition slower, for example, 900 ms or 1 s .

Note. . Since we are only using CSS , animations and transitions are performed only when the button is clicked. SO in your case both will be the same.

+3
source

360deg rotate without click

 .syncRotate { overflow: hidden; transition-duration: 0.8s; transition-property: transform; } .syncRotate:hover { transform: rotate(360deg); -webkit-transform: rotate(360deg); } 
 <button class="iconBtn" mdTooltipPosition="above" mdTooltip="Sync User" (click)="sync()"> <img class="syncRotate" style="width:20px;background-color: #a6a6a6;border-radius: 13px;padding:2px;" src="https://lh6.ggpht.com/bE_u2kFoX28G428YH1jpWTMoaZ3B9cokstrKxD82zj7hoBNL-U1wiAVddau3mvvUpPoD=w300" alt="Sync"> </button> 
+2
source

Vanilla JS Solution :

  • Add a rotation class to the element with classList.add ()
  • Delete the rotation class after it so that the next click changes as desired using classList.remove () inside setTimeout () .
  • You can adjust the rotation speed from the transition time interval (1500 per demo), do not forget to set the time equal to or exceeding the transition time.

 document.querySelector(".iconBtn").addEventListener('click', function() { document.querySelector("img").classList.add("syncRotate"); //remove class after rotation is over so next click will rotate again setTimeout(function() { document.querySelector("img").classList.remove('syncRotate'); }, 1500); }); 
 .syncRotate { -webkit-transition: 1500ms ease all; -moz-transition: 1500ms ease all; -o-transition: 1500ms ease all; transition: 1500ms ease all; -webkit-transform: rotate(360deg); } 
 <button class="iconBtn" mdTooltipPosition="above" mdTooltip="Sync User" (click)="sync()"> <img class="image" style="width:20px;background-color: #a6a6a6;border-radius: 13px;padding:2px;" src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png" alt="Sync"> </button> 

JQuery solution :

  • Add a rotation class to an element using addClass ()
  • Retrieve the rotation class after it so that the next click changes as desired using removeClass () inside setTimeout () .
  • You can adjust the rotation speed from the transition time interval (1500 per demo), do not forget to set the time equal to or exceeding the transition time.

 $(".iconBtn").on('click', function() { $('img').addClass("syncRotate"); //remove class after rotation is over so next click will rotate again setTimeout(function() { $('img').removeClass('syncRotate'); }, 1500); }); 
 .syncRotate { -webkit-transition: 1500ms ease all; -moz-transition: 1500ms ease all; -o-transition: 1500ms ease all; transition: 1500ms ease all; -webkit-transform: rotate(360deg); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="iconBtn" mdTooltipPosition="above" mdTooltip="Sync User" (click)="sync()"> <img class="image" style="width:20px;background-color: #a6a6a6;border-radius: 13px;padding:2px;" src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png" alt="Sync"> </button> 
+1
source

Source: https://habr.com/ru/post/1270744/


All Articles