xinggevip

jQuery之css属性offset()、position()
<!DOCTYPE html> <html lang="en"> &l...
扫描右侧二维码阅读全文
04
2019/07

jQuery之css属性offset()、position()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        body{
            padding: 0px;
            margin: 0px;
        }
        .wrapper{
            position: relative;
            left: 100px;
            top:100px;
            width: 300px;
            height: 300px;
            background: #ccc;


        }
        .one{
            position: absolute;
            top: 100px;
            left: 100px;
            width: 100px;
            height: 100px;
            background: blueviolet;
        }
    </style>
    <title>Document</title>
</head>
<body>
    <div class="wrapper">
        <div class="one">
            div
        </div>
    </div>

    
    <script src="../jqueryFile/jquery-3.4.1.js"></script>
    <script>
        console.log( $(".one").offset() );      // {top: 200, left: 200}    相对于屏幕
        console.log( $(".one").position() );    // {top: 100, left: 100}    相对于最近的有定位的父容器
        // 设置offset()、position()值
        $(".one").offset({
            left: 50,
            top: 50

        });

        console.log( $(".one").offset() );      // {top: 50, left: 50}    相对于屏幕
        console.log( $(".one").position() );    // {top: -50, left: -50}    相对于最近的有定位的父容器
    </script>
</body>
</html>
Last modification:July 4th, 2019 at 09:57 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment