English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The offset() method gets or sets the offset coordinates of the selected element relative to the document.
When using the offset() methodGetWhen setting the offset, it will returnThe first selected elementThe offset coordinates (including2an object of properties (top and left))。
When using the offset() methodSetWhen setting the offset, it will setAll selected elementsoffset coordinates.
Get the offset coordinates:
$("selector").offset()
Set the offset coordinates:
$("selector").offset({top:value, left:value})
Usar função para definir coordenadas de deslocamento:
$("selector").offset(function(index, currentOffset))
Get the offset coordinates of the paragraph:
$("button").click(function(){ let p = $("p"); let offset = p.offset(); p.html("left: ", + offset.left + ", top: ", + offset.top);Teste e Veja‹/›
Set the offset coordinates of all paragraphs:
$("button").click(function(){ $("p").offset({ top: 60, left: 30Teste e Veja‹/›
Configure a element's offset coordinates using the offset coordinates of another element:
$("button").click(function(){ $("p").offset($("div").offset());Teste e Veja‹/›
Usar função para definir coordenadas de deslocamento:
$("button").click(function(){ $("p").offset(function(i, val){ let newCord = new Object(); newCord.left = val.left + 100; newCord.top = val.top + 100; return newCord;Teste e Veja‹/›
Parâmetros | Descrição |
---|---|
{top:value, left:value | Especificar as coordenadas superior e esquerda em pixels |
function(index, currentOffset) | Especificar uma função que retorna um objeto contendo as coordenadas superior e esquerda
|