jQuery offset()เมธอด

❮ วิธีการ jQuery HTML/CSS

ตัวอย่าง

กลับพิกัดออฟเซ็ตของ <p> องค์ประกอบ:

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

ความหมายและการใช้งาน

offset() วิธีตั้งค่าหรือส่งคืนพิกัดออฟเซ็ตสำหรับองค์ประกอบที่เลือกซึ่งสัมพันธ์กับเอกสาร

เมื่อใช้เพื่อคืนค่าออฟเซ็ต:
วิธีนี้จะคืนค่าพิกัดออฟเซ็ตขององค์ประกอบที่ตรงกัน FIRST ส่งคืนวัตถุที่มีคุณสมบัติ 2 อย่าง; ตำแหน่งด้านบนและด้านซ้ายเป็นพิกเซล

เมื่อใช้เพื่อตั้งค่าออฟเซ็ต:
วิธีนี้จะตั้งค่าพิกัดออฟเซ็ตขององค์ประกอบที่ตรงกันทั้งหมด


ไวยากรณ์

ส่งกลับพิกัดออฟเซ็ต:

$(selector).offset()

ตั้งค่าพิกัดออฟเซ็ต:

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

ตั้งค่าพิกัดออฟเซ็ตโดยใช้ฟังก์ชัน:

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

Parameter Description
{top:value,left:value} Required when setting the offset. Specifies the top and left coordinates in pixels.

Possible values:
  • Name/Value pairs, like {top:100,left:100}
  • An object with top and left properties ()
function(index,currentoffset) Optional. Specifies a function that returns an object containing the top and left coordinates
  • index - Returns the index position of the element in the set
  • currentoffset - Returns the current coordinates of the selected element

ลองด้วยตัวคุณเอง - ตัวอย่าง


วิธีการตั้งค่าพิกัดออฟเซ็ตขององค์ประกอบ


การใช้ฟังก์ชันเพื่อตั้งค่าพิกัดออฟเซ็ตขององค์ประกอบ


วิธีตั้งค่าพิกัดออฟเซ็ตสำหรับองค์ประกอบโดยใช้ออบเจกต์ใหม่


วิธีตั้งค่าพิกัดออฟเซ็ตสำหรับองค์ประกอบโดยใช้พิกัดออฟเซ็ตขององค์ประกอบที่มีอยู่


❮ วิธีการ jQuery HTML/CSS