按钮 CSS 过渡/动画悬停问题



我想创建一个在悬停时会更改的按钮。在这里你可以检查它:https://drive.google.com/file/d/1fIcil2Xyky8DC2NRcfZBKXCMHk9gpat2/view?usp=sharing。

在这里你可以看到我的尝试:https://codepen.io/koravski/pen/BEvKRP

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  font-family: 'Lato', sans-serif;
}
.txt {
  position: absolute;
  color: #fff;
}
.box {
  width: 200px;
  height: 50px;
  background-color: red;
  transition: all 1s;
}
.box:hover {
  width: 50px;
  height: 5px;
  margin-top: 40px;
}
<div class="txt">Call to action</div>
<div class="box"></div>

问题是当我悬停它时,它开始循环。它应该在红色矩形上,这样它就不会循环。

如果你有任何建议,那就太好了。谢谢。

目前还不清楚你追求什么效果,但你正在改变大小,所以当它缩小时,你不再悬停在它上面。

我建议改用transform(实际上是两个,一个用于大小,另一个用于位置(。

html,
body {
  height: 100%;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  font-family: 'Lato', sans-serif;
}
.txt {
  position: absolute;
  color: #fff;
}
.box {
  width: 200px;
  height: 50px;
  background-color: red;
  transition: all 1s;
}
.box:hover {
  transform: translateY(40px) scale(.1);
}
<div class="txt">Call to action</div>
<div class="box"></div>

还有其他问题,但这取决于这实际上应该做什么。

实际上,您可以使用单个div来执行此操作。

html,
body {
  height: 100%;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  font-family: 'Lato', sans-serif;
}
.txt {
  position: relative;
  color: #fff;
  width: 200px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  transition: all 1s;
}
.txt:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-color: red;
  z-index: -1;
  transition: all 1s;
}
.txt:hover:before {
  transform: translateY(40px) scale(.1);
}
<div class="txt">Call to action</div>

发生这种情况

是因为每当按钮缩小时,您的鼠标不再悬停在其上,因此动画会重新出现并击中框(导致它再次触发。我建议使用伪元素 -

.box::before {
  background-color: red;
  content: '';
  position: absolute;
  height: 100%;
  width: 100%;
  z-index: 0;
  transition: all 1s;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}
.box:hover.box::before {
   width: 50px;
   height: 5px;
}

https://codepen.io/aedenmurray/pen/XQodQj

这是一个使用背景动画的想法,你只需要一个元素:

body {
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Lato', sans-serif;
}
.txt {
  color: #fff;
  padding:20px 50px;
  background-image:linear-gradient(red,red);
  background-size:100% 100%;
  background-position:bottom center;
  background-repeat:no-repeat;
  transition:1s;
}
.txt:hover {
  background-size:20% 5%;
  color:#000;
}
<div class="txt">Call to action</div>

我会将您的.box.txt包裹在一个容器中,然后在该容器悬停时,执行过渡。这样,您的悬停区域就不会调整大小 - 而是调整内容的大小。

html, body {
  height: 100%;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  font-family: 'Lato', sans-serif;
}
.txt {
  position:absolute;
  color: #fff;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 100px;
}
.box {
  width: 200px;
  height: 50px;
  background-color: red;
  transition: all 1s;
}
.container {
  position: relative;
}
.container:hover > .box {
  width: 50px;
  height: 5px;
  margin-top: 40px;
}
<div class="container">
  <div class="txt"> Call to action</div>
  <div class="box"></div>
</div>

查看更新的代码笔

诀窍

是将:hover应用于周围的元素。因此,您一直将鼠标悬停在元素上,只有包含的块会发生变化。

仅供参考:

翻译3D用于比普通translateX更好的性能。您可以进一步改进代码。红色元素可以是::before元素,您可能也想附加:focus,以获得更好的可访问性。也许使用button元素。

html, body {
  height: 100%;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #333;
  font-family: 'Lato', sans-serif;
}
.btn {
  position: relative;
}
.btn-txt {
  color: #fff;
  text-align: center;
  vertical-align: middle;
  position: relative;
  padding: 20px 30px;
}
.btn-box {
  background-color: red;
  transition: all 1s;
  position: absolute;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
}
.btn:hover .btn-box {
  width: 50px;
  height: 5px;
  transform: translate3d(50px, 0, 0);
  top: auto;
}
<div class="btn">
  <div class="btn-box"></div>
  <div class="btn-txt">Call to action</div>
</div>

最新更新