html - 使用 HTML 和 CSS 修复按钮动画

标签 html css

我有一个用 HTML 和 CSS 制作的基本按钮,当鼠标悬停在它上面时会显示一个文本。我想纠正动画效果,这不太正确。
当光标被移除时,动画会急剧减小按钮的大小。那么也许它可以通过过渡效果来完成?
我希望我说得够清楚了!

.home-button {
  line-height: 100%;
  padding: 5px 40px 5px 5px;
  font-family: Arial;
  font-size: 12px;
  position: relative;
}

.home-button span {
  position: absolute;
  top: 20px;
  opacity: 0;
  font-weight: 600;
  color: #454b54;
}

.home-button:hover {
  animation-name: enlarge;
  animation-duration: 1s;
  animation-fill-mode: forwards;
}

.home-button:hover span {
  animation-name: appear;
  animation-duration: 1.5s;
  animation-fill-mode: forwards;
}

.home-button::before {
  margin-right: 5px;
  background-image: url(https://mcusercontent.com/ec104f3d77537e1962ab6441c/images/d7bb4928-a156-4682-9677-d0d5b47c3a21.png);
  background-size: 40px 40px;
  display: inline-block;
  width: 40px;
  height: 40px;
  border-radius: 100%;
  content: "";
}

.home-complement-button {
  background-color: #fff;
  border-radius: 100px;
  box-shadow: 0 4px 8px #dadce0;
  transition: 0.3s;
}

.home-complement-button:focus {
  background-color: #e8f0fb !important;
}

@keyframes appear {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes enlarge {
  from {
    padding-right: 40px;
  }
  to {
    padding-right: 50px;
  }
}
<div style="display:flex;user-select:none"><a class="home-button home-complement-button" href="https://esims.one/" style="-webkit-tap-highlight-color:rgba(0,0,0,0);text-decoration:none"><span>Home</span></a></div>

最佳答案

您不需要为此使用动画。只需使用过渡。这样更流畅。

.home-button {
  line-height: 100%;
  padding: 5px 40px 5px 5px;
  font-family: Arial;
  font-size: 12px;
  position: relative;
  transition: 1s;
}

.home-button span {
  position: absolute;
  top: 20px;
  opacity: 0;
  font-weight: 600;
  color: #454b54;
}

.home-button:hover {
  padding-right: 50px;
}

.home-button:hover span {
  transition: opacity 1.5s; /* I added the transition here because I want it to take 0 seconds when come back. */
  opacity: 1;
}

.home-button::before {
  margin-right: 5px;
  background-image: url(https://mcusercontent.com/ec104f3d77537e1962ab6441c/images/d7bb4928-a156-4682-9677-d0d5b47c3a21.png);
  background-size: 40px 40px;
  display: inline-block;
  width: 40px;
  height: 40px;
  border-radius: 100%;
  content: "";
}

.home-complement-button {
  background-color: #fff;
  border-radius: 100px;
  box-shadow: 0 4px 8px #dadce0;
  transition: 0.3s;
}

.home-complement-button:focus {
  background-color: #e8f0fb !important;
}
<div style="display:flex;user-select:none"><a class="home-button home-complement-button" href="https://esims.one/" style="-webkit-tap-highlight-color:rgba(0,0,0,0);text-decoration:none"><span>Home</span></a></div>

关于html - 使用 HTML 和 CSS 修复按钮动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466334/

相关文章:

CSS 网格 : dynamically span the last column

javascript - 导入外部 javascript (clamp.js) 以进行线钳位不起作用

css - 如何启动 te select 元素与表单中的标签对齐

jquery - 更改 .hover <h2> 标题的颜色(但保持其他 <h2> 的原始颜色)

javascript - 收缩订单价目表中的税金和运费 list

javascript - jquery.min.js 获取错误字符

javascript - 滑动 map 如何工作?

html - CSS 和 HTML 中的最小高度

html - 响应式 CSS 在移动设备上具有不同的 rem 大小

javascript - onclick my 'button' 打开所有同类型按钮