【问题标题】:When using the transform rotate CSS property, how can we prevent the vibrating border issue on hover transition?使用 transform rotate CSS 属性时,我们如何防止悬停过渡时的振动边框问题?
【发布时间】:2016-11-07 21:00:27
【问题描述】:

我正在尝试将一个元素固定在页面的右侧,使用 transform rotate CSS 属性将其旋转 90 度,然后在悬停时我希望元素使用缓慢的过渡向左滑出。但是,当我在 Chrome 中尝试对此进行基本实现时,会出现难看的振动顶部边框。

Bug Demo Page

该问题似乎仅在元素中输入文本时发生,并且仅在使用该文本动态调整元素大小时发生。这让我相信 transform 属性在悬停过渡期间上下舍入一个像素,导致元素在轻松过渡期间不规则地调整大小。

我可以通过在元素上设置固定宽度来解决此问题,但在这种情况下固定元素的宽度不是可接受的解决方案,因为文本可以在此固定元素内变化。

有没有人有预防或解决此问题的想法?谢谢!

HTML

<a id="right_fixed_element">
  Fixed Side Button
</a>

CSS

#right_fixed_element {
/* vibrating border bug goes away with fixed width */
/*   width: 150px; */
  position: fixed;
  top: 40%;
  right: 0;
  padding: 10px;
  color: white;
  background-color: red;
  border: 1px solid #777;
  transform: rotate(-90deg);
  -ms-transform: rotate(-90deg);
  -webkit-transform: rotate(-90deg);
  -moz-transform: rotate(-90deg);
  transition: all .5s ease;
  -moz-transition: all .5s ease;
  -webkit-transition: all .5s ease;
  -o-transition: all .5s ease;
  -webkit-backface-visibility: hidden;
  -webkit-transform: rotate(-90deg) translateZ(0) scale(1.0, 1.0);
}
#right_fixed_element:hover {
  right: 10px;
}

【问题讨论】:

    标签: html css transform


    【解决方案1】:

    我相信使用transform: translateY() 移动元素(而不是动画right: 10px)动画更简洁:

    #right_fixed_element {
    /* vibrating border bug goes away with fixed width */
    /*   width: 150px; */
      position: fixed;
      top: 40%;
      right: 0;
      padding: 10px;
      color: white;
      background-color: red;
      border: 1px solid #777;
      transform: rotate(-90deg);
      -ms-transform: rotate(-90deg);
      -webkit-transform: rotate(-90deg);
      -moz-transform: rotate(-90deg);
      transition: all .5s ease;
      -moz-transition: all .5s ease;
      -webkit-transition: all .5s ease;
      -o-transition: all .5s ease;
      -webkit-backface-visibility: hidden;
    }
    #right_fixed_element:hover {
      transform: rotate(-90deg) translateY(-10px);
      -ms-transform: rotate(-90deg) translateY(-10px);
      -moz-transform: rotate(-90deg) translateY(-10px);
      -webkit-transform: rotate(-90deg) translateY(-10px);
      
    }
    <!-- When you hover over this transform-rotated element with the slow transition speed, a buggy vibrating behavior appears at the top of the element. 
    
    If you set a fixed width on the element, then the vibrating behavior will go away.
    
    How can we prevent the vibrating bug, without setting a fixed width, while still acheiving the transform on this fixed element?
    -->
    
    <a id="right_fixed_element">
      Fixed Side Button
    </a>

    【讨论】:

    • 哦,太好了,我没想到!如果这通过了浏览器测试,我会将此答案标记为正确。
    猜你喜欢
    • 2015-11-23
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2020-11-19
    • 2017-06-22
    • 1970-01-01
    相关资源
    最近更新 更多