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

标签 html css 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>

最佳答案

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

.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);
}
<button class="ui medium primary button  button-icon-with-text " role="button" style="margin-bottom: 0px;">Primary Button</button>

关于html - 使用 CSS,如何防止 <button> 在状态...位置之间跳转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48714641/

相关文章:

css - 是否可以为转换选项设置不同的持续时间/延迟?

html - Bootstrap 护理人员 : Making sub carets and float them to the right?

css - 没有行高的垂直对齐文本内部 div

html - 如何仅使用 CSS 将播放按钮覆盖在缩略图上

javascript - 使用 jQuery Animate 分步动画图像 Sprite ?

html - 内联 block <div> 向下推到父级之外,尽管高度为 : inherit;,但高度仍高于父级

css - 裁剪部分div

html - 如何在中心垂直挤压一个div?

html - IE 9 无法从 Tomcat 加载 HTML5 音频

html - Flexbox 页脚元素不会粘在 IE11 中的底部