css3过渡与动画

js实现动画属于帧动画(使用定时器),而css3显示动画为补间动画(使用过渡动画),补间动画需要有起始状态和最终状态。这篇将谈谈css3的过渡与动画。

transition过渡

属性描述
transition-property过渡属性的名称
transition-duration过渡使用的时间,不设置的情况下为0
transition-timing-function过渡的运动曲线,默认为ease(快➡慢)
transition-delay过渡何时开始

1.transition-property,一般情况下值填all,all表示所有属性都获得过渡效果,none则是没有属性获得过渡效果,若定义应用过渡效果的 CSS 属性名称列表,列表以逗号隔开,可以只设置这一个值

2.transition-duration,可以只设置该一个值

3.transition-timing-function

描述
linear匀速
ease(默认值)规定慢速开始,然后变快,然后慢速结束的过渡效果
ease-in规定以慢速开始的过渡效果
ease-out规定以慢速结束的过渡效果
ease-in-out规定以慢速开始和结束的过渡效果

4.transition-delay,相当于是一个延时器,多久后开始

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box1 {
				width: 200px;
				height: 200px;
				background-color: pink;
			}
		</style>
	</head>
	<body>
		<input class="button1" type="button" value="点击">
		<div class="box1"></div>
	</body>
	<script>
		var button1 = document.querySelector(".button1");
		var box1 = document.querySelector(".box1");
		button1.onclick = function(){
			box1.style.transform = "translate(100px,100px)";
			box1.style.transition = "all 1s linear 1s";
		}
	</script>
</html>

transform

animation动画

animation: 动画的名称 时间 运动曲线 开始时间 播放次数 是否反向播放 是否运用结束的样式 动画是否运行或暂停

描述
animation-name指定要绑定到选择器的关键帧的名称
animation-duration动画指定需要多少秒或毫秒完成
animation-timing-function设置动画将如何完成一个周期
animation-delay设置动画在启动前的延迟间隔
animation-iteration-count定义动画的播放次数
animation-direction指定是否应该轮流反向播放动画
animation-fill-mode规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式
animation-play-state指定动画是否正在运行或已暂停

body部分

<input class="button1" type="button" value="点击">
<div class="box1"></div>

css部分

<style>
.box1 {
	width: 200px;
	height: 200px;
	background-color: pink;
}
@keyframes name1 {
	from {
		width: 200px;
		height: 200px;
	}
	to {
		width: 300px;
		height: 300px;
	}
}
</style>

1.动画名称(animation-name),指定要绑定到选择器的关键帧的名称。与@keyframes一起连用,@keyframes 规则是创建动画,紧跟@keyframes后面的值就是动画名称

可以使用from…to…的形式

@keyframes name1 {
	from{}
	to {}
}

也可以使用百分比的形式,百分比的比例由自己定

@keyframes name1 {
	20%{}
	40%{}
	60%{}
	80%{}
	100%{}
}

2.时间(animation-duration),与transform的第二个值一样,表示过渡使用的时间,不设置的情况下为0

3.运动曲线(animation-timing-function),与transform的第三个值一样,具体值,可以看transform-timing-function

4.开始时间(animation-delay),与transform的第四个值一样,延时器

5.播放次数(animation-iteration-count

描述
n一个数字,定义应该播放多少次动画
infinite无穷次

n次

<script>
	var button1 = document.querySelector(".button1");
	var box1 = document.querySelector(".box1");
	button1.onclick = function(){
		box1.style.animation = "name1 1s linear 0.5s 2";
	}
</script>

animation-iteration-count

无穷次

<script>
	var button1 = document.querySelector(".button1");
	var box1 = document.querySelector(".box1");
	button1.onclick = function(){
		box1.style.animation = "name1 1s linear 0.5s infinite";
	}
</script>

animation(infinite)

6.是否反向播放(animation-direction

描述
normal(默认值)动画按正常播放
reverse动画反向播放
alternate动画在奇数次(1、3、5…)正向播放,在偶数次(2、4、6…)反向播放
alternate-reverse动画在奇数次(1、3、5…)反向播放,在偶数次(2、4、6…)正向播放

reverse反向

<script>
	var button1 = document.querySelector(".button1");
	var box1 = document.querySelector(".box1");
	button1.onclick = function(){
		box1.style.animation = "name1 1s linear 0.5s 2 reverse";
	}
</script>

animation-direction

alternate(1,3,5…)正向,(2,4,6…)反向
alternate-reverse则与alternate相反

<script>
	var button1 = document.querySelector(".button1");
	var box1 = document.querySelector(".box1");
	button1.onclick = function(){
		box1.style.animation = "name1 1s linear 0.5s infinite alternate";
	}
</script>

animation-direction(alternate)

7.是否运用结束的样式(animation-fill-mode

描述
none(默认值)动画在动画执行之前和之后不会应用任何样式到目标元素
forwards在动画结束后(由 animation-iteration-count 决定),动画将应用该属性值
backwards动画将应用在 animation-delay 定义期间启动动画的第一次迭代的关键帧中定义的属性值。这些都是 from 关键帧中的值(当 animation-direction 为 “normal” 或 “alternate” 时)或 to 关键帧中的值(当 animation-direction 为 “reverse” 或 “alternate-reverse” 时)
both动画遵循 forwards 和 backwards 的规则。也就是说,动画会在两个方向上扩展动画属性

forwards保持最后的运动状态

<script>
	var button1 = document.querySelector(".button1");
	var box1 = document.querySelector(".box1");
	button1.onclick = function(){
		box1.style.animation = "name1 1s linear 0.5s 2 forwards";
	}
</script>

animation-fill-mode(forwards)

backwards回到初始运动状态

<script>
	var button1 = document.querySelector(".button1");
	var box1 = document.querySelector(".box1");
	button1.onclick = function(){
		box1.style.animation = "name1 1s linear 0.5s backwards";
	}
</script>

animation-fill-mode(backwards)

8.动画是否运行或暂停(animation-play-state

描述
paused暂停动画
running正在运行的动画

animation-play-state

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值