几个CSS的简单动画

本文介绍了CSS中的transform属性,包括translate、scale和rotate的使用方法,并通过实例展示了如何创建放大、心跳和心电图动画效果。同时,详细解析了animation属性的各项参数,如animation-name、animation-duration等,帮助理解CSS动画的工作原理。
摘要由CSDN通过智能技术生成

几个CSS的简单动画

transform属性简介

  1. translate(X, Y):2D面上的X方向和Y方向的移动;animation 属性里面,如果设置transform,则原样式中设置的transform可能会失效

    比如:

     .box {
         width: 100px;
         height: 100px;
         background-color: pink;
         transform: translate(100px, 100px);
         animation: move 2s linear infinite;
     }
    
     @keyframes move {
         0% {transform: scale(1);}
         100% {transform: scale(2);}
     }
    

    .box的“transform”属性就会失效。。。

  2. scale(num):2D面上的缩放,1是原始大小

  3. rotate(num):2D面上的旋转,带上单位deg,如:rotate(30deg)

animation属性简介

  1. animation-name:@keyframes的名称;keyframes是具体动画样式设置
  2. animation-duration:动画持续时间,一般为秒,如:animation-duration: 2s
  3. animation-timing-function:动画的速度曲线,即动画运动速度,一般为linear
  4. animation-delay:动画延迟时间,一般为秒
  5. animation-iteration-count:动画播放次数,有1次,2次和infinite,如:animation-iteration-count: infinite
  6. animation-direction:动画是否轮流反向,即设置是否倒放等效果,一般为alternate
  7. animation:综合写法,如:animation: name duration timing-function iteration-count direction

栗子一

不断放大的正方形

<style>
    .box {
        width: 100px;
        height: 100px;
        background-color: pink;
        // 设置动画效果,持续两秒,匀速运动,无限次数,不倒放
        animation: move 2s linear infinite;
    }

    @keyframes move {
    	// 开始大小为原始大小,结束大小为两倍大小
        0% {transform: scale(1);}
        100% {transform: scale(2);}
    }
</style>

<body>
	<div class="box"></div>
</body>

栗子二

类似心脏跳动的心形

<style>
	.heart {
		width: 50rpx;
		height: 50rpx;
		// 设置动画效果,持续一秒,匀速运动,无限次数,不倒放
		animation: beat 1s linear infinite;
	}

	@keyframes beat {
		// 每个大括号都是一次关键帧,0的时候为原始大小0.8,透明度1,剩下的以此类推
		0% {
			transform: scale(0.8);
			opacity: 1;
		}
		25% {
			transform: scale(1.2);
			opacity: 0.8;
		}
		100% {
			transform: scale(0.8);
			opacity: 1;
		}
	}
</style>

<body>
	// 插入心脏图片
	<image class="heart" src="./love.png"></image>
</body>

栗子三

类似心电图,从左往右逐渐出现

<style>
	.pictureBox {
        width: 360px;
        height: 100px;
        // 添加超出隐藏
        overflow: hidden;
		animation: picStretch 10s linear infinite;
    }

	// 图片也要设置宽高,不要用百分比,用数字
    .pictureBox img {
        width: 360px;
        height: 100px;
    }

    @keyframes picStretch {
        0% {width: 0;}
        20% {width: 72px;}
        40% {width: 144px;}
        60% {width: 216px;}
        80% {width: 288px;}
        100% {width: 360px;}
    }
</style>

<body>
	<div class="pictureBox">
		<img src="./images/line.png" alt="">
	</div>
</body>

以上
有错望指正…

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值