坐標(biāo)值操作

    <style>
        body {
            height: 5000px;
        }
        .box1 {
            width: 300px;
            height: 300px;
            position: relative;
            margin: 10px;
            overflow: auto;
            background-color: pink;
        }
        .box2 {
            width: 200px;
            height: 400px;
            position: absolute;
            top: 50px;
            left: 50px;
            background-color: yellow;
        }
    </style>
    <script src="jquery-1.11.1.js"></script>
    <script>
        $(function () {
            //距離頁(yè)面最頂端或者最左側(cè)的距離和有沒有定位沒有關(guān)系
            $("button").eq(0).click(function () {
                alert($(".box2").offset().top);
            })

            //距離頁(yè)面最頂端或者最左側(cè)的距離和有沒有定位沒有關(guān)系
            $("button").eq(1).click(function () {
                $(".box2").offset({"left":1000,"top":1000});
            })

            //距離父系盒子中帶有定位的盒子的距離(獲取的就是定位值,和margin/padding無關(guān))
            $("button").eq(2).click(function () {
                alert($(".box2").position().top);
            })

            //距離父系盒子中帶有定位的盒子的距離(獲取的就是定位值,和margin/padding無關(guān))
            $("button").eq(3).click(function () {
                $(".box2").position().top = "100px";
            })

            //獲取被選取的頭部
            $("button").eq(4).click(function () {
                alert($(window).scrollTop());
            })

            //獲取被選取的頭部
            $("button").eq(5).click(function () {
                $(window).scrollTop(100);
            })

        })
    </script>

</head>
<body>


<div class="box1">
    <div class="box2"></div>
</div>

<button>offset().top獲取</button>
<button>offset().top設(shè)置</button>
<button>position().top獲取</button>
<button>position().top設(shè)置</button>
<button>scrollTop()</button>
<button>scrollTop()</button>

</body>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容