HTML DOM zIndex 屬性
定義和用法
zIndex 屬性設(shè)置元素的堆疊順序。
該屬性設(shè)置一個定位元素沿 z 軸的位置,z 軸定義為垂直延伸到顯示區(qū)的軸。如果為正數(shù),則離用戶更近,為負(fù)數(shù)則表示離用戶更遠(yuǎn)。
語法:
Object.style.zIndex=auto|number
可能的值
值 | 描述 |
---|---|
auto | 默認(rèn)。堆疊順序與父元素相等。 |
number | 設(shè)置元素的堆疊順序。 |
提示和注釋
注釋:元素可擁有負(fù)的 z-index 屬性值。
注釋:Z-index 僅能在定位元素上奏效(例如 position:absolute;)!
實例
本例改變元素的堆疊順序:
<html>
<head>
<style type="text/css">
#img1
{
position:absolute;
left:0px;
top:0px;
z-index:-1
}
</style>
<script type="text/javascript">
function changeStackOrder()
{
document.getElementById("img1").style.zIndex="1";
}
</script>
</head>
<body>
<h1>This is a Heading</h1>
<img id="img1" src="bulbon.gif" width="100" height="180">
<p>Default z-index is 0. Z-index -1 has lower priority.</p>
<input type="button" onclick="changeStackOrder()"
value="Change stack order" />
</body>
</html>