纯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>
相关推荐
大福ya7 分钟前
AI开源项目改造NextChat(ChatGPT-Next-Web)实现前端SSR改造打造一个初始框架
前端·chatgpt·前端框架·开源·aigc·reactjs·ai编程
n***333519 分钟前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
纯粹的热爱40 分钟前
🌐 阿里云 Linux 服务器 Let's Encrypt 免费 SSL 证书完整部署指南
前端
北辰alk42 分钟前
Vue3 自定义指令深度解析:从基础到高级应用的完整指南
前端·vue.js
重铸码农荣光1 小时前
从 0 到 1 实现 LocalStorage 待办清单:CSS 进阶 + 前端工程化思想实践
css·前端工程化
小熊哥7221 小时前
谈谈最进学习(低延迟)直播项目的坎坷与收获
前端
用户89225411829011 小时前
游戏框架文档
前端
Holin_浩霖1 小时前
mini-react 实现function 组件
前端
Yanni4Night1 小时前
JS 引擎赛道中的 Rust 角色
前端·javascript
欧阳的棉花糖1 小时前
纯Monorepo vs 混合式Monorepo
前端·架构