使用CSS,如何防止<button>在状态...位置之间跳转?

时间:2018-02-09 21:54:26

标签: html css html5 css3 css-transforms

我正在努力构建按钮样式,当用户将鼠标悬停在按钮上时,该按钮向上移动2个像素。我在悬停时使用了以下内容:

`transform: translateY(-2px);`

问题是,如果你将鼠标移到按钮下方,按钮就会开始移动,但按钮现在会从正常跳转/跳转到活动状态,导致按钮跳跃。

关于如何实现按钮在悬停时向上移动2个像素但在鼠标靠近按钮边缘时没有跳跃的任何想法?

.ui.button {
  outline: none;
  height: 48px;
  font-weight: 500;
  font-size: 17px;
  line-height: 20px;
  padding-top: 12.5px;
  padding-bottom: 12.5px;
  color: #fff;
  background: green;
  border: 1.5px solid darkGreen;
  box-shadow: 0 2px rgba(6,164,105,.25);
  transition: transform .1s ease-in-out,box-shadow .1s ease-in-out,-webkit-transform .1s ease-in-out,-webkit-box-shadow .1s ease-in-out;
}

.ui.primary.button:hover {
  transform: translateY(-2px);
  background: green;
  border-color: darkGreen;
  box-shadow: 0 4px rgba(6,164,105,.25);
}
<button class="ui medium primary button  button-icon-with-text " role="button" style="margin-bottom: 0px;">Primary Button</button>

2 个答案:

答案 0 :(得分:2)

一个想法是在翻译后使用一个覆盖按钮下方空间的伪元素,你将避免这种影响。所以就像你只是增加元素的总高度而不是翻译它。

.ui.button {
  position:relative;
  outline: none;
  height: 48px;
  font-weight: 500;
  font-size: 17px;
  line-height: 20px;
  padding-top: 12.5px;
  padding-bottom: 12.5px;
  color: #fff;
  background: green;
  border: 1.5px solid darkGreen;
  box-shadow: 0 2px rgba(6,164,105,.25);
  transition: transform .1s ease-in-out,box-shadow .1s ease-in-out,-webkit-transform .1s ease-in-out,-webkit-box-shadow .1s ease-in-out;
}
.ui.button:after {
  content:"";
  position:absolute;
  bottom:0;
  right:0;
  left:0;
  height:0;
}
.ui.primary.button:hover {
  transform: translateY(-2px);
  background: green;
  border-color: darkGreen;
  box-shadow: 0 4px rgba(6,164,105,.25);
}
.ui.primary.button:hover:after {
  content:"";
  height:2px;
  bottom:-3.5px; /* Don't forget the border !*/
}
<button class="ui medium primary button  button-icon-with-text " role="button" style="margin-bottom: 0px;">Primary Button</button>

另一个想法是在应用悬停的位置使用按钮的容器。这将适用于任何transalation值,您可以避免进行计算以查找伪元素的值:

.ui.button {
  position: relative;
  outline: none;
  height: 48px;
  font-weight: 500;
  font-size: 17px;
  line-height: 20px;
  padding-top: 12.5px;
  padding-bottom: 12.5px;
  color: #fff;
  background: green;
  border: 1.5px solid darkGreen;
  box-shadow: 0 2px rgba(6, 164, 105, .25);
  transition: transform .1s ease-in-out, box-shadow .1s ease-in-out, -webkit-transform .1s ease-in-out, -webkit-box-shadow .1s ease-in-out;
}

.contain {
  display: inline-block;
}

.contain:hover .ui.primary.button {
  transform: translateY(-2px);
  background: green;
  border-color: darkGreen;
  box-shadow: 0 4px rgba(6, 164, 105, .25);
}
<div class="contain">
<button class="ui medium primary button  button-icon-with-text " role="button" style="margin-bottom: 0px;">Primary Button</button>
</div>

答案 1 :(得分:1)

与Temani Afif的anwser类似,我会使用比元素长2px的伪元素,但在悬停时将溢出从隐藏切换为可见。

&#13;
&#13;
.ui.button {
  position:relative; overflow:hidden;
  outline: none;
  height: 48px;
  font-weight: 500;
  font-size: 17px;
  line-height: 20px;
  padding-top: 12.5px;
  padding-bottom: 12.5px;
  color: #fff;
  background: green;
  border: 1.5px solid darkGreen;
  box-shadow: 0 2px rgba(6,164,105,.25);
  transition: transform .1s ease-in-out,box-shadow .1s ease-in-out,-webkit-transform .1s ease-in-out,-webkit-box-shadow .1s ease-in-out;
}

.ui.primary.button::after{
  content:"";
  position:absolute;
  top:0; left:0;
  width:100%;
  height:calc(100% + 3.5px); /* translate + bottom border height*/
}
.ui.primary.button:hover {
  overflow:visible;
  transform: translateY(-2px);
  background: green;
  border-color: darkGreen;
  box-shadow: 0 4px rgba(6,164,105,.25);
}
&#13;
<button class="ui medium primary button  button-icon-with-text " role="button" style="margin-bottom: 0px;">Primary Button</button>
&#13;
&#13;
&#13;