web-profile

jQuery .offset() position of an element relative to the document

The .offset() gets the position of an element relative to the document. Also check jQuery .position().

Code:

<script>
$(function(){
    var offset = $('.target').offset();
    $('.target').append( ": left=" + offset.left + ", top=" + offset.top );
});
</script>

html:

<h3 class="target">get offset position with jQuery</h3>​

css:

<style>
.target {
    margin:20px;
    padding:3px;
    position:absolute;
    left:10px;
    top:5px;
}​
</style>

Leave a Reply

Your email address will not be published. Required fields are marked *