溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

jQuery Position方法使用和兼容性

發(fā)布時間:2020-09-29 09:24:39 來源:腳本之家 閱讀:323 作者:仰望星空,腳踏實地 學以致用 欄目:web開發(fā)

1、position方法

jquery api地址:http://jquery.cuishifeng.cn/position.html

position方法獲取匹配元素相對父元素的偏移。

2、說明

2.1 與offset()區(qū)別

.offset()是獲得該元素相對于documet的當前坐標

.position()方法可以取得元素相對于父元素的偏移位置,父元素為該元素最近的而且被定位過的祖先元素。

2.2 值計算

.元素本身所占用的邊框,邊距和填充的大小不計。

.父元素的邊框和邊距不計,父元素的填充計算在內。

3、示例代碼

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>jQuery position()示例</title>
    <style>
      html {
        line-height: 1.15;
      }
      /*父元素--相對定位*/
      .parent {
        position: relative;
        width: 200px;
        height: 400px;
        /*父元素的margin不計算在內*/
        margin-top: 10px;
        /*父元素的border不計算在內*/
        border: 1px solid green;
        /*父元素的padding計算在內*/
        padding-top: 10px;
      }
      .child-1 {
        width: 100px;
        height: 100px;
        margin: 0 auto;
        border: 1px solid #2E8DED;
      }
      .child-2 {
        width: 100px;
        height: 100px;
        /*子元素的margin不計算在內*/
        margin: 10px auto 0;
        /*子元素的border不計算在內*/
        border: 1px solid #2E8DED;
        /*子元素的padding不計算在內*/
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      <p class="child-1">
        first child
      </p>
      <p class="child-2" id="no-2">
        second child
      </p>
    </div>
    <script src=".output/js/jquery-1.12.4.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        //獲取child-2子元素距離父元素的距離
        console.log($('#no-2').position().top);
      });
    </script>
  </body>
</html>

4、注意

對于文字的line-height等屬性,瀏覽器(chrome、IE、Firefox)默認大小不一致,因此不同的瀏覽器position()在計算尺寸時會存在不一致,因此必須保證所有瀏覽器一致的line-height等屬性。

示例代碼為沒有設置line-height的例子,position()在不同的瀏覽器上計算出的值不一樣。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>jQuery position()示例</title>
    <style>
      html {
        /*必須給予一致的設置,否則position()計算值不同*/
        /*line-height: 1.15;*/
      }
      /*父元素--相對定位*/
      .parent {
        position: relative;
        width: 200px;
        height: 400px;
        /*父元素的margin不計算在內*/
        margin-top: 10px;
        /*父元素的border不計算在內*/
        border: 1px solid green;
        /*父元素的padding計算在內*/
        padding-top: 10px;
      }
      .child-1 {
        width: 100px;
        height: 100px;
        margin: 0 auto;
        border: 1px solid #2E8DED;
      }
      .child-2 {
        width: 100px;
        height: 100px;
        /*子元素的margin不計算在內*/
        margin: 10px auto 0;
        /*子元素的border不計算在內*/
        border: 1px solid #2E8DED;
        /*子元素的padding不計算在內*/
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <div class="parent">
      文字文字
      <p class="child-1">
        first child
      </p>
      <p class="child-2" id="no-2">
        second child
      </p>
    </div>
    <script src=".output/js/jquery-1.12.4.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        //獲取child-2子元素距離父元素的距離
        console.log($('#no-2').position().top);
      });
    </script>
  </body>
</html>

總結

以上所述是小編給大家介紹的jQuery Position方法使用和兼容性,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI