2009-06-02 114 views
0

我正在查看jQuery截屏視頻(jQuery for Absolute Beginners: Day 8)。它有這樣的代碼:堆疊HTML元素的順序

<script type="text/javascript"> 
    $(function() { 
     $('.wrap').hover(function() { 
      $(this).children('.front').stop().animate({ "top" : '300px'}, 900); 
     }, function() { 
      $(this).children('.front').stop().animate({ "top" : '0'}, 700);  
     }); 
    }); 
</script> 

<style type="text/css"> 

#container { 
width: 850px; 
text-align: center; 
margin: auto; 
} 

.wrap { 
width: 250px; 
height: 140px; 
position: relative; 
overflow: hidden; 
padding: 0 1em; 
} 

img { 
position: absolute; 
top: 0; left: 0; 
} 
</style> 
</head> 

<body> 
<div id="container"> 
    <div class="wrap"> 
     <img src="back.jpg" alt="image" /> 
     <img src="front.jpg" class="front" alt="image" /> 
    </div> 
</div> 
</body> 
</html> 

爲什麼front.jpg出現在back.jpg的頂部?是因爲front.jpg在back.jpg標籤後面的標籤中?

+0

這應該被標記HTML,CSS。我還建議你在深入jQuery之前熟悉html/css,因爲它通常用於操縱這兩者。 – 2009-06-02 02:31:56

回答

4

當省略z-index時,以後的兄弟姐妹會自動在比以前的兄弟姐妹更高的層上。這是預期的行爲。

爲了從下面的MDC頁引證:

當沒有元件具有z索引,元件被以此順序堆疊(從底部到頂部):

  1. 背景和邊界根元素
  2. 後裔塊的正常流動,在外觀上(在HTML)
  3. 後代定位元素,順序在外觀(在HTML)的順序

見:https://developer.mozilla.org/en/understanding_css_z-index,特別是,第一部分,https://developer.mozilla.org/en/Understanding_CSS_z-index/Stacking_without_z-index

+0

你能否回答http://stackoverflow.com/questions/9943560/html-alignment-issue-in-one-machine-only-both-ie8? – Lijo 2012-03-30 13:19:43

0

是的,這有點僥倖... z-index應該確定兩者以確保。