.offset()
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> p { margin-left:10px; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ var p = $("p:last"); var offset = p.offset(); p.html( "left: " + offset.left + ", top: " + offset.top ); }); </script> </head> <body> <p>Hello</p><p>2nd Paragraph</p> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <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> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("*", document.body).click(function (e) { var offset = $(this).offset(); e.stopPropagation(); $("#result").text(this.tagName + " coords ( " + offset.left + ", " + offset.top + " )"); }); }); </script> </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>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> p { margin-left:10px; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p:last").offset({ top: 10, left: 30 }); }); </script> </head> <body> <p>Hello</p><p>2nd Paragraph</p> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท