悬停上的按钮边框可在其中移动文本

时间:2019-04-20 16:49:41

标签: html css css-animations keyframe

我创建了一个类似边框的关键帧CSS样式。当我将鼠标悬停在按钮上时,类似边框的动画应该从右上开始到左上,然后到左下,然后再到右下,最后再到右上。当我将鼠标悬停在按钮上时,先前的序列应该发生并且已经创建。然而;悬停时,按钮内的文本会移动,这会使按钮看起来很奇怪。

我查看了此question的答案,但由于我未在悬停时使用边框样式,因此不适用于我的情况。相反,我更改了三个跨度而不是边框​​的背景颜色,宽度和高度。

如何使用动画创建方法来防止这种抖动?

CodePen:https://codepen.io/Tes3awy/pen/ZZRpBW

HTML

<div class="wrapper">
      <a class="custom-btn" href="https://mince.34way.com/about/" title="About">
        About Us
        <span class="border-top"></span>
        <span class="border-right"></span>
        <span class="border-bottom"></span>
        <span class="border-left"></span>
      </a>
    </div>

CSS

body {
  position: relative;
  height: 100vh;
}

.wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.custom-btn {
  position: relative;
  width: 183px;
  height: 55px;
  line-height: 55px;
  display: inline-block;
  text-align: center;
  text-transform: uppercase;
  margin: 0 auto;
  border: 2px solid #77a942;
  color: #77a942;
  text-decoration: none;
}

span[class^="border-"] {
  opacity: 0;
}

.border-top {
  position: absolute;
  top: -2px;
  right: 0;
  width: 100%;
  height: 3px;
  background-color: transparent;
}

.border-left {
  position: absolute;
  top: 0;
  left: -2px;
  width: 3px;
  height: 100%;
  background-color: transparent;
}

.border-bottom {
  position: absolute;
  left: 0;
  bottom: -2px;
  width: 100%;
  height: 3px;
  background-color: transparent;
}

.border-right {
  position: absolute;
  bottom: 0;
  right: -2px;
  width: 3px;
  height: 100%;
  background-color: transparent;
}

.custom-btn:hover .border-top {
  animation: animateTop .2s 1 alternate ease forwards;
}

.custom-btn:hover .border-left {
  animation: animateLeft .2s 1 alternate ease forwards;
  animation-delay: .2s;
}

.custom-btn:hover .border-bottom {
  animation: animateBottom .2s 1 alternate ease forwards;
  animation-delay: .4s;
}

.custom-btn:hover .border-right {
  animation: animateRight .2s 1 alternate ease forwards;
  animation-delay: .6s;
}

@keyframes animateTop {
  0% {
    width: 0;
    height: 0;
    opacity: 0;
    background-color: #77a942;
  }
  50% {
    width: 50%;
    height: 3px;
    opacity: 1;
    background-color: #77a942;
  }
  100% {
    width: 100%;
    height: 3px;
    opacity: 1;
    background-color: #77a942;
  }
}

@keyframes animateLeft {
  0% {
    width: 0;
    height: 0;
    opacity: 0;
    background-color: #77a942;
  }
  50% {
    width: 3px;
    height: 50%;
    opacity: 1;
    background-color: #77a942;
  }
  100% {
    width: 3px;
    height: 100%;
    opacity: 1;
    background-color: #77a942;
  }
}

@keyframes animateBottom {
  0% {
    width: 0;
    height: 0;
    opacity: 0;
    background-color:#77a942;
  }
  50% {
    width: 50%;
    height: 3px;
    opacity: 1;
    background-color:#77a942;
  }
  100% {
    width: 100%;
    height: 3px;
    opacity: 1;
    background-color:#77a942;
  }
}

@keyframes animateRight {
  0% {
    width: 0;
    height: 0;
    opacity: 0;
    background-color: #77a942;
  }
  50% {
    width: 3px;
    height: 50%;
    opacity: 1;
    background-color: #77a942;
  }
  100% {
    width: 3px;
    height: 100%;
    opacity: 1;
    background-color: #77a942;
  }
}

1 个答案:

答案 0 :(得分:0)

当您将事物翻译50%时,它们最终可能会介于像素之间。使用过渡时,CSS倾向于改变其舍入到哪个像素的想法。尝试确保将文本居中的按钮的高度/宽度使CSS具有确定的位置,当您将其除以一半时可以放置该位置。