jQuery offset() get element position relative to the document

Description

jQuery offset() get element position relative to the document

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Find Element's Position Relative to the Document</title>
<style>
    #box{//  w  ww .  j  a va 2  s  .c  om
        width:150px;
        height:100px;
        background: orange;
        margin: 150px 100px;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function() {
        $("button").click(function(){
            var offset = $("#box").offset();
            document.getElementById("demo").innerHTML = 
              "left: " + offset.left + ", top: " + offset.top;
        });
    });
</script>
</head>
<body>
    
    <p id="demo"></p>
  <button type="button">Get Box Position</button>
    <div id="box"></div>
</body>
</html>



PreviousNext

Related