纯css:一个好玩的按钮边框动态动画

思路:使用伪元素+动画来实现

代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
  
    body {
      background: #000;
    }
  
    .wrap {
      width: 400px;
      height: 400px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  
    .btn {
      width: 80px;
      height: 30px;
      border-radius: 5px;
      color: #fff;
      text-align: center;
      position: relative;
      overflow: hidden;
    }
  
    /* 这是需要动画的一个div */
    .btn::before {
      display: inline-block;
      background: red;
      content: '';
      width: 80px;
      height: 80px;
      position: absolute;
      left: 50%;
      top: 50%;
      z-index: -2;
      transform-origin: left top;
      animation: 5s animate 0s infinite;
    }
  
    /* 这是需要把动画的div隐藏一部分的div*/
    .btn::after {
      display: inline-block;
      background: #3f3f3f;
      border-radius: 5px;
      content: '';
      --w: 2px;
      left: var(--w);
      top: var(--w);
      width: calc(100% - 2 * var(--w));
      height: calc(100% - 2 * var(--w));
      position: absolute;
      z-index: -1;
    }
  
    @keyframes animate {
      from {
        transform: rotate(0deg)
      }
  
      to {
        transform: rotate(360deg)
      }
    }
  </style>
  </head>

<body>
  <!-- 写一个按钮的边框在移动的css -->
  <div class="wrap">
    <span class="btn">Button</span>
  </div>
  </body>

</html>
相关推荐
GIS之路1 天前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide1 天前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter1 天前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸1 天前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000001 天前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉1 天前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化
兆子龙1 天前
从高阶函数到 Hooks:React 如何减轻开发者的心智负担(含 Demo + ahooks 推荐)
前端
狗胜1 天前
测试文章 - API抓取
前端
三小河1 天前
VS Code 集成 claude-code 教程:告别海外限制,无缝对接国内大模型
前端·程序员