html - 使用css旋转图像而不绕圈移动
问题描述
我有一张风力涡轮机的图像,我想旋转它。下面的代码将完成这项工作,但是风力涡轮机围绕一个圆圈旋转。我怎样才能让它不绕圈旋转?请有任何建议
<!DOCTYPE html>
<html>
<head>
<style>
.rotate {
animation: rotation 3s infinite linear;
}
@keyframes rotation {
from { transform: rotate(0deg); }
to { transform: rotate(359deg); }
}
</style>
</head>
<body>
<img src="./blackwindturbine.png" class="rotate" width="200" height="200" />
</body>
</html>
解决方案
旋转变换的默认行为是围绕其中心旋转元素。您可以通过设置transform-origin
属性来更改它。
https://www.w3schools.com/cssref/css3_pr_transform-origin.asp
您应该在图像编辑器中找到旋转中心的坐标。
<!DOCTYPE html>
<html>
<head>
<style>
.rotate {
animation: rotation 3s infinite linear;
transform-origin: 50% 63.67%; // (vertical center) / (image height) * 100 = (percentage)
}
@keyframes rotation {
from { transform: rotate(0deg); }
to { transform: rotate(359deg); }
}
</style>
</head>
<body>
<img src="https://cdn2.iconfinder.com/data/icons/industry-3/512/rotor-512.png" class="rotate" width="200" height="200" />
</body>
</html>
推荐阅读
- azure - 用于 OneDrive for business 代码流的重定向 url 是什么?
- firebase - Google Analytics 事件参数仅显示在过去 30 分钟内的事件中
- .net-core - 通过自定义 NuGet 包向 appsettings.json 添加日志记录条目
- javascript - 创建基于 API 的星级评分系统
- spring-boot - 如何使用连接表加载相互关系@OneToMany的2个实体的完整图表
- azure-functions - azure 函数应用程序配置中的 APPINSIGHTS_INSTRUMENTATIONKEY
- charts - 仅部分标签的 chart.js 行
- azure - 如何在 Azure 中删除服务器时发出警报?
- r - 如何在 R 中确定日期间隔是否与数据框中同一个人的另一个日期间隔重叠?
- c# - 尝试在 websocket 消息事件中时无法实例化预制件