CSS3 怎么来实现动画的?

在 CSS3 中,实现动画主要依靠 @keyframes 规则和 animation 属性。@keyframes 规则用于定义动画过程中的关键帧,而 animation 属性用于将定义好的动画应用到选择的元素上。以下是实现动画的基本步骤:

  1. 定义关键帧 (@keyframes): 使用 @keyframes 创建动画名称和关键帧。关键帧描述了动画序列中的样式变化。

  2. 应用动画: 使用 animation 属性将定义的动画应用到元素上。可以控制动画的持续时间、延迟、迭代次数等。

以下都是小例子,直接运行可看到效果 

 1、淡入淡出动画

<template>
	<view class="content">
		<view>
			<view class="box"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	/* 定义关键帧 */
	@keyframes fadeInOut {
	  from {
	    opacity: 0;
	  }
	  to {
	    opacity: 1;
	  }
	}
	.box{
		display: block;
		width: 100px;
		height: 100px;
		background-color: orange;
		border-radius: 50%;
		/* 应用动画 */
		animation-name: fadeInOut; /* 动画名称 */
		animation-duration: 3s; /* 动画持续时间 */
		animation-iteration-count: infinite; /* 动画迭代次数, infinite 是无限循环*/
		animation-direction: alternate; /* 动画方向 */
	}
</style>

2.旋转动画

<template>
	<view class="content">
		<view>
			<view class="box"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	/* 定义关键帧 */
	@keyframes rotateAnimation {
	  from {
	    transform: rotate(0deg);
	  }
	  to {
	    transform: rotate(360deg);
	  }
	}
	
	.box{
		margin-top: 200px;
		width: 100px;
		height: 100px;
		background-color: orange;
		/* 应用动画 */
		animation: rotateAnimation 2s linear infinite;
	}
</style>

3.弹跳动画

<template>
	<view class="content">
		<view>
			<view class="box"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	/* 定义关键帧 */
	@keyframes bounce {
	  0%, 20%, 50%, 80%, 100% {
	    transform: translateY(0);
	  }
	  40% {
	    transform: translateY(-200px);
	  }
	  60% {
	    transform: translateY(-15px);
	  }
	}
	
	.box{
		margin-top: 200px;
		width: 100px;
		height: 100px;
		background-color: orange;
		/* 应用动画 */
		 animation: bounce 2s infinite;
	}
</style>

4.颜色渐变动画

<template>
	<view class="content">
		<view>
			<view class="box"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	/* 定义关键帧 */
	@keyframes colorChange {
	  0% {
	    background-color: orange;
	  }
	  50% {
	    background-color: skyblue;
	  }
	  100% {
	    background-color: lightblue;
	  }
	}
	
	.box{
		margin-top: 200px;
		width: 100px;
		height: 100px;
		background-color: orange;
		/* 应用动画 */
		animation: colorChange 5s infinite;
	}
</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用CSS来美化页面可以提高页面的可读性和易用性,以下是几个可能的优化方法: 1. 使用CSS Reset:使用CSS Reset可以消除不同浏览器之间的差异,从而使页面在不同浏览器上呈现更加一致。常用的CSS Reset库有Normalize.css和Reset.css。 2. 使用CSS布局:使用CSS布局可以使页面的结构更加清晰,使其易于维护和更新。常用的CSS布局技术有Flexbox和Grid布局。 3. 使用CSS预处理器:使用CSS预处理器可以使CSS更加易于编写和维护。常用的CSS预处理器有Sass和Less。 4. 使用CSS模块化:使用CSS模块化可以使CSS更加易于管理和组织。常用的CSS模块化方案有BEM和CSS Modules。 5. 使用CSS动画:使用CSS动画可以增强页面的交互性和视觉效果,使其更加生动和吸引人。常用的CSS动画技术有Transition和Animation。 例如,可以使用CSS来美化购物车页面,例如: ``` .shopping-cart { display: flex; flex-direction: column; align-items: center; } .shopping-cart-title { font-size: 24px; font-weight: bold; margin-bottom: 24px; } .shopping-cart-total { font-size: 18px; font-weight: bold; margin-top: 24px; } .cart-item { display: flex; justify-content: space-between; align-items: center; width: 100%; margin-bottom: 8px; } .cart-item-name { flex: 1; } .cart-item-price { margin-left: 16px; } ``` 其中,shopping-cart是购物车页面的根元素,shopping-cart-title是购物车标题,shopping-cart-total是购物车总金额,cart-item是购物车中的商品项,cart-item-name是商品名称,cart-item-price是商品价格。通过使用CSS来美化页面,可以使页面更加易读易用,提高用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值