当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML DOMRect top用法及代码示例


在Web API中,有一个DOMRect接口,该接口具有一个属性top,该属性使我们在DOMRect对象的顶部。它返回y坐标值,或者如果高度为负,则返回y +高度。

用法:

var recX = DOMRect.top;

返回类型:



Double value

范例1:当高度为正时

<!DOCTYPE html> 
<html> 
  
<head> 
  
    <title> 
        DOMRect top property 
    </title> 
  
</head> 
  
<body> 
    <center> 
  
        <h1 style="color:green;">  
        GeeksForGeeks  
        </h1> 
  
        <h2>DOMRect top property</h2> 
        <button onclick="getDOMRect ();"> 
          Get top 
      </button> 
        <p id='DOMRect'></p> 
    </center> 
  
</body> 
  
<script type="text/javascript"> 
    function getDOMRect() { 
        var myDOMRect = new DOMRect(0, 0, 100, 100); 
        var rectop = myDOMRect.top; 
  
        document.getElementById( 
          'DOMRect').innerHTML = rectop; 
  
    } 
</script> 
  
</html>

输出:
在单击按钮之前:

单击按钮后:

范例2:当高度为负数时

<!DOCTYPE html> 
<html> 
  
<head> 
  
    <title> 
        DOMRect top property 
    </title> 
  
</head> 
  
<body> 
    <center> 
  
        <h1 style="color:green;">  
                GeeksForGeeks  
            </h1> 
  
        <h2>DOMRect top property</h2> 
        <button onclick="getDOMRect ();"> 
          Get top 
      </button> 
        <p id='DOMRect'></p> 
    </center> 
  
</body> 
  
<script type="text/javascript"> 
    function getDOMRect() { 
        var myDOMRect = new DOMRect(0, 0, 100, -100); 
        var rectop = myDOMRect.top; 
  
        document.getElementById('DOMRect').innerHTML = rectop; 
  
    } 
</script> 
  
</html>

输出:
在单击按钮之前:

单击按钮后:

支持的浏览器:下面列出了DOMRect top属性支持的浏览器:

  • 谷歌浏览器
  • Safari 10.1
  • Firefox
  • Opera



相关用法


注:本文由纯净天空筛选整理自DeepakDev大神的英文原创作品 Web API | DOMRect top property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。