jquery 中的 offset()

一.语法:

1.返回偏移坐标

  $(select).offset();

  top:  $(select).offset().top;

  left:  $(select).offset().left;

 

2.设置偏移坐标

   $(select).offset({top:value,left:value});

   参数的含义:

   {top:value,left:value} 当设置偏移时是必需的。规定以像素为单位的 top 和 left 坐标。

 

  3、使用函数设置偏移坐标:

        $(selector).offset(function(index,currentoffset));

        可选。规定返回包含 top 和 left 坐标的对象的函数。
        index - 返回集合中元素的 index 位置。
        currentoffset - 返回被选元素的当前坐标。

 

二. 实例

 <!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){

      名/值 对
       $("button").click(function(){
             $("p").offset({top:200,left:200});
       });

     对象

$("button").click(function(){
newPos=new Object();
newPos.left="0";
newPos.top="100";
$("p").offset(newPos);
});

      函数

$("p").offset(function(n,c){
newPos=new Object();
newPos.left=c.left+100;
newPos.top=c.top+100;
return newPos;
});


});
</script>
</head>
<body>

<p style="border:1px solid red">This is a paragraph.</p>
<button>Set the offset coordinates of the p element</button>

</body>
</html>

 

三.注意事项

ffset() 方法 返回的top , left. 跟 margin-top,margin-left 也有关系。

      如果元素有margin-top,margin-left. 它获取当前的margin. 没有则是默认取值。

      如果父元素也有margin,broder 的话。它也会受到影响。取值要更大。 因为offset() 取的当前与文档的偏移坐标。

 

转载于:https://www.cnblogs.com/vvWeb/p/5564098.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值