像制作Airforce.com上的按钮一样,如何制作悬停动画的按钮

时间:2019-02-09 09:39:21

标签: html css html5 css3 css-animations

如何像在此GIF中找到的按钮或在airforce.com中找到的按钮一样,使按钮在悬停时动画。

Button GIF

2 个答案:

答案 0 :(得分:7)

这是一个具有多个元素和多个背景的想法:

.box {
  display:inline-block;
  padding:10px 20px;
  border-right:1px solid;
  border-left:1px solid;
  background:
      linear-gradient(#000,#000) top left,
      linear-gradient(#000,#000) top right,
      linear-gradient(#000,#000) bottom right,
      linear-gradient(#000,#000) bottom left;
  background-size:15% 1px,75% 1px;
  background-repeat:no-repeat;
  transition:.8s all;
}
.box:hover {
  background-size:75% 1px,15% 1px;
}
<div class="box">
  some text
</div>

<div class="box">
  more and more text
</div>

如果要固定空格,可以调整background-size。这是一个10px的示例,它的透明部分从边缘10px开始。

.box {
  display:inline-block;
  padding:10px 20px;
  border-right:1px solid;
  border-left:1px solid;
  background:
      linear-gradient(#000,#000) top left,
      linear-gradient(#000,#000) top right,
      linear-gradient(#000,#000) bottom right,
      linear-gradient(#000,#000) bottom left;
  background-size:10px 1px,calc(100% - 20px) 1px;
  background-repeat:no-repeat;
  transition:.8s all;
}
.box:hover {
  background-size:calc(100% - 20px) 1px,10px 1px;
}
<div class="box">
  some text
</div>

<div class="box">
  more and more text
</div>

另一个带有渐变的想法:

.box {
  display:inline-block;
  padding:10px 20px;
  border-right:1px solid;
  border-left:1px solid;
  background:
      linear-gradient(to right,
        #000 calc(50% - 5px),transparent calc(50% - 5px),
        transparent calc(50% + 5px),#000 calc(50% + 5px)),
      linear-gradient(to right,
        #000 calc(50% - 5px),transparent calc(50% - 5px),
        transparent calc(50% + 5px),#000 calc(50% + 5px));       
  background-size:150% 1px;
  background-position:top left,bottom right;
  background-repeat:no-repeat;
  transition:.8s all;
}
.box:hover {
  background-position:top right,bottom left;
}
<div class="box">
  some text
</div>

<div class="box">
  more and more text
</div>

答案 1 :(得分:6)

尝试对边框进行动画处理,但是非常复杂,因此只需将::after::before设置为按钮,然后对其进行动画处理即可。

body {
  display: flex;
  flex-direction: column;
  height: 93vh;
  justify-content: center;
  align-items: center;
  background: #222;
  color: #eee;
  font-family: monospace;
}

.brk-btn {
  position: relative;
  background: none;
  color: aqua;
  text-transform: uppercase;
  text-decoration: none;
  border: 0.2em solid aqua;
  margin-top: 1em;
  padding: 0.5em 1em;
}

.brk-btn::before {
  content: "";
  display: block;
  position: absolute;
  width: 10%;
  background: #222;
  height: 0.3em;
  right: 20%;
  top: -0.21em;
  transform: skewX(-45deg);
  -webkit-transition: all 0.45s cubic-bezier(0.86, 0, 0.07, 1);
  transition: all 0.45s cubic-bezier(0.86, 0, 0.07, 1);
}

.brk-btn::after {
  content: "";
  display: block;
  position: absolute;
  width: 10%;
  background: #222;
  height: 0.3em;
  left: 20%;
  bottom: -0.25em;
  transform: skewX(45deg);
  -webkit-transition: all 0.45 cubic-bezier(0.86, 0, 0.07, 1);
  transition: all 0.45s cubic-bezier(0.86, 0, 0.07, 1);
}

.brk-btn:hover::before {
  right: 80%;
}

.brk-btn:hover::after {
  left: 80%;
}

.underlined-a {
  text-decoration: none;
  color: aqua;
  padding-bottom: 0.15em;
  box-sizing: border-box;
  box-shadow: inset 0 -0.2em 0 aqua;
  transition: 0.2s;
}

.underlined-a:hover {
  color: #222;
  box-shadow: inset 0 -2em 0 aqua;
  transition: all 0.45s cubic-bezier(0.86, 0, 0.07, 1);
}
<div>As inspired from the website of the <a href="https://www.airforce.com/" class="underlined-a">&nbsp;U.S. AirForce&nbsp;</a></div>
<br><br>
<a href="https://www.airforce.com/" class="brk-btn">
  Join Now
</a>