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

Metodo offset() di jQuery

jQuery遍历方法

Il metodo offset() di jQuery ottiene o imposta le coordinate di offset relativamente al documento dell'elemento selezionato.

quando si utilizza il metodo offset()ottenereimpostare l'offset, esso restituiràil primo elemento selezionatole coordinate di offset (un oggetto con 2 attributi: top e sinistra)).

quando si utilizza il metodo offset()impostareimpostare l'offset, esso imposteràtutti gli elementi selezionatile coordinate di offset.

sintassi:

ottenere le coordinate di offset:

$("selettore").offset()

impostare le coordinate di offset:

$("selettore").offset({cima: valore, sinistra: valore})

$("p").offset($("div").offset());

$("selettore").offset(function(index, currentOffset))

esempio

ottenere le coordinate di offset del paragrafo:

示例
  let p = $("p");
  let offset = p.offset();
  p.html("sinistra: " + offset.left + "  ", "cima: " + offset.top);
return newCord;
});

impostare le coordinate di offset di tutti i paragrafi:

示例
  $("p").offset({
    cima: 60,
    sinistra: 30
  return newCord;
return newCord;
});

使用另一个元素的偏移量坐标设置元素的偏移量坐标:

示例
  使用另一个元素的偏移量坐标设置元素的偏移量坐标:
return newCord;
});

$("p").offset($("div").offset());

示例
  $("button").click(function(){
    $("p").offset(function(i, val){
    let newCord = new Object();
    newCord.left = val.left + 100;
    newCord.top = val.top + 100;
  return newCord;
return newCord;
});

测试看看‹/›

参数值参数
描述, left:{top:, left:value}
指定像素的顶部和左侧坐标function(index, currentOffset)
  • 指定一个函数,该函数返回包含顶部和左侧坐标的对象index-

  • 返回元素在集合中的索引位置currentOffset-

jQuery遍历方法