CSS结合序列帧实现动效

想要实现空调出风效果,但是只有从左到右出风的序列帧图片,需要通过animation实现正序倒序循环播放,需要使用scss语法。

代码如下:

// 定义一个放置动效的容器
<div className='animation'></div>
// scss
	$imgCount: 35;	//图片数量减1
	.animation {
		position: absolute;
		animation-name: animation;
		animation-duration: 2s;
		animation-timing-function: linear;
		// animation-delay: 0, 2s;
		animation-iteration-count: infinite; //无线循环播放
		animation-fill-mode: forwards;
		animation-direction: alternate;  //奇数次正序播放、偶数次倒序播放;如果给出了完整的从左到右从右到左序列帧,则不需要设置此参数
		width: 352px;
		height: 200px;
		background-position: center center;
    	background-repeat: no-repeat;
    	background-size: contain;
	}

	@keyframes animation {
		@for $i from 0 through $imgCount {
			#{($i * 100%/$imgCount)} {
				background-image:url(../imgs/guangjiao_zidong_gaosu/G-Z-G#{$i}.png); //图片路径
			}
		}
	}

这样就可以实现从左到右,从右到左循环出风效果。

拓展:
想要实现两个动画顺序循环播放,需要将第二个动画的animation-delay设置为第一个动画的animation-duration,即将第二个动画的延时时间设置为第一个动画的持续时间。

参考代码如下:

// 定义一个放置动效的容器
<div className='animation'></div>
// scss
	$imgCount: 35;
		.animation {
			position: absolute;
			animation-name: animation1, animation2;
			animation-duration: 2s, 2s;
			animation-timing-function: linear, linear;
			animation-delay: 0, 2s;
			animation-iteration-count: 1, infinite;
			animation-fill-mode: forwards, forwards;
			animation-direction: normal, alternate;
			width: 350px;
			height: 200px;
			background-position: center center;
    		background-repeat: no-repeat;
    		background-size: contain;
		}

		@keyframes animation1 {
			@for $i from 0 through $imgCount {
				#{($i* 100%/$imgCount)} {
					background-image: url(../imgs/guangjiao_zidong_gaosu/G-Z-G#{$i}.png);
				}
			}
		}
		@keyframes animation2 {
			@for $i from $imgCount through 0 {
				#{((35 -$i) * 100%/$imgCount)} {
					background-image: url(../imgs/guangjiao_zidong_gaosu/G-Z-G#{$i}.png);
				}
			}
		}

参考链接:
animation中各参数介绍

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值