2009-06-02 98 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应该确定两者以确保。