【前端|CSS】动态边框按钮

离职了,闲来无事,写个边框旋转按钮,打打牙祭。

效果:

代码


<!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>Document</title>
</head>

<body>
  <div class="container">
    <button>边框按钮</button>
  </div>
</body>

</html>

<style>
  body {
    background-color: #000000;
    text-align: center;
  }

  button {
    width: 120px;
    height: 40px;
    position: relative;
    overflow: hidden;
    margin: 100px 0;
    background-color: #000;
    color: #0ebeff;
    border-radius: 10px;
    outline: none;
    border: none;
    z-index: 1;
  }

  button::before {
    content: "";
    position: absolute;
    background-color: #0ebeff;
    width: 200%;
    height: 200%;
    left: 50%;
    top: 50%;
    transform-origin: 0 0;
    animation: rotate 3s linear infinite;
    z-index: -2;
  }

  button::after {
    content: "";
    position: absolute;
    background-color: #000;
    border-radius: 10px;
    width: calc(100% - 2px);
    height: calc(100% - 2px);
    left: 1px;
    top: 1px;
    z-index: -1;
  }


  @keyframes rotate {
    to {
      transform: rotate(1turn);
    }
  }
</style>

思路

  1. 在按钮内放置一个元素宽高为父元素的200%,左上角定位到按钮的中心点,然后为这个元素设置旋转动画(此元素为A)

  1. 在按钮内放入第二个元素,层级在A元素的上层,设置该元素距离按钮各个边的间隙为1px

  1. 这样A元素旋转正好绕着整个按钮的边框,旋转的可视线条1px

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好喝的西北风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值