Добавлена в версию: 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>
Была ли эта информация полезной?

