I am trying to make hover scale work smoothly with animation scale. Hover works with #test but with .rotate I can’t get it to work correctly. Also, why the hover scale doesn’t work when the animation-fill-mode is forwards instead of none?
https://codepen.io/yoholil/pen/qBLbYYm
let flag = false;
let test = document.querySelector("#test")
test.addEventListener("click", function() {
console.log(1);
flag && test.classList.add("rotate");
!flag && test.classList.remove("rotate");
flag = !flag;
});
#test {
width: 200px;
transition: 0.3s ease;
animation: rotateRight 0.3s ease-in-out none;
}
#test:hover {
transform: scale(1.2);
}
#test.rotate {
transform: scaleX(-1);
animation: rotateLeft 0.3s ease-in-out none;
}
@keyframes rotateRight {
0% {
transform: scaleX(-1);
}
100% {
transform: scaleX(1);
}
}
@keyframes rotateLeft {
0% {
transform: scaleX(1);
}
100% {
transform: scaleX(-1);
}
}
<img id="test" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Stack_Overflow_icon.svg/768px-Stack_Overflow_icon.svg.png" />
2
Answers
If you need the initial flip animation, then consider using a CSS variable that carries the scaling multiplier so that it works within the
animation
:Otherwise, if you don’t need the initial flip animation, consider removing the use of any
animation
altogether:Additionally, the hover scale doesn’t work when the
animation-fill-mode
isforwards
instead ofnone
because thetransform
property value in the@keyframes
would take precedence over any of properties defined in the CSS rules.Just use a wrapper and apply
:hover
to it: