Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery position method

As per Jquery API documentation:

.position()Returns: Object

Description: Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.

This method does not accept any arguments.Reference here

But somewhere i found using this:

$("#position1").position({
  my: "center",
  at: "center",
  of: "#targetElement"
});

An object has been passed to position method .Isn't this against API documentation?It seems that the properties passed to an object above has some special meaning.What are those properties stating .What they do?I m a complete beginner to jquery.So may be i m wrong .

like image 770
Maizere Pathak.Nepal Avatar asked May 01 '13 12:05

Maizere Pathak.Nepal


People also ask

How does jQuery position work?

jQuery position() MethodThe position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

What is offset () in jQuery?

jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.

What is the difference between position and offset in jQuery?

Difference between offset() and position() Method:The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element. When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method.

What is the difference between position and offset?

The offset() method retrieves the current position relative to the document, whereas the position() method retrieves the current position of an element relative to the parent element.


4 Answers

As per the jQuery API for the Position

  1. my: Defines which position on the element being positioned to align with the target element.
  2. at: Defines which position on the target element to align the positioned element against,
  3. of: Is for the element to position against. If you provide a selector, the first matching element will be used. Example: "#targetElement" in your case.
like image 26
palaѕн Avatar answered Sep 30 '22 04:09

palaѕн


This variant of .position() is part of the jQuery UI position utility. It gives you an easy way to place an element relative to another one (or the mouse cursor) in a certain way.

You are totally right that the original position() method does not accept arguments... but:

This plugin extends jQuery's built-in .position() method. If jQuery UI is not loaded, calling the .position() method may not fail directly, as the method still exists. However, the expected behavior will not occur.

like image 84
ThiefMaster Avatar answered Sep 30 '22 06:09

ThiefMaster


Check this out - http://docs.jquery.com/UI/API/1.8/Position

That feature is in jqueryUI position utility not in Core jQuery

like image 40
Mohammad Adil Avatar answered Sep 30 '22 06:09

Mohammad Adil


Let's take it to the codez ! A quick glance into the jQuery 1.9.1 source reveals:

position: function() {
    if ( !this[ 0 ] ) {
        return;
    }

    var offsetParent, offset,
        parentOffset = { top: 0, left: 0 },
        elem = this[ 0 ];

    // fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is it's only offset parent
    if ( jQuery.css( elem, "position" ) === "fixed" ) {
        // we assume that getBoundingClientRect is available when computed position is fixed
        offset = elem.getBoundingClientRect();
    } else {
        // Get *real* offsetParent
        offsetParent = this.offsetParent();

        // Get correct offsets
        offset = this.offset();
        if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
            parentOffset = offsetParent.offset();
        }

        // Add offsetParent borders
        parentOffset.top  += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
        parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
    }

    // Subtract parent offsets and element margins
    // note: when an element has margin: auto the offsetLeft and marginLeft
    // are the same in Safari causing offset.left to incorrectly be 0
    return {
        top:  offset.top  - parentOffset.top - jQuery.css( elem, "marginTop", true ),
        left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true)
    };
},

No arguments read, no arguments used. Wherever you saw that code, its not jQuery core. Most likely, its because the original author used the jQuery UI, which extends that method.

like image 20
jAndy Avatar answered Sep 30 '22 04:09

jAndy