圆形按钮边框长度动画

luk*_*s23 5 css

我有这个按钮边框动画,当按钮不圆角时可以正常工作。如果按钮的边框半径为 9px,如何才能达到相同的效果。我无法让动画在不打破右边框的情况下正确工作。

在此输入图像描述

body {
  font-family: sans-serif;
  background-color: black;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.button {
  position: relative;
  width: 200px;
  height: 40px;
  background-color: #083a65;
  border: none;
  color: white;
  font-size: 18px;
  border-radius: 10px;
}

.button::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  border-radius: 10px;
  border-top: 1px solid yellow;
  border-right: 1px solid yellow;
  animation: border-top-right-animation 2s infinite;
}

.button::after {
  content: "";
  position: absolute;
  right: 0px;
  bottom: 0px;
  width: 0;
  height: 0;
  border-radius: 10px;
  border-left: 1px solid yellow;
  border-bottom: 1px solid yellow;
  animation: border-bottom-left-animation 2s infinite;
}

@keyframes border-top-right-animation {
  0% {
    width: 0px;
    height: 0px;
  }
  25% {
    width: 100%;
    height: 0px;
  }
  50% {
    width: 100%;
    height: 100%;
  }
  100% {
    width: 100%;
    height: 100%;
  }
}

@keyframes border-bottom-left-animation {
  0% {
    width: 0px;
    height: 0px;
    opacity: 0;
  }
  50% {
    width: 0px;
    height: 0px;
    opacity: 0;
  }
  50.1% {
    width: 0px;
    height: 0px;
    opacity: 1;
  }
  75% {
    width: 100%;
    height: 0px;
    opacity: 1;
  }
  100% {
    width: 100%;
    height: 100%;
    opacity: 1;
  }
}
Run Code Online (Sandbox Code Playgroud)
<button class="button">My button</button>
Run Code Online (Sandbox Code Playgroud)

luk*_*s23 6

所以我终于设法解决了它。我希望这可以帮助别人。我为所有 4 条线制作了 4 个不同的跨度,对它们的宽度/高度进行了动画处理,并使按钮的内容不同元素绝对定位。

这是代码:

body {
  margin: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(0, 0, 0, 0.9);
}

.button {
  position: relative;
  width: 500px;
  height: 100px;
  background-color: #083a65;
  border: none;
  color: white;
  font-size: 18px;
  border-radius: 10px;
  overflow: hidden;
}

.button .text {
  position: absolute;
  width: calc(100% - 4px);
  height: calc(100% - 4px);
  top: 2px;
  left: 2px;
  background-color: #083a65;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 9px;
}

.button .top {
  position: absolute;
  left: 5px;
  top: 0px;
  width: 100%;
  height: 2px;
  background-color: yellow;
  animation: animate-top 1500ms linear;
}

.button .top::after {
  content: "";
  position: absolute;
  top: 0;
  right: 5px;
  width: 10px;
  height: 10px;
  background-color: yellow;
}

.button .right {
  position: absolute;
  right: 0;
  top: -5px;
  width: 2px;
  height: 100%;
  background-color: yellow;
  animation: animate-right 1500ms linear;
}

.button .right::before {
  content: "";
  position: absolute;
  bottom: -5px;
  right: 0;
  width: 10px;
  height: 10px;
  background-color: yellow;
}

.button .bottom {
  position: absolute;
  right: 0;
  bottom: 0px;
  width: 100%;
  height: 2px;
  background-color: yellow;
  animation: animate-bottom 1500ms linear;
}

.button .bottom::before {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 10px;
  height: 10px;
  background-color: yellow;
}

.button .left {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 2px;
  height: 100%;
  background-color: yellow;
  animation: animate-left 1500ms linear;
}

.button .left::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 10px;
  height: 10px;
  background-color: yellow;
}

@keyframes animate-top {
  0% {
    width: 0px;
  }
  25% {
    width: 100%;
  }
  50% {
    width: 100%;
  }
  75% {
    width: 100%;
  }
  100% {
    width: 100%;
  }
}

@keyframes animate-right {
  0% {
    height: 0;
  }
  25% {
    height: 0;
  }
  50% {
    height: 100%;
  }
  75% {
    height: 100%;
  }
  100% {
    height: 100%;
  }
}

@keyframes animate-bottom {
  0% {
    width: 0;
  }
  25% {
    width: 0;
  }
  50% {
    width: 0;
  }
  75% {
    width: 100%;
  }
  100% {
    width: 100%;
  }
}

@keyframes animate-left {
  0% {
    height: 0;
  }
  25% {
    height: 0;
  }
  50% {
    height: 0;
  }
  75% {
    height: 0;
  }
  100% {
    height: 100%;
  }
}
Run Code Online (Sandbox Code Playgroud)
 <button class="button">
   <span class="top"></span>
   <span class="right"></span>
   <span class="bottom"></span>
   <span class="left"></span>
   <span class="text">Animated button</span>
 </button>
Run Code Online (Sandbox Code Playgroud)


imh*_*ost 4

通常,形状的属性stroke-dasharray用于此类动画。像这样的东西:stroke-dashoffsetsvg

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}


body {
  font-family: sans-serif;
  background-color: black;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.button {
  position: relative;
  width: 200px;
  height: 40px;
  background-color: #083a65;
  border: 0;
  color: white;
  font-size: 18px;
  border-radius: 10px;
  cursor: pointer;
  max-width: 100%;
}

button svg {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

button rect {
  animation: button-border 2s linear both;
}

@keyframes button-border {
  to {
    stroke-dashoffset: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)
<button
  type="button"
  class="button"
>
  My button
  <svg
    viewBox="0 0 200 40"
    fill="none"
    preserveAspectRatio="none"
  >
    <rect
      vector-effect="non-scaling-stroke"
      x="0.5"
      y="0.5"
      width="199"
      height="39"
      rx="9.5"
      stroke="yellow"
      stroke-dasharray="460"
      stroke-dashoffset="460"
    />
  </svg>
</button>
Run Code Online (Sandbox Code Playgroud)