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>
