webAPI

offsetLeft和offsetTop

2019-05-13  本文已影响0人  椋椋夜色

<!DOCTYPE html>
<html lang="zh-CN">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> offsetLeft和offsetTop </title>

    <!-- offsetLeft:
            获取自身左外边框到定位父级的左内边框的距离
            offsetTop
            获取自身上外边框到定位父级的上内边框的距离

            只能获取不能修改 -->

    <style>
        .box {
            width: 200px;
            height: 200px;
            background-color: #0f0;
            position: relative;
            border: 10px solid black;
        }

        .box1 {
            width: 100px;
            height: 100px;
            background-color: #f00;
            position: absolute;
            margin-left: 50px;
            left: 50px;
            margin-top: 20px;
            top: 110px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="box1"></div>
    </div>

    <script>
        var box = document.getElementsByClassName('box')[0];
        var box1 = document.querySelector('.box1');
        console.log(box1.offsetLeft, box1.offsetTop);       
    </script>

</body>

</html>

上一篇下一篇

猜你喜欢

热点阅读