jQuery CSS 操作 - offset() 方法 实例详解
jQuery CSS 操作 - offset() 方法
实例
获得 <p> 元素当前的偏移:
$(".btn1").click(function(){
x=$("p").offset();
$("#span1").text(x.left);
$("#span2").text(x.top);
});
完整实例:
<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
x=$("p").offset();
$("#span1").text(x.left);
$("#span2").text(x.top);
});
});
</script>
</head>
<body>
<p>本段落的偏移是 <span id="span1">unknown</span> left 和 <span id="span2">unknown</span> top。</p>
<button>获得 offset</button>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
定义和用法
offset() 方法返回或设置匹配元素相对于文档的偏移(位置)。
返回偏移坐标
返回第一个匹配元素的偏移坐标。
该方法返回的对象包含两个整型属性:top 和 left,以像素计。此方法只对可见元素有效。
语法
$(selector).offset()
完整实例:
<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
x=$("p").offset();
$("#span1").text(x.left);
$("#span2").text(x.top);
});
});
</script>
</head>
<body>
<p>本段落的偏移是 <span id="span1">unknown</span> left 和 <span id="span2">unknown</span> top。</p>
<button>获得 offset</button>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
设置偏移坐标
设置所有匹配元素的偏移坐标。
语法
$(selector).offset(value)
参数 | 描述 |
---|---|
value |
必需。规定以像素计的 top 和 left 坐标。 可能的值:
|
完整实例:
<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").offset({top:100,left:0});
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>设置新的偏移</button>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
使用函数来设置偏移坐标
使用函数来设置所有匹配元素的偏移坐标。
语法
$(selector).offset(function(index,oldoffset))
参数 | 描述 |
---|---|
function(index,oldoffset) |
规定返回被选元素新偏移坐标的函数。
|
完整实例:
<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").offset(function(n,c){
newPos=new Object();
newPos.left=c.left+100;
newPos.top=c.top+100;
return newPos;
});
});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<button>设置 p 元素的 offset 坐标</button>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
亲自试一试 - 实例
完整实例【使用对象来为对象设置新的 offset 值】:
<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
newPos=new Object();
newPos.left="0";
newPos.top="100";
$(document).ready(function(){
$("button").click(function(){
$("p").offset(newPos);
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>设置新的偏移</button>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
使用新对象中的坐标来定位元素。
完整实例【使用另一个元素的位置来为元素设置新的 offset 值】:
<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").offset($("span").offset());
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>为段落设置偏移</button>
<span id="middlepos" style="position:absolute;left:100px;top:150px;">This is a span</span>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
使用已有对象的位置来定位元素。