css 动画

1. css 动画属性

CSS 动画通过 @keyframes 规则和动画属性,让元素在一段时间内平滑地改变样式。动画的使用包括定义关键帧(描述每个阶段样式变化)以及在元素上应用动画属性。

  • 基础的,css 动画包含 8 个基础属性:
    • animation-name 自定义动画的名称
    • animation-duration 动画的持续时间
    • animation-timing-function 速度曲线
    • animation-delay 延迟时间
    • animation-iteration-count 循环次数
    • animation-direction 动画方向
    • animation-fill-mode 动画执行前后的样式
    • animation-play-state 动画播放状态
  • 属性简写 animation 的顺序推荐
.animation-style {
	animation: name duration timing-function delay iteration-count direction fill-mode play-state;
}


2. loading 动画示例

关键帧名称可以是自定义的字符串。每个关键帧定义动画的某个时间点的样式。通常,0% 表示动画的开始,100% 表示动画的结束。也可以使用 from 和 to 替代 0% 和 100%。

  • 示例1:
<div class="spinner"></div>
<style>
.spinner {
	width: 40px;
	height: 40px;
	border: 5px solid transparent;
	border-top-color: lightcoral;
	border-radius: 50%;
	animation: spin 1s linear infinite;
}

@keyframes spin {
	0% {
		transform: rotate(0deg);
	}
	100% {
		transform: rotate(360deg);
	}
}
</style>
  • 示例2:
<div class="loading-spinner">
	<svg class="circular" viewBox="0 0 50 50">
		<circle class="path" cx="25" cy="25" r="20" fill="none"></circle>
	</svg>
</div>
<style>
.loading-spinner .circular {
    display: inline;
    height: 100px;
    width: 100px;
    animation: loading-rotate 2s linear infinite;
}

.loading-spinner {
    top: 50%;
    width: 100%;
    text-align: center;
    position: absolute;
}

.loading-spinner .path {
    animation: loading-dash 1.5s ease-in-out infinite;
    stroke-dasharray: 90, 150;
    stroke-dashoffset: 0;
    stroke-width: 2;
    stroke: lightblue;
    stroke-linecap: round;
}

@keyframes loading-rotate {
    100% {
        transform: rotate(360deg);
    }
}

@keyframes loading-dash {
    0% {
        stroke-dasharray: 1, 200;
        stroke-dashoffset: 0;
    }

    50% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -40px;
    }

    100% {
        stroke-dasharray: 90, 150;
        stroke-dashoffset: -120px;
    }
}
</style>


3. 动画事件

  • animationstart 动画开始时触发
  • animationend 动画正常结束时触发
  • animationcancel 动画被中止时触发
  • animationiteration 新一轮循环开始时触发

ps: 动画异常中止时会触发 animationcancel,不会触发 animationend

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值