Get the offset of an element using .offset()

.offset()


Description: Get the coordinates for the first matched element, relative to the document.


Example:

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>offset demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
    </head>
    <body>
      
      <div class="mycontainer">This is my container</div>
      <div class="result"></div>
      
      <script>
         
         bitsyCode.ready(function(){
            var offset = bitsyCode(".mycontainer").offset();
            bitsyCode(".result").append("Top: " + offset.top + "<br /> Left: "+ offset.left);
         })
         
      </script>
     
    </body>
    </html>

Preview:

This is my container
  • Usage

    • How to use:

      Get the coordinates for the first matched element, relative to the document using .offset()

    • USAGE:

      bitsyCode("element").offset();
  • Options

    • What options are there:

      There are no options for this method.

  • Returns

    • Return: { top:0, left:0 }   // Returns an object with the top and left position of the element.
Share