css3之animation属性

本文详细介绍了CSS3中的animation属性,包括animation-name、animation-duration、animation-timing-function、animation-delay、animation-iteration-count和animation-direction的用法,并提供了一个简单的@keyframes动画实例,展示了如何创建和应用动画效果。同时,文章提到了浏览器兼容性问题,例如Chrome、Safari需要使用-webkit-前缀,而Internet Explorer 10及以上版本和Firefox、Opera则直接支持。通过实例,读者可以了解并掌握CSS3动画的基本实现。

css3之animation

animation,是css属性中的简写,它主要有六种动画属性设置,下面简单介绍。

主要包括以下六种:

  • animation-name
  • animation-duration
  • animation-timing-function
  • animation-delay
  • animation-iteration-count
  • animation-direction

基本语法:

animation:name duration timing-function delay iteration-count direction;

下面进行分别介绍:

animation-name规定需要绑定到选择器的keyframe名称
animation-duration规定动画需要完成的时间,一般以秒或是毫秒来计数
animation-timing-function规定动画的速度曲线
animation-delay规定在动画开始时的延迟时间
animation-iteration-count规定动画的播放次数
animation-direction规定是否轮流反向播放动画

了解到以上的用法,我简单介绍一下,@keyframe。

@keyframes 规则用于创建动画。在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果。
谈到@keyframe创建动画了,我们考虑一下浏览器的兼容问题,这就是有时候发现我们的动画在某一个浏览器中无法看到其变化。


Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 规则和 animation 属性。

Chrome 和 Safari 需要前缀 -webkit-。

注释:Internet Explorer 9,以及更早的版本,不支持 @keyframe 规则或 animation 属性。

下面,我们用一个简单实例来演示一下animation的用法和规范。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>animation动画篇</title>
		<style type="text/css">
			.content
			{
				width: 100px;
				height: 100px;
				background: #FFA500;
				animation: myanimation 3s 2;
				-webkit-animation: myanimation 3s 2 true;
				-moz-animation: myanimation 3s 2 true;
				position: relative;
			}
			@-moz-keyframes myanimation
			{
				from{left: 100px;top:0px;background-color: aqua;}
				to{left: 200px;top: 100px;background-color: bisque;}
			}
			@-webkit-keyframes myanimation
			{
				from{left: 100px;top: 0px;background-color: aqua;}
				to{left: 200px;top: 100px;background-color: bisque;}
			}
		</style>
	</head>
	<body>
		<div class="content">
			
		</div>
	</body>
</html>

动画演示:
在这里插入图片描述

### CSS3 `animation` 属性使用教程 #### 一、常见动画属性简介 CSS3 提供了一系列用于创建复杂动画效果的属性。其中最核心的是 `animation` 组合属性,它能够一次性设定多个子属性来控制动画的行为[^2]。 - **`animation-name`**: 定义应用到目标元素上的关键帧名称。 - **`animation-duration`**: 设置完成整个动画循环所需的时间长度,默认单位为秒(s),也可以使用毫秒(ms)[^4]。 - **`animation-timing-function`**: 描述动画的速度曲线,常见的有线性(`linear`)、缓入/缓出(`ease-in`, `ease-out`, `ease-in-out`)等选项[^1]。 - **`animation-delay`**: 指定动画开始前等待的时间间隔。 - **`animation-iteration-count`**: 控制动画重复播放次数;可设为无限次(`infinite`)。 - **`animation-direction`**: 决定了每次迭代的方向变化模式,比如正向运行还是交替反向执行。 - **`animation-fill-mode`**: 影响动画之外的状态,在未启动或结束后如何影响被选中的元素样式[^3]。 #### 二、代码示例 - 创建简单的淡入淡出效果 下面是一个利用上述提到的各种参数组合而成的例子: ```css /* 定义名为 &#39;fade&#39; 的 @keyframes */ @keyframes fade { from { opacity:0; } to { opacity:1; } } /* 应用该动画至特定类名 .example 上 */ .example { /* 单独指定各子属性 */ animation-name: fade; animation-duration: 2s; animation-timing-function: ease-in; animation-delay: 1s; animation-iteration-count: infinite; animation-direction: alternate; animation-fill-mode: both; /* 或者采用简写的复合形式 */ /* animation: fade 2s ease-in 1s infinite alternate both; */ } ``` 此段代码实现了当页面加载完成后的一秒钟延迟后,`.example` 类下的HTML元素将以两秒内逐渐变得完全可见的方式不断切换透明度状态,并且在动画前后保持最终的效果显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值