jQuery偏移量offset


jQuery偏移量offset

jquery的參考文檔地址:http://jquery.cuishifeng.cn/

獲取匹配元素在當前視口的相對偏移。參照物是可視窗口。

返回的對象包含兩個整型屬性:top 和 left,以像素計。此方法只對可見元素有效。

position和offset相似,position的參照物是父親窗口,必須是已經定位的父親窗口。

初始:

點擊按鈕之后:

點擊按鈕設置偏移量,代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        .c1{
            width: 200px;
            height: 200px;
            background-color: #2459a2;
        }
    </style>
</head>
<body>



<div class="c1"></div>
<button>change</button>
<script src="jquery-3.1.1.js"></script>

<script>

    // offset方法的參照物是可視窗口
    console.log($(".c1").offset());         // 偏移量對象
    console.log($(".c1").offset().top);     // 偏移量對象
    console.log($(".c1").offset().left);     // 偏移量對象
    
//    var top1=100
    $("button").click(function () {
        $(".c1").offset({"top":100,left:200})
    })
</script>
</body>
</html>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM