gpt4 book ai didi

jquery - jQuery animate 和 z-index/相对定位的问题

转载 作者:行者123 更新时间:2023-12-01 00:43:41 24 4
gpt4 key购买 nike

我试图使用position:relative 来让一个div 包含鼠标悬停时的其他div。不幸的是它只适用于最后一个 div。如果您将鼠标悬停在其他元素上,它们只会覆盖在它们之前声明的 div。

代码在这里:http://jsfiddle.net/YuKaR/3/

绝对定位工作正常,但不幸的是我无法对这个特定的应用程序使用绝对定位。

有人知道我做错了什么吗?感谢您抽出时间。

最佳答案

相对定位的问题在于,位置是相对于其正常位置的,这意味着如果您调整中间元素的大小,浏览器将移动并重新排列其后面的所有内容。

需要进行一些更改才能使其正常工作。如果您想使用相对定位,则必须将调整大小的 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 标记:

 <div id="main" style="position:relative;z-index:1;">

<div class="container"><div id="lefttop" class="resizer">left top</div></div>
<div class="container"><div id="righttop" class="resizer">right top</div></div>
<p style="clear:both;"></p>
<div class="container"><div id="leftbottom" class="resizer">left bottom</div></div>
<div class="container"><div id="rightbottom" class="resizer">right bottom</div></div>

</div>

CSS:

.resizer { position:relative; border: 1px solid #000000; padding:10px; margin:0px; width:150px; height:150px; }

.container { position:relative; padding:0px; margin:8px; float:left; z-index: 2; width:172px; height:172px; }

JavaScript:

$(function(){
$(".resizer").mouseover(function() {
$(".container").css('z-index' , '2');
$(this).parent().css('z-index' , '3');
if(this.id == "lefttop"){
aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '0', top: '0'}
}else if(this.id == "righttop"){
aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '-=190', top: '0'}
}else if(this.id == "leftbottom"){
aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '0', top: '-=190'}
}else if(this.id == "rightbottom"){
aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '-=190', top: '-=190'}
}
$(this).css('z-index' , '99').animate(aoptions, 800);
}).mouseout(function(){
if(this.id == "lefttop"){
aoptions = {width: "150px", height: "150px", backgroundColor: "#FFFFFF", left: '0', top: '0'}
}else if(this.id == "righttop"){
aoptions = {width: "150px", height: "150px", backgroundColor: "#FFFFFF", left: '+=190'}
}else if(this.id == "leftbottom"){
aoptions = {width: "150px", height: "150px", backgroundColor: "#FFFFFF", left: '0', top: '+=190'}
}else if(this.id == "rightbottom"){
aoptions = {width: "150px", height: "150px", backgroundColor: "#FFFFFF", left: '+=190', top: '+=190'}
}
$(this).animate(aoptions, 800, function(){
$(this).parent().css('z-index' , '2');
});
});
});

关于jquery - jQuery animate 和 z-index/相对定位的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5734891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com