美文网首页CSS
CSS 实现灯笼动画,祝大家元旦快乐

CSS 实现灯笼动画,祝大家元旦快乐

作者: 苏苏哇哈哈 | 来源:发表于2022-12-30 00:04 被阅读0次

前言

👏CSS 实现大红灯笼动画,祝大家元旦快乐,2023越来越棒!速速来Get吧~

🥇文末分享源代码。记得点赞+关注+收藏!

1.实现效果

在这里插入图片描述

2.实现步骤

  • 定义一个灯笼的背景色bg,线条颜色lineColor
:root {
  --lineColor: #ecaa2f;
  --bg: #f00;
}
  • 父容器container宽高设置为200px/150px
.container {
  width: 200px;
  height: 150px;
  position: relative;
}
  • 在父容器内添加一个圆角矩形,宽高为父容器一致,设置一定的圆角与box-shadow阴影,背景色为 bg
在这里插入图片描述
<div class="container">
 <div class="center"></div>
</div>
.center {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--bg);
  border-radius: 120px;
  box-shadow: 0 0 80px -10px var(--bg);
}
  • 在center中添加一个标签,宽高与父容器一致,在其前后伪元素上绘制灯笼上的线条
<div class="center">
  <div class="center-line"></div>
 </div>
.center-line {
   width: 100%;
   height: 100%;
 }
  • 在center-line的前伪元素上添加一个圆环,高度与父容器一致,宽度为150px;边框设置为2px,颜色为lineColor,距离顶部为0,水平居中
在这里插入图片描述
.center-line::before {
 content: "";
 position: absolute;
 top: 0;
 left: calc(50% - 75px);
 width: 150px;
 height: 150px;
 border: 2px solid var(--lineColor);
 border-radius: 50%;
}
  • 在center-line的后伪元素上添加一个圆环,高度与父容器一致,宽度为70px;边框设置为2px,颜色为lineColor,距离顶部为0,水平居中
在这里插入图片描述
.center-line::after {
  content: "";
  position: absolute;
  top: 0;
  left: calc(50% - 35px);
  width: 70px;
  height: 150px;
  border: 2px solid var(--lineColor);
  border-radius: 50%;
}
  • 在center-line中添加span标签,内容为"元旦快乐",设置宽度与字体的font-size相同,水平垂直居中,字体颜色为lineColor
在这里插入图片描述
<div class="center">
  <div class="center-line"><span>元旦快乐</span></div>
</div>
.center-line span {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
 width: 18px;
 font-size: 18px;
 color: var(--lineColor);
 font-weight: bold;
}
  • 为center容器添加前伪元素,宽高为80px/10px,背景色为lineColor,设置左上右上的一定的圆角弧度,水平居中,top设置为-8px,层级设置为2,居于最顶层
在这里插入图片描述
 .center::before {
  content: "";
  position: absolute;
  top: -8px;
  left: calc(50% - 40px);
  width: 80px;
  height: 10px;
  background: var(--lineColor);
  border-radius: 5px 5px 0 0;
  z-index: 2;
}
  • 为center容器添加后伪元素,宽高为80px/10px,背景色为lineColor,设置左下右下的一定的圆角弧度,水平居中,bottom设置为-8px,层级设置为2,居于最顶层
在这里插入图片描述
.center::after {
 content: "";
 width: 80px;
 height: 10px;
 background: var(--lineColor);
 border-radius: 0 0 5px 5px;
 position: absolute;
 bottom: -8px;
 left: calc(50% - 40px);
 z-index: 2;
}

  • 在最外层的父容器container中添加一个标签,用来绘制灯笼最上面的线条
在这里插入图片描述
<div class="container">
 + <span class="head-line"></span>
</div>
.head-line {
 position: absolute;
 left: calc(50% - 2px);
 top: -60px;
 width: 4px;
 height: 60px;
 background-color: var(--lineColor);
}
  • 在center中添加一个标签,用来绘制灯笼下面的线条
在这里插入图片描述
 <div class="center">
   + <span class="footer-line"></span>
 </div>
.footer-line {
 position: absolute;
 left: calc(50% - 2px);
 bottom: -50px;
 width: 4px;
 height: 50px;
 background-color: var(--lineColor);
 z-index: -1;
}

  • 为footer-line添加一个伪元素,用来绘制灯笼最下面的细须,用linear-gradient设置背景色,这样一个灯笼就绘制完成了,接下来我们给灯笼添加上动画效果
在这里插入图片描述
.footer-line::after {
 content: "";
 position: absolute;
 bottom: -75px;
 left: calc(50% - 8px);
 width: 16px;
 height: 80px;
 background: linear-gradient(
   #f00,
   #e36d00 3px,
   #fbd342 5px,
   #e36d00 8px,
   #e36d00 12px,
   #f00 16px,
   rgba(255, 0, 0, 0.8) 26px,
   rgba(255, 0, 0, 0.6)
 );
 border-radius: 5px 5px 0 0;
}
  • 添加rotate动画,动画的0%和100%旋转-10deg,50%的时候旋转10deg
@keyframes rotate {
  0%,
  100% {
    transform: rotate(-10deg);
  }
  50% {
    transform: rotate(10deg);
  }
}
  • 为灯笼底部的细须添加旋转动画
在这里插入图片描述
.footer-line {
 + animation: rotate 3s infinite ease-in-out;
}
  • 为灯笼的中间区域center添加旋转动画
在这里插入图片描述
.center {
 + animation: rotate 3s infinite ease-in-out;
}
  • 可以看到,灯笼中间与上方线条之间摆动的幅度太大,为center设置transform-origin
在这里插入图片描述
.center {
 + transform-origin: top center;
}
  • 为最外层父容器添加旋转动画,就实现好了啦~
在这里插入图片描述
.container {
  +  animation: rotate 3s infinite ease-in-out;
}

3.实现代码

<!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" />
    <title>CSS 实现灯笼动画</title>
  </head>
  <link rel="stylesheet" href="../common.css" />
  <style>
    :root {
      --lineColor: #ecaa2f;
      --bg: #f00;
    }
    .container {
      width: 200px;
      height: 150px;
      position: relative;
      animation: rotate 3s infinite ease-in-out;
    }
    .center {
      position: relative;
      width: 100%;
      height: 100%;
      background: var(--bg);
      border-radius: 120px;
      box-shadow: 0 0 80px -10px var(--bg);
      animation: rotate 3s infinite ease-in-out;
      transform-origin: top center;
    }
    .center::before {
      content: "";
      position: absolute;
      top: -8px;
      left: calc(50% - 40px);
      width: 80px;
      height: 10px;
      background: var(--lineColor);
      border-radius: 5px 5px 0 0;
      z-index: 2;
    }
    .center::after {
      content: "";
      width: 80px;
      height: 10px;
      background: var(--lineColor);
      border-radius: 0 0 5px 5px;
      position: absolute;
      bottom: -8px;
      left: calc(50% - 40px);
      z-index: 2;
    }
    .center-line {
      width: 100%;
      height: 100%;
    }
    .center-line span {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 18px;
      font-size: 18px;
      color: var(--lineColor);
      font-weight: bold;
    }
    .center-line::before {
      content: "";
      position: absolute;
      top: 0;
      left: calc(50% - 75px);
      width: 150px;
      height: 150px;
      border: 2px solid var(--lineColor);
      border-radius: 50%;
    }
    .center-line::after {
      content: "";
      position: absolute;
      top: 0;
      left: calc(50% - 35px);
      width: 70px;
      height: 150px;
      border: 2px solid var(--lineColor);
      border-radius: 50%;
    }
    .head-line {
      position: absolute;
      left: calc(50% - 2px);
      top: -60px;
      width: 4px;
      height: 60px;
      background-color: var(--lineColor);
    }
    .footer-line {
      position: absolute;
      left: calc(50% - 2px);
      bottom: -50px;
      width: 4px;
      height: 50px;
      background-color: var(--lineColor);
      animation: rotate 3s infinite ease-in-out;
    }
    .footer-line::after {
      content: "";
      position: absolute;
      bottom: -75px;
      left: calc(50% - 8px);
      width: 16px;
      height: 80px;
      background: linear-gradient(
        #f00,
        #e36d00 3px,
        #fbd342 5px,
        #e36d00 8px,
        #e36d00 12px,
        #f00 16px,
        rgba(255, 0, 0, 0.8) 26px,
        rgba(255, 0, 0, 0.6)
      );
      border-radius: 5px 5px 0 0;
    }
    @keyframes rotate {
      0%,
      100% {
        transform: rotate(-10deg);
      }
      50% {
        transform: rotate(10deg);
      }
    }
  </style>
  <body>
    <div class="container">
      <span class="head-line"></span>
      <div class="center">
        <div class="center-line">
          <span>元旦快乐</span>
        </div>
        <span class="footer-line"></span>
      </div>
    </div>
  </body>
</html>

4.写在最后🍒

🍡提前祝大家元旦快乐,2023越来越棒!🍪
看完本文如果觉得对你有一丢丢帮助,记得点赞+关注+收藏鸭 🍕
更多相关内容,关注🍥苏苏的bug,🍡苏苏的github,🍪苏苏的码云~

相关文章

  • CSS 实现灯笼动画,祝大家元旦快乐

    前言 ?CSS 实现大红灯笼动画,祝大家元旦快乐,2023越来越棒!速速来Get吧~ ?文末分享源代码。记得点赞+...

  • 2022-01-01 元旦快乐!

    今天,元旦,祝大家元旦快乐!

  • 2019.01.01

    今天元旦,祝大家元旦快乐!

  • 祝大家元旦快乐

    感谢2017大家的喜欢和支持,欢迎大家在2018年继续关注和支持我。 2018年写点啥呢?欢迎大家提出宝贵的意见和建议。

  • 祝大家元旦快乐

    2016年即将过去,新的一年就要到来。 蒲公英希望大家在新的一年,在回顾过去中再次起航,一起奔向新的追求。在新的一...

  • 祝大家元旦快乐

    元旦由来 晋代诗人辛兰曾有《元正》诗:“元正启令节,嘉庆肇自兹。咸奏万年觞,小大同悦熙。”记述元旦庆贺情景。现代中...

  • 祝大家元旦快乐

    记录最美的瞬间 2019 幸福 快乐 九九

  • 祝大家元旦快乐!

  • 祝大家,元旦快乐!

    今天学校办的游园会像极了家里边过年时的灯火通明,没错,我想家,我想我爸妈了…… 我们院很奇葩,搞来了真的鸭子,鸭子...

  • 祝大家元旦快乐

    带着爱和希望, 重新开始。 相信未来。 经历,是财富,也是负担。 轻装前行吧。

网友评论

    本文标题:CSS 实现灯笼动画,祝大家元旦快乐

    本文链接:https://www.haomeiwen.com/subject/tvfxcdtx.html