2012-08-22 32 views
1

我試圖創建一個懸停在事件,將不會影響我的佈局的其餘部分。看來z-index是做到這一點的方法,但我無法實現它。我試圖堅持使用CSS,但jQuery是可行的。z-index懸停在不起作用

充分應用:http://jsfiddle.net/9cRdq/

HTML

<div class="big-square" id="br"> 
    <div class="little-square" id="br-tl"></div> 
    <div class="little-square" id="br-tr"></div> 
    <div class="little-square" id="br-bl"></div> 
    <div class="little-square" id="br-br"> </div> 
</div> 

CSS

.big-square 
{ 
    position: relative; 
    z-index: 1; 
    font-size: 2em; 
    float: left; 
    width: 30%; 
    min-width: 4em; 
    min-height: 4em; 
    margin: 0 2% 2% 0; 
    background: #000; 
} 
.little-square 
{ 
    position: relative; 
    z-index: 1; 
    cursor: pointer; 
    float: left; 
    width: 48%; 
    height: 48%; 
    background: #cd2626; 
} 
.little-square:hover 
{ 
    z-index: 1000; 
} 

jQuery的

$(document).ready(resizeBigSquare); 
window.onresize = resizeBigSquare; 
function resizeBigSquare() { 
    $('.big-square').css(
     { 
      'height': ($('.big-square').width() * .85) + 'px', 
      'line-height': ($('.big-square').width() * .85) + 'px' 
     }); 
    $('.little-square, .rectangle-top, .rectangle-bottom').css(
     { 
      'line-height': $('.little-square').height()+ 'px' 
     }); 
} 
+0

我認爲你不能一起使用浮動和位置。基本上浮動渲染其他元素放置後的文檔。與位置屬性相同。所以我想如果你想用z-index懸停擺脫浮動。 –

+0

我刪除了花車,它仍然無法工作。 – user1134179

+0

js請好嗎? –

回答

1

z-index變化對懸停正在強制很好。實際上,here's a fiddle顯示它通過使用box-shadow工作。你的div.little-square實際上高於它的同伴,否則box-shadow會落後於其他人。

注意z-index僅影響z軸堆疊順序沒有別的(大小,顏色等)

如果你正在尋找懸停改變div.little方的大小(不會破壞網格),請考慮在每個方形內創建一個子元素。然後,將position:absolute應用到新的子元素,將小方塊的一些屬性轉移到它,並相應地設置懸停狀態。 This fiddle就是我正在談論的一個例子。