jQuery offset() 方法

jQuery 教程 · 2019-03-27 15:12:29

实例

返回 <p> 元素的偏移坐标:

$("button").click(function(){ var x=$("p").offset(); alert("Top: " + x.top + " Left: " + x.left); });

定义和用法

offset() 方法设置或返回被选元素相对于文档的偏移坐标。

当用于返回偏移时:
该方法返回第一个匹配元素的偏移坐标。它返回一个带有两个属性(以像素为单位的 top 和 left 位置)的对象。

当用于设置偏移时:
该方法设置所有匹配元素的偏移坐标。

语法

返回偏移坐标:

  
    $(selector).offset()
  

设置偏移坐标:

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

使用函数设置偏移坐标:

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

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

可能的值:
  • 名/值对,比如 {top:100,left:100}
  • 一个带有 top 和 left 的对象(实例)
function(index,currentoffset) 可选。规定返回包含 top 和 left 坐标的对象的函数。
  • index - 返回集合中元素的 index 位置。
  • currentoffset - 返回被选元素的当前坐标。

点击查看所有 jQuery 教程 文章: https://www.codercto.com/courses/l/35.html

查看所有标签

Measure What Matters

Measure What Matters

John Doerr / Portfolio / 2018-4-24 / GBP 19.67

In the fall of 1999, John Doerr met with the founders of a start-up he’d just given $11.8 million, the biggest investment of his career. Larry Page and Sergey Brin had amazing technology, entrepreneur......一起来看看 《Measure What Matters》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具