CSS3 过渡 +不显示 +防止过度滚动

发布于 2024-12-06 20:47:38 字数 504 浏览 0 评论 0原文

因此,如果您还不熟悉的话,CSS3 过渡不会对 display: none 进行动画处理,因为它会从 DOM 中完全删除目标元素。这是我的问题。我有一个侧边栏,其中悬停时会出现更大的弹出 div。不幸的是,由于我只能在 visibility: hideopacity: 0 上进行转换,因此由于布局中包含明显隐藏的 div,因此我出现了过度滚动,从而产生了一个非常长的弹出窗口占页面的滚动条。

寻找一些创造性的解决方案,以了解如何在不将滚动条搞砸的情况下仍然可以制作动画。

我正在本地开发,所以我没有可以展示的实时示例,但您可以在此截屏视频中看到问题: http://dreamstarstudios.com/screencasts/2011-09-27_2111.swf

提前致谢!

So if you're not already familiar, CSS3 transitions do not animate display: none since it removes the targeted element from the DOM altogether. So here's my issue. I have a sidebar with larger popup divs that appear on hover. Unfortunately Since I can only transition on visibility: hidden and opacity: 0 I have overscroll due to the visibly hidden divs being included in the layout and thus making a very long popup which is accounted for in the page's scrollbar.

Looking for some creative solutions for how I can still animate without having the scrollbar all screwed up.

I'm developing local so I don't have a live example to show, but you can see the problem in this screencast: http://dreamstarstudios.com/screencasts/2011-09-27_2111.swf

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味拾荒者 2024-12-13 20:47:38

我假设您的弹出窗口已绝对定位,因此您可以执行以下操作:

  1. 隐藏时,将弹出窗口 top 设置为一个巨大的负值。这会将其移出屏幕并摆脱滚动条。
  2. 悬停时,将 top 设置为正确的值并转换 opacity 值。
  3. 确保您的 CSS transition 规则仅适用于 opacity 属性。

HTML

<a href="">Popup go now</a>
<div class="popup">
    My cat's breath smells like cat food...
</div>

CSS

.popup {
   position: absolute;
   top: -2000px;
   opacity: 0;

   transition: opacity 1s ease-in-out;
}

a:hover + .popup,
.popup:hover {
   top: 30px;
   opacity: 1;   
}

这是上面的实际操作

更新:要添加向左摆动,并清理鼠标移出动画,可以使用以下代码:

.popup {
   position: absolute;
   top: -2000px;
   width: 300px;
   left: 0;
   opacity: 0;

   /* Animate opacity, left and top  
      opacity and left should animate over 1s, top should animate over 0s
      opacity and left should begin immediately.  Top should start after 1s. */
   transition-property: opacity, left, top;
   transition-duration: 1s, 1s, 0s;
   transition-delay: 0s, 0s, 1s;
}

a:hover + .popup,
.popup:hover {
   top: 30px;
   left: 30px;
   opacity: 1;  

   /* All animations should start immediately */
   transition-delay: 0s;
}

其执行方式如下:

  1. 指定多个属性的动画(不透明度、左侧、顶部)。
  2. transition-delay 防止顶部值发生更改,直到不透明度和左侧动画完成为止。这里的技巧是,当元素为 :hover 时,没有延迟(不透明度、左侧和顶部动画全部立即开始)。但是,一旦 :hover 不再处于活动状态,顶部动画会在开始之前等待 1 秒。这会产生不透明度并留出足够的时间来完成。

这是更新的示例

I'm assuming your popup is absolutely positioned so you could do the following:

  1. While hidden, set the popup top to a huge negative value. This moves it off the screen and gets rid of the scrollbar.
  2. On hover, set the top to the correct value and transition the opacity value.
  3. Make sure that your CSS transition rules are only for the opacity property.

HTML

<a href="">Popup go now</a>
<div class="popup">
    My cat's breath smells like cat food...
</div>

CSS

.popup {
   position: absolute;
   top: -2000px;
   opacity: 0;

   transition: opacity 1s ease-in-out;
}

a:hover + .popup,
.popup:hover {
   top: 30px;
   opacity: 1;   
}

Here's the above in action.

Update: To add the left swing, and clean up the mouseout animation, you can use the following code:

.popup {
   position: absolute;
   top: -2000px;
   width: 300px;
   left: 0;
   opacity: 0;

   /* Animate opacity, left and top  
      opacity and left should animate over 1s, top should animate over 0s
      opacity and left should begin immediately.  Top should start after 1s. */
   transition-property: opacity, left, top;
   transition-duration: 1s, 1s, 0s;
   transition-delay: 0s, 0s, 1s;
}

a:hover + .popup,
.popup:hover {
   top: 30px;
   left: 30px;
   opacity: 1;  

   /* All animations should start immediately */
   transition-delay: 0s;
}

It does this as follows:

  1. Animation for multiple properties are specified (opacity, left, top).
  2. transition-delay prevents the top value from being changed until the opacity and left animations are complete. The trick here is that when the element is :hover, there is no delay (opacity, left and top animations all start at once). However once :hover is no longer active, the top animation waits 1 second before starting. This gives opacity and left enough time to finish.

Here's the updated example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文