原型 - viewportOffset() 方法
此方法返回相对于视口的元素的 X/Y 坐标。返回的位置将是绝对位置。
注意 - 虽然都用像素表示,但所有值均以数字形式返回。
语法
element.viewportOffset();
返回值
数字 [Left, Top] 数组。
实例
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
var coordinates = $('visible').viewportOffset();
alert("Distance from the Left : " + coordinates[0] );
alert("Distance from the Top : " + coordinates[1] );
}
</script>
</head>
<body>
<div id = "visible" style = "position:absolute;left:50px; top:50px;">
This is visible division 50 pixels down from the top.
</div>
<input type = "button" value = "Click" onclick = "showResult();"/>
</body>
</html>
输出
prototype_element_object.htm
广告