Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery UI API – .position()

jQuery UI API - .position()

Category

Method Overrides | Methods | Utilities

Usage

**Description:** Position an element relative to another element.

Returns:jQuery

**Version added:** 1.8

.position( options )

Parameters Type Description
options Object
  • my(default:"center")
    Type: String
    Description: Defines where theon the positioned elementaligns with the target element: a "horizontal vertical" alignment. A single value such as "right" is normalized to "right center", and "top" to "center top" (per the CSS convention below). Acceptable horizontal values: "left", "center", "right". Acceptable vertical values: "top", "center", "bottom". For example, "left top" or "center center". Each dimension can also contain an offset, in pixels or percentages, e.g. "right+10 top-25%". Percentage offsets are relative to the positioned element.
  • at(default:"center")
    Type: String
    Description: Defines where theOn the target element,it defines where the positioned element aligns: a "horizontal vertical" alignment. For more details, see the possible values for the my option. Percentage offsets are relative to the target element.
  • of(default:null)
    Type: Selector or Element or jQuery or Event
    Description: The element to position. If you provide a Selector or jQuery object, the first matched element is used. If you provide an Event object, the pageX and pageY properties are used, e.g. "#top-menu".
  • collision(default:"flip")
    Type: String
    Description: When the positioned element overflows the window in some direction, move it to another position. Like the my and at options, this option accepts a single value or a pair of horizontal/vertical values. For example: "flip", "fit", "fit flip", "fit none".
    • "flip": Flip the element to the opposite side of the target and run collision detection again to see if it fits. Whichever side allows more of the element to be visible is used.
    • "fit": Move the element away from the edges of the window.
    • "flipfit": First apply the flip logic, placing the element on the side that allows more of it to be visible. Then apply the fit logic to ensure as much of the element as possible is visible.
    • "none": Do not apply any collision detection.
  • using(default:null)
    Type: Function()
    Description: When this option is specified, the actual property setting is delegated to this callback. It accepts two arguments: the first is a hash of top and left values for the position, which can be passed to .css() or .animate(); the second provides feedback about the positions and dimensions of both elements, as well as their relative position. Both target and element have the following properties: element, left, top, width, height. In addition, horizontal, vertical, and important provide the twelve possible directions, e.g. { horizontal: "center", vertical: "left", important: "horizontal" }.
  • within(default:window)
    Type: Selector or Element or jQuery
    Description: The element to position within, which affects collision detection. If you provide a Selector or jQuery object, the first matched element is used.
  • |

    jQuery UI .position() The method allows you to position an element relative to the window, document, another element, or the cursor/mouse, without having to account for offsets relative to a parent element.

    Note: jQuery UI does not support positioning hidden elements.

    This is a standalone jQuery plugin with no dependencies on other jQuery UI components.

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

    Example

    A simple jQuery UI Position example.

    
    
    
    
    
    
    
    
      
    
      .position() 实例