2011-04-20 95 views
3

我想要获得一个div来包含其他使用position:relative的鼠标悬停。不幸的是,它只适用于最后一个div。如果您将鼠标悬停在其他人身上,他们只会覆盖在他们之前宣布的div。jQuery动画和Z索引问题/相对定位

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

绝对定位工作得很好,但不幸的是我不能使用绝对定位对于这个特定的应用程序。

任何人都知道我在做什么错了?感谢您的时间。

+0

我强烈建议你使用样式表上的小提琴。内联阅读几乎是不可能的。 – 2011-04-20 18:50:50

+0

对不起,我不知道我是如何错过的。谢谢。 – Jace 2011-04-20 19:47:32

回答

6

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

需要做一些改变才能使其发挥作用。如果你想使用相对定位,你必须将你的调整大小div封装在固定大小的容器中,所以当它们调整大小时,它们不会破坏元素流。你的div有150px的宽度和高度,固定大小的容器必须足够大以容纳它,假设默认盒子模型为150px + 10px * 2 padding + 1px * 2 border = 172px。由于元素流由容器控制,我将边界移至CSS中的容器。

通过额外的固定大小的div包裹其中,元素流永远不会改变,你的调整div一定只是通过容器的边缘流血,重叠其他容器(溢出:可见)。

我也改变了你的z-index逻辑,因为你现在需要为容器设置z-index(它将适用于所有子元素)。默认情况下,所有的z-index都是2.当div重新调整到原始状态时,我使用.animate()上的回调函数将其容器的z-index设置回到2。当调整开始,所有的集装箱被重置为Z-指数2的情况下,有一个还是动画恢复到原来的状态,当前调整div的容器设定为z指数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'); 
     }); 
    }); 
}); 
+0

看起来很像我的解决方案,首先发布,只是不如= D – 2011-04-20 22:22:51

+1

@Scott:true,但它更接近原始代码。 :P – DarthJDG 2011-04-20 22:24:18

+0

@DarthJDG - 我的OCD不会让我忽略可以优化代码或让它看起来更有条理的地方。这就是我偏离的原因。 – 2011-04-21 01:29:17

4

花了我一段时间,但我终于明白了。看看这个working jsFiddle demo

HTML:

<div id="main"> 

    <div class="overlay"><div id="lefttop">left top</div></div> 
    <div class="overlay"><div id="righttop">right top</div></div> 
    <p></p> 
    <div class="overlay"><div id="leftbottom">left bottom</div></div> 
    <div class="overlay"><div id="rightbottom">right bottom</div></div> 

</div> 

CSS:

* { 

    margin: 0px; 
    padding: 0px; 
    border: none; 
    z-index: -1; 
} 

p { clear: both; } 

#main { 

    float: left; 
    margin: 50px; 
    background: black; 

} 

#main div { 

    position: relative; 
    float: left; 

    height: 150px; 
    width: 150px; 

} 

.overlay { 

    height: 0; 
    overflow: visible; 
    position: relative; 
    z-index: 99; 

} 

#lefttop { background: yellow; } 
#righttop { background: green; } 
#leftbottom { background: red; } 
#rightbottom { background: blue; } 

的jQuery:

var $divs = $("div", ".overlay"), 
    optionsOver = { 

     width: "300px", 
     height: "300px" 

    }, 
    optionsOut = { 

     width: "150px", 
     height: "150px" 

    }; 

$divs.hover(function() { 

    $divs 
     .stop(true,true) 
     .parent() 
     .css({"z-index" : "1"}); 

    if (this.id == "lefttop") { 

     optionsOver.left = "0px"; 
     optionsOver.top = "0px"; 

    } else if (this.id == "righttop") { 

     optionsOver.left = "-=150px"; 
     optionsOver.top = "0px"; 

    } else if (this.id == "leftbottom") { 

     optionsOver.left = "0px"; 
     optionsOver.top= "-=150px"; 

    } else if (this.id == "rightbottom") { 

     optionsOver.left = "-=150px"; 
     optionsOver.top= "-=150px"; 

    } 

    var $this = $(this); 
    $this.stop(true,true); 
    $this.parent().css({"z-index" : "99"}); 
    $this.animate(optionsOver, 400); 

}, function() { 

    if (this.id == "lefttop") { 

     optionsOut.left = "0px"; 
     optionsOut.top = "0px"; 

    } else if (this.id == "righttop") { 

     optionsOut.left = "+=150px"; 
     optionsOut.top = "0px"; 

    } else if (this.id == "leftbottom") { 

     optionsOut.left = "0px"; 
     optionsOut.top= "+=150px"; 

    } else if (this.id == "rightbottom") { 

     optionsOut.left= "+=150px"; 
     optionsOut.top = "+=150px"; 

    } 

    $(this) 
     .stop(true,true) 
     .animate(optionsOut, 400, function() { 
      $divs.parent().css({"z-index" : "1"}); 
     }); 

}); 
+0

主要的诀窍是将每个div换成另一个div,并使用'.overlay'类。该类被设置为“height:0”和“overflow:visible”。然后,当一个div被掩盖时,我们将'.overlay' div设置为'z-index:1',同时将'overlay' div设置为'z-index:99'。 '.overlay' CSS和moused over action的结合告诉浏览器不要推送其他HTML元素。 – 2011-04-20 22:12:29

+0

我还将对象文字向上移动,以便我们不必重新键入一堆相同的属性,以及添加'.stop(true,true)'来消除动画排队。 – 2011-04-20 22:14:39

+0

非常好,谢谢!我会立即采用这些优化。 – Jace 2011-04-21 15:55:50