Style top 属性

Style 对象参考手册 Style 对象

定义和用法

top 属性设置或返回定位元素的顶部位置。

该属性规定了元素的顶部位置,包括:内边距、滚动条、边框和外边距。

提示:一个定位元素是元素的 position 属性被设置为:relative(相对)、absolute(绝对)或 fixed(固定)。

语法

设置 top 属性:

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

返回 top 属性:

Object.style.top

描述
auto默认。通过浏览器计算顶部的位置。
length使用 px、cm 等单位定义元素的顶边到最近一个具有定位设置父元素的顶部边缘的位置。可使用负值。
%定义元素的顶边到最近一个具有定位设置父元素的顶部边缘的百分比位置。
inherittop 属性的值从父元素继承。


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 top 属性。

注意:IE7 及更早的版本不支持 "inherit" 值。IE8 只有规定了 !DOCTYPE 才支持 "inherit"。IE9 支持 "inherit"。


实例

实例

设置按钮的顶部位置:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本站教程(icodebang.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 对象参考手册 Style 对象

0 个评论

要回复文章请先登录注册