纯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>
相关推荐
幸运小圣几秒前
全面解析 Web 核心性能指标:LCP、INP、CLS 是什么、怎么用、怎么看
前端
如果超人不会飞5 分钟前
TinyRobot SuggestionPopover智能建议弹出框组件
前端·vue.js
LiuJun2Son20 分钟前
Angular 快速入门:从零搭建你的第一个应用
前端·javascript·angular.js
小徐_233328 分钟前
Wot UI 2.1.0 发布:ConfigProvider 全局配置能力升级
前端·uni-app
方白羽29 分钟前
Vibe Coding 四个核心阶段
android·前端·app
奶油话梅糖29 分钟前
浏览器解析 HTML 头部的底层逻辑:从字节流到资源调度
前端·html
YHL30 分钟前
🚀从零理解树与二叉树 —— 概念、实现与遍历
前端·javascript·数据结构
小时前端31 分钟前
微前端技术选型深度分析:从概念到实践
前端
wyhwust1 小时前
基于Apifox的接口管理工具
前端
柒和远方1 小时前
后端认证、鉴权、高并发:从 Session 到 JWT 再到 Redis
前端·后端·面试