1. 程式人生 > 實用技巧 >HTML DOM Style top 屬性

HTML DOM Style top 屬性

[

Style top 屬性
Style 物件

定義和用法

top 屬性設定或返回定位元素的頂部位置。
該屬性規定了元素的頂部位置,包括:內邊距、滾動條、邊框和外邊距。
提示:一個定位元素是元素的 position 屬性被設定為:relative(相對)、absolute(絕對)或 fixed(固定)。

語法

設定 top 屬性:

Object.style.top="auto|length|%|inherit"

返回 top 屬性:

Object.style.top

描述
auto 預設。通過瀏覽器計算頂部的位置。
length 使用 px、cm 等單位定義元素的頂邊到最近一個具有定位設定父元素的頂部邊緣的位置。可使用負值。
% 定義元素的頂邊到最近一個具有定位設定父元素的頂部邊緣的百分比位置。
inherit top 屬性的值從父元素繼承。

瀏覽器支援

所有主要瀏覽器都支援 top 屬性。
注意:IE7 及更早的版本不支援 "inherit" 值。IE8 只有規定了 !DOCTYPE 才支援 "inherit"。IE9 支援 "inherit"。

例項

例項

設定按鈕的頂部位置:


<!DOCTYPE html>

<html>

<head> 

<meta charset="utf-8"> 

<title>Break易站(runoob.com)</title> 

<style type="text/css">

#b1{

    position:absolute;

}

</style>

<script>

function displayResult(){

    document.getElementById("b1").style.top="100px";

}

</script>

</head>

<body>


<input type="button" id="b1" onclick="displayResult()" value="設定頂部位置為100 px">


</body>

</html>

Style 物件

]
  •   本文標題:HTML DOM Style top 屬性 - Break易站轉載請保留頁面地址:https://www.breakyizhan.com/html/22444.html