jQuery UI API - Resizable Widget
Category
Usage
**Description:** Change the size of an element using the mouse.
**Version added:** 1.0
Dependencies:
**Note:** The jQuery UI Resizable plugin makes selected elements resizable (meaning they have draggable resize handles). You can specify one or more handles, as well as minimum and maximum widths and heights.
**Additional notes:** This widget requires some functional CSS, otherwise it will not work. If you create a custom theme, use the widget-specific CSS file as a starting point.
Quick navigation
| Option | Method | Events |
|---|---|---|
| Option | Type | Description | Default value |
|---|---|---|---|
| alsoResize | Selector or jQuery or Element | One or more elements to be resized in sync with the resizable element. |
代码实例:
初始化带有指定 alsoResize 选项的 resizable:
$( ".selector" ).resizable({ alsoResize: "#mirror" });
After initialization, get or set alsoResize Option:
// getter var alsoResize = $( ".selector" ).resizable( "option", "alsoResize" );| false| onSelect | event | animate | Boolean | Animates to the final size after resizing.// setter $( ".selector" ).resizable( "option", "alsoResize", "#mirror" );
Code example:
Initializes with the specified animate The resizable option:
$( ".selector" ).resizable({ animate: true });
After initialization, get or set animate Option:
// getter var animate = $( ".selector" ).resizable( "option", "animate" );| false| onSelect | event | animateDuration | Number or String | When using// setter $( ".selector" ).resizable( "option", "animate", true );
animate When the option changes, the duration of the animation.
Supports multiple types:
- Number: A duration in milliseconds.
- String: a named duration, such as
"slow"or"fast"。
Code example:
Initializes with the specified animateDuration The resizable option:
$( ".selector" ).resizable({ animateDuration: "fast" });
After initialization, get or set animateDuration Option:
// getter var animateDuration = $( ".selector" ).resizable( "option", "animateDuration" );| "slow"| onSelect | event | animateEasing | String | When using// setter $( ".selector" ).resizable( "option", "animateDuration", "fast" );
animate when using the option Easings。
Code example:
Initializes with the specified animateEasing The resizable option:
$( ".selector" ).resizable({ animateEasing: "easeOutBounce" });
After initialization, get or set animateEasing Option:
// getter var animateEasing = $( ".selector" ).resizable( "option", "animateEasing" );| "swing"| onSelect | event | aspectRatio | Boolean or Number | Whether the element should be constrained to a specific aspect ratio.// setter $( ".selector" ).resizable( "option", "animateEasing", "easeOutBounce" );
Supports multiple types:
- Boolean: when set to
truewhen, the element maintains its original aspect ratio. - Number: Forces the element to maintain a specific aspect ratio while resizing.
Code example:
Initializes with the specified aspectRatio The resizable option:
$( ".selector" ).resizable({ aspectRatio: true });
After initialization, get or set aspectRatio Option:
// getter var aspectRatio = $( ".selector" ).resizable( "option", "aspectRatio" );| false| onSelect | event | autoHide | Boolean | Whether to hide the handles when the user's mouse is not hovering over the element.// setter $( ".selector" ).resizable( "option", "aspectRatio", true );
Code example:
Initializes with the specified autoHide The resizable option:
$( ".selector" ).resizable({ autoHide: true });
After initialization, get or set autoHide Option:
// getter var autoHide = $( ".selector" ).resizable( "option", "autoHide" );| false| onSelect | event | cancel | Selector | Prevents resizing from starting on the specified elements.// setter $( ".selector" ).resizable( "option", "autoHide", true );
Code example:
Initializes with the specified cancel The resizable option:
$( ".selector" ).resizable({ cancel: ".cancel" });
After initialization, get or set cancel Option:
// getter var cancel = $( ".selector" ).resizable( "option", "cancel" );| "input, textarea, button, select, option"| onSelect | event | containment | Selector or Element or String | Constrains resizing to within the bounds of the specified element or region.// setter $( ".selector" ).resizable( "option", "cancel", ".cancel" );
Multiple types are supported:
- Selector: The resizable element will be contained within the bounds of the first element matched by the selector. If no element is found, containment is not set.
- Element: The resizable element will be contained within the bounds of the element.
- String: possible values:
"parent"、"document"。
Code example:
Initializes with the specified containment The resizable option:
$( ".selector" ).resizable({ containment: "parent" });
After initialization, get or set containment Option:
// getter var containment = $( ".selector" ).resizable( "option", "containment" );| false| onSelect | event | delay | Number | The time in milliseconds after mousedown until resizing starts. If this option is specified, resizing starts only after the mouse has moved following that delay. This option can prevent unwanted resizing when clicking on an element.// setter $( ".selector" ).resizable( "option", "containment", "parent" );
Code example:
Initializes with the specified delay The resizable option:
$( ".selector" ).resizable({ delay: 150 });
After initialization, get or set delay Option:
// getter var delay = $( ".selector" ).resizable( "option", "delay" );| 0| onSelect | event | disabled | Boolean | If set to// setter $( ".selector" ).resizable( "option", "delay", 150 );
true, then the resizable is disabled.
Code example:
Initializes with the specified disabled The resizable option:
$( ".selector" ).resizable({ disabled: true });
After initialization, get or set disabled Option:
// getter var disabled = $( ".selector" ).resizable( "option", "disabled" );| false| onSelect | event | distance | Number | The distance in pixels the mouse must move after mousedown before resizing starts. If this option is specified, resizing starts only after the mouse has moved beyond the specified distance. This option can prevent unwanted resizing when clicking on an element.// setter $( ".selector" ).resizable( "option", "disabled", true );
Code example:
Initializes with the specified distance The resizable option:
$( ".selector" ).resizable({ distance: 30 });
After initialization, get or set distance Option:
// getter var distance = $( ".selector" ).resizable( "option", "distance" );| 1| onSelect | event | ghost | Boolean | If set to// setter $( ".selector" ).resizable( "option", "distance", 30 );
true, then a semi-transparent helper element is displayed for resizing.
Code example:
Initializes with the specified ghost The resizable option:
$( ".selector" ).resizable({ ghost: true });
After initialization, get or set ghost Option:
// getter var ghost = $( ".selector" ).resizable( "option", "ghost" );| false| onSelect | event | grid | Array | Align the resizable element to the grid, every x and y pixels. The array form must be// setter $( ".selector" ).resizable( "option", "ghost", true );
[ x, y ]。
Code example:
Initializes with the specified grid The resizable option:
$( ".selector" ).resizable({ grid: [ 20, 10 ] });
After initialization, get or set grid Option:
// getter var grid = $( ".selector" ).resizable( "option", "grid" );| false| onSelect | event | handles | String or Object | The handles available for resizing.// setter $( ".selector" ).resizable( "option", "grid", [ 20, 10 ] );
Supports multiple types:
- String: A comma-separated list of values, where each value is one of the following: n, e, s, w, ne, se, sw, nw, all. The necessary handles are generated automatically by the plugin.
- Object: The following keys are supported: { n, e, s, w, ne, se, sw, nw }. Any specified value should be a jQuery selector matching a child element of the resizable to be used as a handle.
Note:When generating your own handlers, each handler must have ui-resizable-handle class, or it can also be an appropriate appropriate ui-resizable-{direction} class, such as ui-resizable-s.
Code example:
Initializes with the specified handles Options of resizable:
$( ".selector" ).resizable({ handles: "n, e, s, w" });
After initialization, get or set handles Option:
// getter var handles = $( ".selector" ).resizable( "option", "handles" );| "e, s, se"| onSelect | event | helper | String | A class name added to the proxy element to depict the outline of the resize while the handle is being dragged. Once resizing is complete, the original element is resized.// setter $( ".selector" ).resizable( "option", "handles", "n, e, s, w" );
Code example:
Initializes with the specified helper The resizable option:
$( ".selector" ).resizable({ helper: "resizable-helper" });
After initialization, get or set helper Option:
// getter var helper = $( ".selector" ).resizable( "option", "helper" );| false| onSelect | event | maxHeight | Number | The maximum height to which the resizable is allowed to be resized.// setter $( ".selector" ).resizable( "option", "helper", "resizable-helper" );
Code example:
Initializes with the specified maxHeight Options of resizable:
$( ".selector" ).resizable({ maxHeight: 300 });
After initialization, get or set maxHeight Option:
// getter var maxHeight = $( ".selector" ).resizable( "option", "maxHeight" );| null| onSelect | event | maxWidth | Number | The maximum width to which the resizable is allowed to be resized.// setter $( ".selector" ).resizable( "option", "maxHeight", 300 );
Code example:
Initializes with the specified maxWidth Options of resizable:
$( ".selector" ).resizable({ maxWidth: 300 });
After initialization, get or set maxWidth Option:
// getter var maxWidth = $( ".selector" ).resizable( "option", "maxWidth" );| null| onSelect | event | minHeight | Number | The minimum height to which the resizable is allowed to be resized.// setter $( ".selector" ).resizable( "option", "maxWidth", 300 );
Code example:
Initializes with the specified minHeight Options of resizable:
$( ".selector" ).resizable({ minHeight: 150 });
After initialization, get or set minHeight Option:
// getter var minHeight = $( ".selector" ).resizable( "option", "minHeight" );| 10| onSelect | event | minWidth | Number | The minimum width to which the resizable is allowed to be resized.// setter $( ".selector" ).resizable( "option", "minHeight", 150 );
Code example:
Initializes with the specified minWidth Options of resizable:
$( ".selector" ).resizable({ minWidth: 150 });
After initialization, get or set minWidth Option:
// getter var minWidth = $( ".selector" ).resizable( "option", "minWidth" );| 10 |// setter $( ".selector" ).resizable( "option", "minWidth", 150 );
| Method | Return | Description |
|---|---|---|
| destroy() | jQuery (plugin only) | Completely removes the resizable functionality. This returns the element to its pre-initialization state. |
Code example:
Call the destroy method:
$( ".selector" ).resizable( "destroy" );| onSelect | event | disable() | jQuery (plugin only) | Disabled resizable。
- This method accepts no arguments.
Code example:
Call the disable method:
$( ".selector" ).resizable( "disable" );| onSelect | event | enable() | jQuery (plugin only) | Enables the resizable.
- This method accepts no arguments.
Code example:
Call the enable method:
$( ".selector" ).resizable( "enable" );| onSelect | event | option( optionName ) | Object | Get the value currently associated with the specified `optionName`.
- optionName
Type: String
Description: The name of the option to get.
Code example:
Call this method:
var isDisabled = $( ".selector" ).resizable( "option", "disabled" );| onSelect | event | option() | PlainObject | Gets an object containing key/value pairs that represent the current resizable options hash.
- This method accepts no arguments.
Code example:
Call this method:
var options = $( ".selector" ).resizable( "option" );| onSelect | event | option( optionName, value ) | jQuery (plugin only) | Sets the value of the resizable option associated with the specified `optionName` option.
- optionName
Type: String
Description: The name of the option to set. - value
Type: Object
Description: The value to set for the option.
Code example:
Call this method:
$( ".selector" ).resizable( "option", "disabled", true );| onSelect | event | option( options ) | jQuery (plugin only) | Sets one or more options for the resizable.
- options
Type: Object
Description: The option-value pairs to set.
Code example:
Call this method:
$( ".selector" ).resizable( "option", { disabled: true } );
| onSelect | event | widget() | jQuery | Returns a `jQuery` object containing the resizable element.
- This method accepts no arguments.
Code example:
Call the widget method:
var widget = $( ".selector" ).resizable( "widget" );|
| Events | Type | Description |
|---|---|---|
| create( event, ui ) | resizecreate | Triggered when the resizable is created. |
Type: Event
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the resizable with the specified create callback:
$( ".selector" ).resizable({
create: function( event, ui ) {}
});
Bind an event listener to the resizecreate event:
$( ".selector" ).on( "resizecreate", function( event, ui ) {} );
| onSelect | event | resize( event, ui ) | resize | Triggered when the resize handle is dragged during resizing.
- event
Type: Event - ui
Type: Object- element
Type: jQuery
Description: A jQuery object representing the element to be resized. - helper
Type: jQuery
Description: A jQuery object representing the helper being resized. - originalElement
Type: jQuery
Description: A jQuery object representing the original element before it was wrapped. - originalPosition
Type: Object
Description: the position before resizable adjustment, represented as{ top, left }。 - originalSize
Type: Object
Description: The size of the resizable before resizing, represented as{ width, height }。 - position
Type: Object
Description: The current position, represented as{ top, left }。 - size
Type: Object
Description: The current size, represented as{ width, height }。
- element
Code example:
Initialize the resizable with the specified resize callback:
$( ".selector" ).resizable({
resize: function( event, ui ) {}
});
Bind an event listener to the resize event:
$( ".selector" ).on( "resize", function( event, ui ) {} );
| onSelect | event | start( event, ui ) | resizestart | Triggered when the resizing operation starts.
- event
Type: Event - ui
Type: Object- element
Type: jQuery
Description: A jQuery object representing the element to be resized. - helper
Type: jQuery
Description: A jQuery object representing the helper being resized. - originalElement
Type: jQuery
Description: A jQuery object representing the original element before it was wrapped. - originalPosition
Type: Object
Description: the position before resizable adjustment, represented as{ top, left }。 - originalSize
Type: Object
Description: The size of the resizable before resizing, represented as{ width, height }。 - position
Type: Object
Description: The current position, represented as{ top, left }。 - size
Type: Object
Description: The current position, represented as{ width, height }。
- element
Code example:
Initialize the resizable with the specified start callback:
$( ".selector" ).resizable({
start: function( event, ui ) {}
});
Bind an event listener to the resizestart event:
$( ".selector" ).on( "resizestart", function( event, ui ) {} );
| onSelect | event | stop( event, ui ) | resizestop | Triggered when the resizing operation stops.
- event
Type: Event - ui
Type: Object- element
Type: jQuery
Description: A jQuery object representing the element to be resized. - helper
Type: jQuery
Description: A jQuery object representing the helper being resized. - originalElement
Type: jQuery
Description: A jQuery object representing the original element before it was wrapped. - originalPosition
Type: Object
Description: The position of the resizable before resizing, represented as{ top, left }。 - originalSize
Type: Object
Description: The size of the resizable before resizing, represented as{ width, height }。 - position
Type: Object
Description: The current position, represented as{ top, left }。 - size
Type: Object
Description: The current position, represented as{ width, height }。
- element
Code example:
Initialize the resizable with the specified stop callback:
$( ".selector" ).resizable({
stop: function( event, ui ) {}
});
Bind an event listener to the resizestop event:
$( ".selector" ).on( "resizestop", function( event, ui ) {} );
|
Example
A simple jQuery UI resizable widget.
可调整尺寸小部件(Resizable Widget)演示