【问题标题】:Button border on hover moves the text inside悬停时的按钮边框将文本移动到里面
【发布时间】:2019-04-21 05:57:43
【问题描述】:

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

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

如何通过创建动画的方法防止这种抖动?

代码笔: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;
  }
}

【问题讨论】:

  • 可能是我,但我看不到里面的文字有任何抖动
  • 我的也不行——我用的是 Chrome。
  • @kukkuz 你试过 CodePen 上的代码了吗?如果是,请尝试放大到 125% 看看是否抖动?
  • @Tes3awy 我放大了一点,是的,我看到了一点点震动......
  • @kukkuz 我怎样才能避免这种微不足道的震动?

标签: html css css-animations keyframe


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-05-19
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多