English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery offset() method

HTML do jQuery/Métodos CSS

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.

Syntax:

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))

Instance

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: 30
  
Teste 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‹/›

Valores dos Parâmetros

ParâmetrosDescrição
{top:value, left:valueEspecificar as coordenadas superior e esquerda em pixels
function(index, currentOffset)Especificar uma função que retorna um objeto contendo as coordenadas superior e esquerda
  • index-Retornar a Posição do Elemento no Conjunto

  • currentOffset-Retornar as Coordenadas Atuais do Elemento Selecionado

HTML do jQuery/Métodos CSS