三十的博客

边框动画

发布时间
最后更新
阅读量 加载中...

效果 1

此演示仅在桌面端浏览器中可用

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>wanglei.live</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div class="wrap">
      <div class="box">
        <h1>Border Animation</h1>
        <span class="border-top"></span>
        <span class="border-right"></span>
        <span class="border-bottom"></span>
        <span class="border-left"></span>
      </div>
    </div>
  </body>
</html>
css
.wrap {
  width: 400px;
  height: 200px;
  margin: 100px auto;
}

.box {
  position: relative;
  padding: 20px 35px;
  overflow: hidden;
  border-radius: 5px;
}

.border-top {
  position: absolute;
  top: 0;
  right: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(to right, #ec107e, #fcc23f);
  animation: animate-border-top 2s linear infinite;
}

h1 {
  text-align: center;
}

@keyframes animate-border-top {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

.border-right {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  width: 3px;
  opacity: 0;
  background: linear-gradient(to bottom, #ec107e, #fcc23f);
  animation: animate-border-right 2s linear infinite;
  animation-delay: 1s;
}

@keyframes animate-border-right {
  0% {
    opacity: 0;
    transform: translateY(-100%);
  }
  0.1% {
    opacity: 1;
  }
  100% {
    opacity: 1;
    transform: translateY(100%);
  }
}

.border-bottom {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: linear-gradient(to left, #ec107e, #fcc23f);
  animation: animate-border-bottom 2s linear infinite;
}

@keyframes animate-border-bottom {
  0% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(-100%);
  }
}

.border-left {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 3px;
  opacity: 0;
  background: linear-gradient(to top, #ec107e, #fcc23f);
  animation: animate-border-left 2s linear infinite;
  animation-delay: 1s;
}

@keyframes animate-border-left {
  0% {
    opacity: 0;
    transform: translateY(100%);
  }
  0.1% {
    opacity: 1;
  }
  100% {
    opacity: 1;
    transform: translateY(-100%);
  }
}

效果 2

此演示仅在桌面端浏览器中可用

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>wanglei.live</title>
    <style>
      a {
        text-decoration: none;
        color: white;
      }

      .btn {
        position: relative;
        display: block;
        width: 140px;
        height: 60px;
        margin: 100px;
        padding: 3px;
        border-radius: 6px;
        text-align: center;
        line-height: 60px;
        overflow: hidden;
      }

      .btn::before {
        position: absolute;
        top: 0;
        left: 0;
        content: "";
        width: 200%;
        height: 100%;
        background: linear-gradient(
          115deg,
          #4fcf70,
          #fad648,
          #a767e5,
          #12bcfe,
          #44ce7b
        );
        background-size: 50% 100%;
        animation: move 0.75s linear infinite;
      }

      @keyframes move {
        to {
          transform: translateX(-50%);
        }
      }

      .btn span {
        position: relative;
        display: block;
        width: 100%;
        height: 100%;
        border-radius: 5px;
        background-color: black;
      }
    </style>
  </head>
  <body>
    <a href="#" class="btn">
      <span>点击开始</span>
    </a>
  </body>
</html>

效果 3

此演示仅在桌面端浏览器中可用

html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>wanglei.live</title>
    <style>
      .btn {
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 300px;
        height: 200px;
        margin: 50px;
        border-radius: 10px;
        color: #fff;
        font-size: 24px;
        font-weight: bold;
        text-align: center;
        line-height: 200px;
        overflow: hidden;
      }

      .btn::before {
        content: "";
        position: absolute;
        z-index: -1;
        width: 500px;
        height: 500px;
        background: conic-gradient(
          transparent,
          transparent,
          transparent,
          #00ccff
        );
        animation: rotate 1s linear infinite;
      }

      .btn::after {
        content: "";
        position: absolute;
        z-index: -1;
        inset: 3px;
        background-color: black;
        border-radius: 10px;
      }

      @keyframes rotate {
        to {
          transform: rotate(360deg);
        }
      }
    </style>
  </head>
  <body>
    <div class="btn">点击开始</div>
  </body>
</html>
#Css #动画