Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Still rotate on item unhover html

I have an icon that rotates when you hover on it, but only on hover not on 'unhover'. The animation however stops when removing the cursor from the object. How can I let it resume the animation even when the cursor isn't on the object anymore? My Code:

.header .logo img {
    transform: rotate(0deg);
    transition: transform 0s 0s; 
}

.header .logo img:hover {
    transform: rotate(360deg);
    transition: transform 1.2s;
    animation-play-state: running;
}

Thank you for helping in advance

like image 257
Brentspine Avatar asked Dec 17 '21 20:12

Brentspine


People also ask

How do you make the hover effect stay even after Unhover?

You can use Jquery to set a class when the mouse is hovered. Then the class will remain set even after the mouse moves away.

How do you rotate something 90 degrees in HTML?

90 degrees would equal to 100 gradient or 0.25 turns. Applying this property to the required element would rotate it by 90 degrees. Syntax: // Using CSS transform: rotate(90deg); // Using JavaScript element.

How to rotate text 360 degrees in HTML with CSS?

HTML Code: In this section we will create a basic div element which will have some text inside of it. CSS Code: In this section first we will design the text using basic CSS properties and then we will create the animation using @keyframes rule, we will use the transform property to rotate the text 360 degrees at regular intervals.

How to rotate the HTML <div> element using CSS transform?

Solution with the CSS transform property ¶ Before showing how to rotate the HTML <div> element, it should be noted that applying the 90deg rotation to the square would result in the same appearance. You need to add the width and height properties for the container and specify the transform property with the “rotate (90deg)” value.

How do you use transition on hover in CSS?

Transition on Hover. CSS transitions allows you to change property values smoothly (from one value to another), over a given duration. Add a transition effect (opacity and background color) to a button on hover: Fade in on hover:

How do you keep the onabout button on when hover?

If you wrap your #onabout in a position: fixed wrapper that is also revealed on hover, and set that wrapper to fix to the top, right, bottom, and left of the screen, then use that wrapper's :hover to keep the #onabout it will stay permanently.


Video Answer


1 Answers

You need some way of the element 'remembering' that it has to carry on rotating.

For that you'll need a bit of Javascript to sense when the mouse is hovering over the element and to sense when the transition has ended. These events will add and remove a class respectively.

const div = document.querySelector('.header .logo img');

function addRotate() {
  div.classList.add('rotate');
}

function removeRotate() {
  div.classList.remove('rotate');
}
div.addEventListener('mouseover', addRotate);
div.addEventListener('transitionend', removeRotate);
.header .logo img {
  transform: rotate(0deg);
  transition: transform 0s 0s;
}

.header .logo img.rotate {
  transform: rotate(360deg);
  transition: transform 1.2s;
}
<div class="header">
  <div class="logo">
    <img src="https://picsum.photos/id/1015/200/300">
  </div>
</div>
like image 91
A Haworth Avatar answered Oct 22 '22 03:10

A Haworth