如何用纯 CSS 创作一只徘徊的果冻怪兽
2024-10-20 05:50:44
效果预览
按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。
https://codepen.io/comehope/pen/VdOKQG
可交互视频
此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。
请用 chrome, safari, edge 打开观看。
https://scrimba.com/p/pEgDAM/c43ekUL
源代码下载
每日前端实战系列的全部源代码请从 github 下载:
https://github.com/comehope/front-end-daily-challenges
代码解读
定义 dom,容器中包含 2 个元素,分别代表怪兽的身体和眼睛:
<div class="monster">
<span class="body"></span>
<span class="eyes"></span>
</div>
设置背景色:
body {
margin: 0;
height: 100vh;
background-color: black;
}
设置前景色:
.monster {
width: 100vw;
height: 50vh;
background-color: lightcyan;
}
画出怪兽的身体:
.monster {
position: relative;
}
.body {
position: absolute;
width: 32vmin;
height: 32vmin;
background-color: teal;
border-radius: 43% 40% 43% 40%;
bottom: calc(-1 * 32vmin / 2 - 4vmin);
}
定义怪兽眼睛所在的容器:
.eyes {
width: 24vmin;
height: 5vmin;
position: absolute;
bottom: 2vmin;
left: calc(32vmin - 24vmin - 2vmin);
}
用伪元素画出怪兽的眼睛:
.eyes::before,
.eyes::after {
content: '';
position: absolute;
width: 5vmin;
height: 5vmin;
border: 1.25vmin solid white;
box-sizing: border-box;
border-radius: 50%;
}
.eyes::before {
left: 4vmin;
}
.eyes::after {
right: 4vmin;
}
为怪兽定义轻轻跳起的动画,结合后面的动画效果,让它具有果冻的弹性:
.body {
animation:
bounce 1s infinite alternate;
}
@keyframes bounce {
to {
bottom: calc(-1 * 32vmin / 2 - 2vmin);
}
}
让怪兽的身体转动起来:
@keyframes wave {
to {
transform: rotate(360deg);
}
}
让怪兽徘徊行走:
.monster {
overflow: hidden;
}
.body {
left: -2vmin;
animation:
wander 5s linear infinite alternate,
wave 3s linear infinite,
bounce 1s infinite alternate;
}
.eyes {
animation: wander 5s linear infinite alternate;
}
@keyframes wander {
to {
left: calc(100% - 32vmin + 2vmin);
}
}
最后,让怪兽的眼睛眨一眨:
.eyes::before,
.eyes::after {
animation: blink 3s infinite linear;
}
@keyframes blink {
4%, 10%, 34%, 40% {
transform: scaleY(1);
}
7%, 37% {
transform: scaleY(0);
}
}
大功告成!
原文地址:https://segmentfault.com/a/1190000015484852
最新文章
- for循环递归树
- Eclipse右下角一直提示Computing additional info解决办法
- php的Excel相关操作
- epoll的使用
- Elasticsearch从0.90到1.2的不兼容变化-系统和设置
- Java API ——包装类
- linux vmstat使用说明
- JAVA FILE or I/O学习 - File学习
- Javascript常用函数收集(不定期更新)
- javascript alert,confirm,prompt弹框用法
- Zip操作的工具类
- Eclipse开发前,常用设置
- [Python] Python 学习 - 可视化数据操作(一)
- C#控件——批量化隐藏或显示同类型控件
- Oracle 11g安装步骤以及Oracle11g创建表空间和用户,并授权
- 确保你想要修改的char*是可以修改的
- bzoj5016 一个简单的询问
- 第三方网站返回hybrid app H5页面缓存问题应对策略
- Kubernetes集群的安装部署
- aspnetcore 认证相关类简要说明二
热门文章
- hibernate的.hbm.xml文件文件配置属性详解
- 基于ZooKeeper的服务注册中心
- local unversioned, incoming add upon update问题
- Storm-源码分析-Topology Submit-Supervisor
- Python中的推导式(列表推导式、字典推导式、集合推导式)
- 用Python爬虫爬取广州大学教务系统的成绩(内网访问)
- vue中获取客户端IP地址(不需要额外引入三方文件)
- git学习------>从SVN迁移到Git之后,项目开发代码继续在SVN提交,如何同步迁移之后继续在SVN提交的代码到Git?
- Python3.6编译安装以及python开发之virtualenv与virtualenvwrapper
- 具体解释linux下的串口通讯开发