资讯专栏INFORMATION COLUMN

前端每日实战:119# 视频演示如何用纯 CSS 创作一个接扎啤的动画(内含2个视频)

Cristic / 941人阅读

摘要:可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。第部分第部分源代码下载每日前端实战系列的全部源代码请从下载代码解读定义,容器中包含一个表示酒桶的元素和表示啤酒杯的元素。

效果预览

按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

https://codepen.io/comehope/pen/rZeOQp

可交互视频

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

请用 chrome, safari, edge 打开观看。

第 1 部分:
https://scrimba.com/p/pEgDAM/c86mdUZ

第 2 部分:
https://scrimba.com/p/pEgDAM/cmVNbTW

源代码下载

每日前端实战系列的全部源代码请从 github 下载:

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器中包含一个表示酒桶的 .keg 元素和表示啤酒杯的 .glass 元素。酒桶有 2 个子元素,.handle 表示把手,.pipe 表示出水管,酒杯有 1 个表示啤酒的子元素 .beer

</>复制代码

居中显示:

</>复制代码

  1. body {
  2. margin: 0;
  3. height: 100vh;
  4. display: flex;
  5. justify-content: center;
  6. background: linear-gradient(
  7. lightslategray 300px,
  8. #333 300px
  9. );
  10. }

定义容器尺寸和伪元素的共有属性:

</>复制代码

  1. .container {
  2. width: 700px;
  3. height: 300px;
  4. position: relative;
  5. }
  6. .container *::before,
  7. .container *::after {
  8. content: "";
  9. position: absolute;
  10. }

画出酒桶:

</>复制代码

  1. .keg {
  2. position: absolute;
  3. width: 90px;
  4. height: 200px;
  5. background: linear-gradient(
  6. to right,
  7. #777 70px,
  8. #555 70px
  9. );
  10. bottom: 0;
  11. left: 310px;
  12. }

画出出水管和它的支架:

</>复制代码

  1. .keg .pipe {
  2. position: absolute;
  3. width: 10px;
  4. height: 40px;
  5. background-color: #ccc;
  6. top: 33px;
  7. left: 10px;
  8. }
  9. .keg .pipe::before {
  10. width: 40px;
  11. height: 20px;
  12. background:
  13. radial-gradient(
  14. circle at 10px 10px,
  15. #eee 7px,
  16. #ccc 7px, #ccc 10px,
  17. transparent 10px
  18. ),
  19. linear-gradient(
  20. #ccc 50%,
  21. #999 50%
  22. );
  23. border-radius: 10px;
  24. top: -2px;
  25. left: -5px;
  26. }

画出把手:

</>复制代码

  1. .keg .handle {
  2. position: absolute;
  3. border-style: solid;
  4. border-width: 50px 10px 0 10px;
  5. border-color: black transparent transparent transparent;
  6. top: -10px;
  7. left: 5px;
  8. }
  9. .keg .handle::before {
  10. width: 20px;
  11. height: 10px;
  12. background-color: #ccc;
  13. top: -60px;
  14. left: -10px;
  15. border-radius: 5px 5px 0 0;
  16. }
  17. .keg .handle::after {
  18. width: 10px;
  19. height: 20px;
  20. background-color: #ccc;
  21. top: -20px;
  22. left: -5px;
  23. }

画出酒杯:

</>复制代码

  1. .glass {
  2. position: absolute;
  3. width: 70px;
  4. height: 100px;
  5. color: rgba(255, 255, 255, 0.3);
  6. background-color: currentColor;
  7. bottom: 0;
  8. left: 300px;
  9. border-radius: 5px;
  10. }
  11. .glass::before {
  12. width: 50px;
  13. height: 40px;
  14. border: 10px solid;
  15. top: 20px;
  16. right: -20px;
  17. border-radius: 0 40% 40% 0;
  18. clip-path: inset(0 0 0 72%);
  19. }

画出杯中的啤酒和泡沫:

</>复制代码

  1. .beer {
  2. position: absolute;
  3. width: 60px;
  4. height: 80px;
  5. background-color: rgba(255, 206, 84, 0.8);
  6. bottom: 15px;
  7. left: 5px;
  8. border-radius: 0 0 5px 5px;
  9. border-top: solid rgba(255, 206, 84, 0.8);
  10. }
  11. .beer::before {
  12. width: inherit;
  13. height: 15px;
  14. background-color: #eee;
  15. top: -15px;
  16. border-radius: 5px 5px 0 0;
  17. }

接下来制作动画。

增加酒杯把手被压下的动画效果:

</>复制代码

  1. .keg .handle {
  2. transform-origin: center 50px;
  3. animation: handle 5s infinite;
  4. }
  5. @keyframes handle {
  6. 10%, 60% {
  7. transform: rotate(0deg);
  8. }
  9. 20%, 50% {
  10. transform: rotate(-90deg);
  11. }
  12. }

增加啤酒被斟满的动画效果:

</>复制代码

  1. .beer {
  2. animation: fillup 5s infinite;
  3. }
  4. @keyframes fillup {
  5. 0%, 20% {
  6. height: 0px;
  7. border-width: 0px;
  8. }
  9. 40% {
  10. height: 40px;
  11. }
  12. 80%, 100% {
  13. height: 80px;
  14. border-width: 5px;
  15. }
  16. }

增加啤酒泡沫泛起的动画效果:

</>复制代码

  1. .beer::before {
  2. animation:
  3. wave 0.5s infinite alternate,
  4. fillup-foam 5s linear infinite;
  5. }
  6. @keyframes fillup-foam {
  7. 0%, 20% {
  8. top: 0;
  9. height: 0;
  10. }
  11. 60%, 100% {
  12. top: -15px;
  13. height: 15px;
  14. }
  15. }
  16. @keyframes wave {
  17. from {
  18. transform: skewY(-3deg);
  19. }
  20. to {
  21. transform: skewY(3deg);
  22. }
  23. }

增加啤酒从出水口流出的效果:

</>复制代码

  1. .keg .pipe::after {
  2. width: 10px;
  3. background-color: rgba(255, 206, 84, 0.5);
  4. animation: flow 5s infinite;
  5. }
  6. @keyframes flow {
  7. 0%, 15% {
  8. top: 40px;
  9. height: 0;
  10. }
  11. 20% {
  12. height: 115px;
  13. }
  14. 40% {
  15. height: 75px;
  16. }
  17. 55% {
  18. top: 40px;
  19. height: 50px;
  20. }
  21. 60%, 100% {
  22. top: 80px;
  23. height: 0;
  24. }
  25. }

最后,增加酒杯滑动的效果:

</>复制代码

  1. .glass {
  2. animation: slide 5s ease infinite;
  3. }
  4. @keyframes slide {
  5. 0% {
  6. left: 0;
  7. filter: opacity(0);
  8. }
  9. 20%, 80% {
  10. left: 300px;
  11. filter: opacity(1);
  12. }
  13. 100% {
  14. left: 600px;
  15. filter: opacity(0);
  16. }
  17. }

大功告成!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/113798.html

相关文章

  • 前端每日实战119# 视频演示何用 CSS 创作扎啤动画内含2视频

    摘要:可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。第部分第部分源代码下载每日前端实战系列的全部源代码请从下载代码解读定义,容器中包含一个表示酒桶的元素和表示啤酒杯的元素。 showImg(https://segmentfault.com/img/bVbfXIy?w=400&h=300); 效果预览 按下右侧的点击预览按钮可以在当前页面预览,点击链接可以全屏预览。 ht...

    cpupro 评论0 收藏0
  • 前端每日实战 2018年10月至2019年6月项目汇总(共 20 项目)

    摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月至年月发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读, 过往项目 2018 年 9 月份项目汇总(共 26 个项目) 2018 年 8 月份项目汇总(共 29 个项目) 2018 年 7 月份项目汇总(...

    muddyway 评论0 收藏0
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 项目)

    摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份项目汇总共个项目年月份发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读,是学习前端开发的活的参考书视频演示如何用纯创作一台咖啡机视频演示如何用 过往项目 2018 年 6 月份项目汇总(共 27 个项目) 2018 年 5 月份项目汇总(共 30 个项目) 2018 年 4 月份项目汇总(...

    ghnor 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<