如何使用 CSS 摇动/摆动图片?

cssweb developmentfront end technology更新于 2025/10/4 23:40:17

以下是使用 CSS 摇动图片的代码 −

示例

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
img{
   width: 400px;
   height: 400px;
   margin:50px;
}
img:hover {
   animation: shakeImg 0.5s;
   animation-iteration-count: infinite;
}
@keyframes shakeImg {
   0% { transform: translate(1px, 1px) rotate(5deg); }
   20% { transform: translate(-3px, 0px) rotate(10deg); }
   30% { transform: translate(3px, 2px) rotate(-10deg); }
   50% { transform: translate(-1px, 2px) rotate(-20deg); }
   70% { transform: translate(3px, 1px) rotate(-25deg); }
   90% { transform: translate(1px, 2px) rotate(10deg); }
   100% { transform: translate(1px, -2px) rotate(-10deg); }
}
</style>
</head>
<body>
<h1>Hover over the image to shake it<h1>
<img src="https://i.picsum.photos/id/551/400/400.jpg">
</body>
</html>

输出

上述代码将产生以下输出 −

鼠标悬停在图像上时,图像会抖动,如下面的输出所示 −


相关文章


有用资源