zoukankan      html  css  js  c++  java
  • jquery position方法使用及兼容性

    1、position方法

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

    position方法获取匹配元素相对父元素的偏移。

    2、说明

    2.1 与offset()区别

    .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>
  • 相关阅读:
    42 最大子数组Ⅱ
    笔试之const问题
    笔试中sizeof求字节数的问题
    40 用栈实现队列
    38 搜索二维矩阵Ⅱ
    25.Remove Nth Node From End of List(删除链表的倒数第n个节点)
    29.最小的K个数
    28.数组中出现次数超过一半的数字
    27.字符串的排列
    26.二叉搜索树与双向链表
  • 原文地址:https://www.cnblogs.com/mengfangui/p/7418150.html
Copyright © 2011-2022 走看看