הוספה בגרסה: 1.2
מחזיר ערך נוכחי של קיזוז של אלמנט ראשון בהתאמה, בנקודות, יחסית למסמך.
אובייקט מוחזר מכיל שני מאפיינים
Float
, עיליון ושמאל.
דפדפנים בדרך כלל מעגלים ערכים האלה למספר שלם הקרוב בנקודות עבור המיקום בפועל.
שיטה עובדת רק עבור אלמנטים נראים.
דוגמאות
דוגמה 1
מקבל מרווח של פסקה השניה:דוגמה 1 - קוד מלא:
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.5.js"></script> <script> $(document).ready(function(){ var p = $("p:last"); var offset = p.offset(); p.html( "left: " + offset.left + ", top: " + offset.top ); }); </script> <style> p { margin-left:10px; } </style> </head> <body> <p>Hello</p><p>2nd Paragraph</p> </body> </html>
דוגמה 2
לחצו כדי לראות קואורדינטות.דוגמה 2 - קוד מלא:
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.5.js"></script> <script> $(document).ready(function(){ $("*", document.body).click(function (e) { var offset = $(this).offset(); e.stopPropagation(); $("#result").text(this.tagName + " coords ( " + offset.left + ", " + offset.top + " )"); }); }); </script> <style> p { margin-left:10px; color:blue; width:200px; cursor:pointer; } span { color:red; cursor:pointer; } div.abs { width:50px; height:50px; position:absolute; left:220px; top:35px; background-color:green; cursor:pointer; } </style> </head> <body> <div id="result">Click an element.</div> <p> This is the best way to <span>find</span> an offset. </p> <div class="abs"> </div> </body> </html>
האם מידע זה היה מועיל?

