使用 jQuery 和/或 CSS3 过渡动画基于百分比的位置(通过插件)

发布于 2024-11-05 02:57:57 字数 1171 浏览 0 评论 0原文

我正在尝试制作一个页面布局,其中的部分在导航时可以滑入和滑出。 每个部分都会填充屏幕(100% 宽度/高度),并且绝对定位在基于百分比的值。

在所附示例中,有四个部分,位置如下:

[a]
[b][c][d]

其中 [a] 位于 0 0,[b] 位于 0 100%,[c] 位于 100% 100%,[d] 位于 200% 100%。

现在,为了实现导航,我将所有部分放置在 100% x 100% 绝对定位的溢出隐藏容器中。当导航到某个部分时,容器的“顶部”和“左侧”值将动画化为该部分偏移量的负数,例如,如果选择 [d],则容器将动画化为 -200% -100%。

奇怪的行为比比皆是!当导航到 0% 偏移(向左或向上)时,过渡效果很好。然而,其他组合(即负偏移)会导致看似不合逻辑(但一致)的怪癖。

亲自查看:http://www.doronassayas.com/ypsite

以下是摘要:

  • [c] 和[d] 到 [a] 或 [b] 都可以正常工作。
  • [a] 到 [b] 和 [a] 到 [d]:跳到结尾,跳回开头并开始动画。
  • [a] 和 [b] 到 [c] 是最奇怪的 - 在转换开始之前,整个 body (或其他一些高级元素?)弹出到一些无法通过 CSS 追踪的视觉偏移(在Firebug和其他开发工具中不可见)。因此,我们最终不是在 [c] 处,而是在 [d] 处(视觉上),尽管 CSS 值清楚地表明我们应该在 [c] 处。执行随机操作(例如单击 Firebug 层次结构)会将视图重置到正确的位置。搞什么?

在 Firefox 4、Chrome 10 和 Safari 5 中进行了测试,所有浏览器中都会出现相同的行为,无论是使用 jQuery.animate() 还是 Louis Remi 非常酷的 jQuery.transition 插件,它只是在调用 animate() 时将 -prefix-transition 以及新的偏移值应用于容器。

这种陌生感的一致性引起了我的兴趣。有什么想法吗?

非常感谢任何帮助。

I'm trying to make a page layout that has sections that slide in and out when navigated to.
Each section fills the screen (100% width/height) and is positioned absolutely at a percentage-based value.

In the attached sample, there are four sections, positioned as follows:

[a]
[b][c][d]

where [a] is at 0 0, [b] is at 0 100%, [c] is at 100% 100%, and [d] is at 200% 100%.

Now, to achieve navigation I placed all sections in a 100% by 100% absolutely positioned, overflow hidden container. When a section is navigated to, the 'top' and 'left' values of the container are animated to the negative of the section's offset, e.g. if [d] is selected, container is animated to -200% -100%.

Strange behavior abounds! When navigating to a 0% offset (going left or going up), the transitions work fine. However, other combinations (i.e. negative offsets) result in seemingly illogical (yet consistent) quirks.

See for yourself: http://www.doronassayas.com/ypsite

Here's a summary:

  • [c] and [d] to either [a] or [b] works fine.
  • [a] to [b] and [a] to [d]: jumps to the end, jumps back to the beginning and starts animating.
  • [a] and [b] to [c] is the strangest - before the transition starts, the entire body (or some other high level element?) pops to some visual offset that is not traceable via CSS (invisible in Firebug and other development tools). So instead of ending up at [c], we end up at [d] (visually), even though the CSS values clearly show we are supposed to be at [c]. Doing random stuff like clicking in the Firebug hierarchy resets the view to the correct location. WTF?

Tested this in Firefox 4, Chrome 10 and Safari 5, and the same behavior occurs in all browsers, whether using jQuery.animate() or Louis Remi's very cool jQuery.transition plugin, which simply applies a -prefix-transition to the container along with the new offset values when animate() is called.

The consistency of the strangeness intrigues me. Any ideas?

Any help greatly appreciated.

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

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

发布评论

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

评论(1

左岸枫 2024-11-12 02:57:57

经过相当多的揪心之后,我终于通过在 body 元素上打开“overflow: auto”来诊断出错误行为。事实证明,具有负值的定位滑稽动作会对计算出的主体宽度和高度造成严重破坏,该宽度和高度在过渡期间不断变化(并且实际上永远不会停留在窗口的 100% 上,这是预期的行为)。结果是滚动条波动和 body 元素的滚动位置不正确,尽管跨浏览器的一致性令人惊讶。

解决方案非常简单:

body
{
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    padding: 0;
    margin: 0;
}

我以前从未使用过的固定位置使 body 元素保持固定的大小和位置,同时忽略对其内容的任何更改。现在你就拥有了!可爱的过渡,硬件加速,感谢 jQuery 和 Louis Remi 的支持。

After considerable hair-pulling I finally diagnosed the buggy behavior by switching on overflow: auto on the body element. Turns out the positioning antics with negative values wreak havoc on the calculated width and height of the body, which keeps changing during transitions (and never actually stays on 100% of the window, which was the expected behavior). The result is fluctuating scrollbars and incorrect scroll locations of the body element, albeit surprisingly consistent across browsers.

The solution turned out to be very simple:

body
{
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    padding: 0;
    margin: 0;
}

The fixed position, which I never used before, makes the body element maintain fixed size and position while ignoring any changes to its content. And there you have it! Lovely transitions, hardware-accelerated where supported thanks to jQuery and Louis Remi.

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