body{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
div {
height: 50px;
width: 200px;
background: rgb(255, 99, 99);
}
div:hover{
transform: translateX(100px) rotateZ(45deg);
transition: transform 2s;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>Hover on me.</div>
</body>
</html>
你可以使用单独的转换,但要注意浏览器的支持,因为这是一个新的功能
body{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
div {
height: 50px;
width: 200px;
background: rgb(255, 99, 99);
transition: translate 2s;
}
body:hover div{
translate: 100px 0;
rotate: 45deg;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>Hover on me.</div>
</body>
</html>
这里有一个使用关键帧实现这一点的简单方法。注意,当div移动时,您需要移动光标,以保持悬停状态。
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
div {
height: 50px;
width: 200px;
background: rgb(255, 99, 99);
}
div:hover {
animation: onlyTranslate 1s linear forwards;
}
@keyframes onlyTranslate {
0% {
transform: translateX(0px) rotateZ(0deg);
}
1% {
transform: translateX(0px) rotateZ(45deg);
}
100% {
transform: translateX(100px) rotateZ(45deg);
}
}
<div>Hover on me.</div>
您可以尝试使用关键帧动画来代替过渡。 将rotateZ()放在它应该触发的%范围内。 否则,你可以尝试JS实现,告诉我你是否需要一个样本。
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
div {
height: 50px;
width: 200px;
background: rgb(255, 99, 99);
}
div:hover {
animation: move 2s normal forwards ease-in-out;
}
@keyframes move {
0% {
transform: translateX(0px) rotateZ(0deg);
}
95% {
transform: translateX(100px) rotateZ(0deg);
}
100% {
transform: translateX(100px) rotateZ(45deg);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>Hover on me.</div>
</body>
</html>