HTML DOM position 屬性
定義和用法
position 屬性把元素放置到一個(gè)靜態(tài)的、相對的、絕對的、或固定的位置中。
語法:
Object.style.position=static|relative|absolute|fixed
可能的值
值 | 描述 |
---|---|
static | 默認(rèn)。位置設(shè)置為 static 的元素,它始終會(huì)處于頁面流給予的位置(static 元素會(huì)忽略任何 top、bottom、left 或 right 聲明)。 |
relative | 位置被設(shè)置為 relative 的元素,可將其移至相對于其正常位置的地方,因此 "left:20" 會(huì)將元素移至元素正常位置左邊 20 個(gè)像素的位置。 |
absolute | 位置設(shè)置為 absolute 的元素,可定位于相對于包含它的元素的指定坐標(biāo)。此元素的位置可通過 "left"、"top"、"right" 以及 "bottom" 屬性來規(guī)定。 |
fixed | 位置被設(shè)置為 fixed 的元素,可定位于相對于瀏覽器窗口的指定坐標(biāo)。此元素的位置可通過 "left"、"top"、"right" 以及"bottom" 屬性來規(guī)定。不論窗口滾動(dòng)與否,元素都會(huì)留在那個(gè)位置。工作于 IE7(strict 模式)。 |
實(shí)例
本例把元素位置由相對改為絕對:
<html>
<head>
<style type="text/css">
input
{
position:relative;
}
</style>
<script type="text/javascript">
function setPositionAbsolute()
{
document.getElementById("b1").style.position="absolute";
document.getElementById("b1").style.top="10px";
}
</script>
</head>
<body>
<p>This is an example paragraph</p>
<p>This is an example paragraph</p>
<input type="button" id="b1" onclick="setPositionAbsolute()"
value="Set button position to be absolute" />
</body>
</html>