首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用HTML和CSS修复按钮动画

使用HTML和CSS修复按钮动画
EN

Stack Overflow用户
提问于 2020-08-18 18:04:25
回答 2查看 90关注 0票数 4

我有一个用HTML和CSS制作的基本按钮,当悬停在它上面时显示一个文本。我想纠正动画效果,这是不太正确的。

移除光标后,动画将大幅减小按钮的大小。因此,也许它可以完成一个过渡效果?

我希望我说得够清楚了!

代码语言:javascript
复制
.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;
  }
}
代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-18 18:07:49

你不需要为此使用动画。只需使用转换即可。这样会更平滑。

代码语言:javascript
复制
.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;
}
代码语言:javascript
复制
<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>

票数 3
EN

Stack Overflow用户

发布于 2020-08-18 18:49:48

从我自己来说,我可以提供这个解决方案:

代码语言:javascript
复制
.home-button {
  line-height: 100%;
  padding: 5px 5px 5px 5px;
  font-family: Arial;
  font-size: 12px;
  position: relative;
  
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 40px;
  
  transition: 0.3s;
}

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

.home-button:hover {
  width: 100px;
}

.home-button:hover span {
  opacity: 1;
  animation-name: text_left;
  animation-duration: 1s;
}

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

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

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

@keyframes text_left {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
代码语言:javascript
复制
<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>

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63466334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档