在 HTML 中使用 CSS3 动画效果需要使用 @keyframes
规则来定义动画,以及 animation
属性来应用动画。
以下是使用 CSS3 动画效果的示例代码:
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Animation</title>
<style>
/* 定义动画 */
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
/* 应用动画 */
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 2s;
animation-iteration-count: infinite;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
在上述代码中,@keyframes
规则定义了一个名为 example
的动画,它从红色背景色渐变到黄色背景色。而 div
元素则应用了这个动画,设置了动画的名称为 example
,持续时间为 2 秒,重复次数为无限次。
关键词高亮:
@keyframes
animation
animation-name
animation-duration
animation-iteration-count