页面上只有一个<div>
。红场,我希望对其应用2种动画:
这是代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.animation {
width: 150px;
height: 150px;
background-color: red;
animation-name: anim;
animation-duration: 3s;
animation-fill-mode: forwards;
}
.animation:hover {
animation-name: hover;
animation-duration: 1s;
}
@keyframes anim {
from {
}
30% {
transform: translateX(100px);
}
70% {
transform: translate(100px, 100%);
/*background-color: blue;*/
}
100% {
transform: translate(100px, 100%) scale(2, 2) rotate(145deg);
}
}
@keyframes hover {
from {
}
to {
background:yellow;
}
}
</style>
</head>
<body>
<div class="animation">
</div>
</body>
</html>
https://jsfiddle.net/tbqj4goy/
问题在于,当我将鼠标悬停在嘴上时,第一个动画会中断并从头开始。
有什么想法可以让这些动画在没有问题的情况下进行协作吗?
答案 0 :(得分:3)
.animation {
width: 150px;
height: 150px;
background-color: red;
animation-name: anim;
animation-duration: 3s;
animation-fill-mode: forwards;
transition: 1s;
}
.animation:hover {
background-color: yellow;
transition: 1s;
}
@keyframes anim {
from {}
30% {
transform: translateX(100px);
}
70% {
transform: translate(100px, 100%);
/*background-color: blue;*/
}
100% {
transform: translate(100px, 100%) scale(2, 2) rotate(145deg);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="animation">
</div>
</body>
</html>
答案 1 :(得分:0)
要更改背景颜色,您不需要动画,只需将背景属性添加到:hover;
.animation {
width: 150px;
height: 150px;
background-color: red;
transition: background-color 250ms ease;
animation: anim 3s forwards;
}
.animation:hover {
background-color: yellow;
}
@keyframes anim {
from {
}
30% {
transform: translateX(100px);
}
70% {
transform: translate(100px, 100%);
/*background-color: blue;*/
}
100% {
transform: translate(100px, 100%) scale(2, 2) rotate(145deg);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="animation">
</div>
</body>
</html>