jQuery animate 和 z-index/相对定位的问题
我试图使用position:relative 来让一个div 包含鼠标悬停时的其他div。不幸的是它只适用于最后一个 div。如果您将鼠标悬停在其他 div 上,它们只会覆盖在它们之前声明的 div。
代码在这里: http://jsfiddle.net/YuKaR/3/
绝对定位工作正常,但不幸的是我不能对这个特定的应用程序使用绝对定位。
有人知道我做错了什么吗?感谢您抽出时间。
I'm trying to get a div to encompass the other divs on mouseover using position: relative. Unfortunately it only works for the last div. If you mouseover the others, they only cover the divs that are declared before them.
The code is here: http://jsfiddle.net/YuKaR/3/
Absolute positioning works fine, but unfortunately I cannot use absolute positioning for this particular app.
Anyone know what I'm doing wrong? Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相对定位的问题在于,位置是相对于其正常位置的,这意味着如果您调整中间元素的大小,浏览器将移动并重新排列其后面的所有内容。
需要进行一些更改才能使其发挥作用。如果您想使用相对定位,则必须将调整大小的 div 包装在固定大小的容器中,这样当它们调整大小时就不会破坏元素流。你的div有150px的宽度和高度,固定大小的容器必须足够大才能容纳它,假设默认的盒子模型是150px + 10px*2 padding + 1px*2 border = 172px。由于元素流是由容器控制的,我将 margin 移到了 css 中的容器。
通过将它们包装在额外的固定大小的 div 中,元素流将永远不会改变,调整大小的 div 只会穿过容器的边缘,与其他容器重叠(溢出:可见)。
我还更改了您的 z 索引逻辑,因为您现在需要设置容器的 z 索引(这将应用于所有子元素)。默认情况下,所有内容的 z 索引均为 2。当 div 大小调整回其原始状态时,我在动画结束后使用 .animate() 上的回调函数将其容器的 z 索引设置回 2。当开始调整大小时,所有容器都将重置为 z-index 2,以防有一个容器仍在动画回到其原始状态,当前调整大小的 div 容器将设置为 z-index 3 以使其显示在所有其他容器之上。
http://jsfiddle.net/x34d3/
HTML 标记:
CSS:
javascript:
The problem with relative positioning is that positions are relative to their normal position, meaning if you resize an element in the middle, the browser will move and re-flow everything that comes after it.
A few changes needed to be made to make it work. If you want to use relative positioning, you have to wrap your resizing divs in fixed size containers, so when they're resizing they won't break the element flow. Your divs have 150px width and height, the fixed sized container must be big enough to hold it, assuming the default box-model it's 150px + 10px*2 padding + 1px*2 border = 172px. As the element flow is controled by the containers, I moved margin to the container in the css.
By wrapping them in an additional fixed sized div, element flow will never change, your resizing divs will just bleed through the edges of the container, overlapping the other containers (overflow:visible).
I also changed your z-index logic, as you need to set the z-index for the containers now (which will apply to all child elements). By default everything has z-index of 2. When the div is resized back to its original state, I set its container's z-index back to 2 after the animation, using a callback function on .animate(). When resizing starts, all containers are reset to z-index 2 in case there is one still animating back to its original state, the currently resizing div's container is set to z-index 3 to make it appear on top of all the others.
http://jsfiddle.net/x34d3/
HTML markup:
CSS:
javascript:
我花了一段时间,但我终于弄清楚了。看看这个工作 jsFiddle 演示 :
HTML:
CSS:
jQuery:
Took me a while, but I finally figured it out. Take a look at this working jsFiddle demo:
HTML:
CSS:
jQuery: