按钮CSS转换/动画悬停问题

问题描述 投票:0回答:5

我想创建一个在悬停时更改的按钮。在这里你可以检查一下: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>

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

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

css css-transitions css-animations
5个回答
1
投票

目前还不是很清楚你的影响是什么,但你正在改变尺寸,所以当它缩小时你不再徘徊在它上面了。

我建议使用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>

1
投票

发生这种情况是因为每当按钮缩小时,鼠标就不再悬停在它上面,因此动画会重新启动并点击框(导致它再次触发。我建议使用伪元素 -

.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


1
投票

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

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>

0
投票

我会将你的.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>

View Updated CodePen


0
投票

诀窍是将:hover应用于周围元素。所以你一直悬停在元素上,只是包含的块会改变。

供参考:

translate3d用于比普通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>
© www.soinside.com 2019 - 2024. All rights reserved.