js原生offsetParent解析

來源:互聯網
上載者:User

標籤:blank   java   targe   round   bsp   net   document   head   https   

offsetParent是個僅僅讀屬性,返回近期顯示指定位置的容器元素的引用。

假設元素沒有指定位置,近期的元素或者根項目(標準模式下是html,怪異模式下是body)就是offsetParent。

當元素的style.display=‘none‘時。offsetParent返回null。

因為offsetTop和offsetLeft是相對於頁面邊界,因此offsetParent是很實用的。

     瀏覽器安全色性     在以WebKit為核心的瀏覽器上,假設元素是隱藏的(該元素或者上階項目style.display=‘none‘)或者元素自身style.position=‘fixed‘,那麼就會返回null。

    在IE(9)上如元素style.position=‘fixed‘,該屬性就會返回null。(然而display:none不影響這個瀏覽器)
<!DOCTYPE html><html><head>  <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>  <title>offsetParent屬性</title>  <style type="text/css">    div {      margin: 5px;    }    #div11 {      border: 2px solid #0000ff;    }    #div21 {      position: relative;      border: 2px solid #ffff00;    }    #div30 {      position: relative;      border: 2px solid red;    }    #div31 {    }  </style>  <script type="text/javascript">    function initPage() {      getOffsetParent(‘div12‘); //BODY      getOffsetParent(‘div22‘);//DIV      getOffsetParent(‘div32‘);//BODY    }    function getOffsetParent(id) {      var divEl = document.getElementById(id);      var el = divEl.offsetParent;      console.log(el.nodeName);      console.log(‘element id is ‘ + el.getAttribute(‘id‘) || ‘‘);    }  </script></head><body onload="initPage()"><div id="div11">  <div id="div12">測試1</div></div><div id="div21">  <div id="div22">測試2</div></div><div id="div30">  <div id="div31">    <div id="div32">測試3</div>  </div></div></body></html>


https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.offsetParent

js原生offsetParent解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.