【动画消消乐|CSS】调皮逃跑的小方块 077

时间:2021-08-06
本文章向大家介绍【动画消消乐|CSS】调皮逃跑的小方块 077,主要包括【动画消消乐|CSS】调皮逃跑的小方块 077使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

Hello!小伙伴!
非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~
 
自我介绍 ଘ(੭ˊᵕˋ)੭
昵称:海轰
标签:程序猿|C++选手|学生
简介:因C语言结识编程,随后转入计算机专业,有幸拿过国奖、省奖等,已保研。目前正在学习C++/Linux(真的真的太难了~)
学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!
 
【动画消消乐】 平时学习生活比较枯燥,无意之间对一些网页、应用程序的过渡/加载动画产生了浓厚的兴趣,想知道具体是如何实现的? 便在空闲的时候学习下如何使用css实现一些简单的动画效果,文章仅供作为自己的学习笔记,记录学习生活,争取理解动画的原理,多多“消灭”动画!

效果展示

来个特写

Demo代码

HTML

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css">
        <title>Document</title>
    </head>

    <body>
        <section>
            <div class="box">
                <div class="cube"></div>
                <div class="shadow"></div>
            </div>
        </section>
    </body>

</html>

CSS

html, body {
	margin: 0;
	height: 100%;
}

body {
	display: flex;
	justify-content: center;
	align-items: center;
	background: #ed556a;
	overflow: hidden;
}

section {
	width: 650px;
	height: 300px;
	padding: 10px;
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	border: 2px solid white;
	overflow: hidden;
}

.box {
	animation: run 8s linear infinite;
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
}

.cube {
	width: 50px;
	height: 50px;
	background: #fff;
	animation: loading .5s linear infinite;
	border-radius: 3px;
}

.shadow {
	width: 50px;
	height: 5px;
	background: #000;
	opacity: .2;
	border-radius: 50%;
	margin-top: 9px;
	animation: shadow .5s linear infinite;
}

@keyframes run {
	0% {
		left: -100%;
	}
	100% {
		left: 110%;
	}
}

@keyframes loading {
	17% {
		border-bottom-right-radius: 3px;
	}
	25% {
		transform: translateY(9px) rotate(22.5deg);
	}
	50% {
		transform: translateY(18px) scale(1, .9) rotate(45deg);
		border-bottom-right-radius: 40px;
	}
	75% {
		transform: translateY(9px) rotate(67.5deg);
	}
	100% {
		transform: translateY(0) rotate(90deg);
	}
}

@keyframes shadow {
	50% {
		transform: scale(1.2, 1);
	}
}

原理详解

步骤1

从效果图中可以看出

整个动画含有两个部分:白色方块+深色阴影

所以 我们使用一个div盒子包含这两个部分

其中

  • cube类表示白色方块
  • shadow类表示深色阴影
            <div class="box">
                <div class="cube"></div>
                <div class="shadow"></div>
            </div>

步骤2

设置box类为

  • 相对定位
  • 使用flex布局
  • 元素上下左右居中
  • 列排列(使得cube在上 shadow在下)
.box {
	position: relative;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
}

步骤3

设置cube

  • 宽度、高度均为50px
  • 背景色:白色
  • border-radius: 3px;
.cube {
	width: 50px;
	height: 50px;
	background: #fff;
	border-radius: 3px;
}

效果图如下

步骤4

设置shadow

  • 宽度为50px 高度为5px
  • 背景色:黑色
.shadow {
	width: 50px;
	height: 5px;
	background: #000;
}

效果图如下


shadow再向下移动9px

	margin-top: 9px;

效果图如下


再设置 border-radius为50%

	border-radius: 50%;

效果图如下

再降低shadow颜色透明度

opacity: .2;

效果图如下

步骤5

为cube添加动画

从最开始的效果展示中可以发现

  • cube自身在不停旋转(2D)
  • 当四个角中的一个角接触到最下方时,变得更加圆润(词穷了)
  • 同时y轴方向有上下移动

效果展示

将动画分为5帧

第一帧 也就是初始状态

第二帧

  • y轴方向移动9px
  • 旋转22.5度(相对于初始位置)
transform: translateY(9px) rotate(22.5deg);

效果图如下


第三帧(关键帧)

  • y轴下移动18px
  • 自身旋转45度(相对于初始位置)
  • 大小缩放: x轴方向不变 y轴缩小为原来的0.9倍
  • 同时修改 右下角border-radius为40px 其余三个角的radius不变
		transform: translateY(18px) scale(1, .9) rotate(45deg);
		border-bottom-right-radius: 40px;

效果图如下


注:border-bottom-right-radius: 40px;是指设置右下角radius为40px

第四帧

  • y轴方向只下移动9px(相当于第三帧后再上移9px)
  • 相对于初始位置 旋转67.5度(相对于初始位置)
transform: translateY(9px) rotate(67.5deg);

效果图如下

第五帧

  • y轴方向移动0px(其实就是又回到了初始位置)
  • 旋转角度为90度(相对于初始位置)
transform: translateY(0) rotate(90deg);

效果图如下

得到cube动画css代码

.cube {
	animation: loading .5s linear infinite;
}

@keyframes loading {
	17% {
		border-bottom-right-radius: 3px;
	}
	25% {
		transform: translateY(9px) rotate(22.5deg);
	}
	50% {
		transform: translateY(18px) scale(1, .9) rotate(45deg);
		border-bottom-right-radius: 40px;
	}
	75% {
		transform: translateY(9px) rotate(67.5deg);
	}
	100% {
		transform: translateY(0) rotate(90deg);
	}
}

效果图如下

步骤6

为shadow添加动画

这个就相对比较简单了

只需要阴影在x轴方向随着时间变大变小即可

.shadow {
	animation: shadow .5s linear infinite;
}
@keyframes shadow {
	50% {
		transform: scale(1.2, 1);
	}
}

效果图如下

步骤7

cube和shadow动画同时生效时

步骤8

为了实现方块从左到右奔跑的效果

只需要在box设置一个动画即可

效果描述为:

  • 初始位置left: -100%;
  • 末尾位置left: 110%;
@keyframes run {
	0% {
		left: -100%;
	}
	100% {
		left: 110%;
	}
}

效果图如下


记得在box的父级元素设置overflow: hidden;

结语

文章仅作为学习笔记,记录从0到1的一个过程

希望对您有所帮助,如有错误欢迎小伙伴指正~

我是 海轰ଘ(੭ˊᵕˋ)੭

如果您觉得写得可以的话,请点个赞吧

谢谢支持❤️

参考:

https://codepen.io/_fbrz/pen/mpiFE

原文地址:https://www.cnblogs.com/haihongpro/p/15107312.html