<jQuery> <方法> 十六. offset()方法和position()方法

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .father {
            width: 400px;
            height: 400px;
            background-color: pink;
            position: relative;
            margin: 100px;
        }
        .son {
            width: 200px;
            height: 200px;
            background-color: cyan;
            position: absolute;
            left: 100px;
            top: 100px;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
<script src="jquery-3.2.1.js"></script>
<script>
    $(function () {
        // 获取元素相对于document的位置
        console.log($(".son").offset());

        // 获取元素相对于有定位的父元素的位置
        console.log($(".son").position());
    });
</script>
</body>
</html>
复制代码

 

posted @   云淡风轻我爱编程  阅读(144)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示