HTML DOM top 屬性
定義和用法
top 屬性設置一個定位元素的上外邊距邊界與其包含塊上邊界之間的偏移。
語法:
Object.style.top=auto|%|length
可能的值
值 | 描述 |
---|---|
auto | 默認。通過瀏覽器來計算頂部的位置。 |
% | 設置元素的頂部到最近一個具有定位設置父元素的上邊緣的百分比位置。 |
length | 使用 px、cm 等單位設置元素的頂部到最近一個具有定位設置上邊緣的頂部的位置。可使用負值。 |
提示和注釋
注釋:如果 "position" 屬性的值為 "static",那么設置 "top" 屬性不會產(chǎn)生任何效果。
實例
本例設置元素的上邊界:
<html>
<head>
<style type="text/css">
input
{
position:absolute;
}
</style>
<script type="text/javascript">
function setTopEdge()
{
document.getElementById("b1").style.top="100px";
}
</script>
</head>
<body>
<input type="button" id="b1" onclick="setTopEdge()"
value="Set top edge to 100 px" />
</body>
</html>