English 中文(简体)
JqueryUI - Resizable
  • 时间:2024-09-17

JqueryUI - Resizable


Previous Page Next Page  

jQueryUI provides resizable() method to resize any DOM element. This method simppfies the resizing of element which otherwise takes time and lot of coding in HTML. The resizable () method displays an icon in the bottom right of the item to resize.

Syntax

The resizable() method can be used in two forms −

$ (selector, context).resizable (options) Method

The resizable (options) method declares that an HTML element can be resized. The options parameter is an object that specifies the behavior of the elements involved when resizing.

Syntax

$(selector, context).resizable (options);

You can provide one or more options at a time of using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −

$(selector, context).resizable({option1: value1, option2: value2..... });

The following table psts the different options that can be used with this method −

Sr.No. Option & Description
1

This option is of type Selector, jQuery , or any DOM Element. It represents elements that also resize when resizing the original object. By default its value is false.

Option - alsoResize

This option can be of type Selector, jQuery , or any DOM Element. It represents elements that also resize when resizing the original object. By default its value is false.

This can be of type −

    Selector − This type indicates DOM elements to be selected from a DOM document for resizing.

    jQuery − A callback function will be called for each resizable element on page. This function should return true if the element is resized.

    Element − An element in the Document Object Model (DOM).

Syntax

$( ".selector" ).resizable({ alsoResize: "#identifier" });
2

This option when set to true is used to enable a visual effect during resizing when the mouse button is released. The default value is false (no effect).

Option - animate

This option when set to true is used to enable a visual effect during resizing when the mouse button is released. The default value is false (no effect).

Syntax

$( ".selector" ).resizable({ animate: true });
3

This option is used to set the duration (in milpseconds) of the resizing effect. This option is used only when animate option is true. By default its value is "slow".

Option - animateDuration

This option is used to set the duration (in milpseconds) of the resizing effect. This option is used only when animate option is true. By default its value is "slow".

This can be of type −

    Number − This specifies duration in milpseconds

    String − This sepcifies named duration, such as "slow" or "fast".

Syntax

$( ".selector" ).resizable({ animateDuration: "fast" });
4

This option is used to specify which easing to apply when using the animate option. By default its value is "swing".

Option - animateEasing

This option is used to specify which easing to apply when using the animate option. By default its value is "swing".

Easing functions specify the speed at which an animation progresses at different points within the animation.

Syntax

$( ".selector" ).resizable({ animateEasing: "easeOutBounce" });
5

This option is used to indicate whether to keep the aspect (height and width) ratio for the item. By default its value is false.

Option - aspectRatio

This option is used to indicate whether to keep the aspect (height and width) ratio for the item. By default its value is false.

This can be of type −

    Boolean − This value if set to true, the element will maintain its original aspect ratio.

    Number − This specifies the element to maintain a specific aspect ratio during resizing.

Syntax

$( ".selector" ).resizable({ aspectRatio: true });
6

This option is used to hide the magnification icon or handles, except when the mouse is over the item. By default its value is false.

Option - autoHide

This option is used to hide the magnification icon or handles, except when the mouse is over the item. By default its value is false.

Syntax

$( ".selector" ).resizable({ autoHide: true });
7

This option is used to prevent resizing on specified elements. By default its value is input,textarea,button,select,option.

Option - cancel

This option is used to prevent resizing on specified elements. By default its value is input,textarea,button,select,option.

Syntax

$( ".selector" ).resizable({ cancel: ".cancel" });
8

This option is used restrict the resizing of elements within a specified element or region. By default its value is false.

Option - containment

This option is used restrict the resizing of elements within a specified element or region. By default its value is false.

This can be of type −

    Selector − This type indicates that resizable element will be contained to only the first item in the pst found by the selector.

    Element − This type indicates any DOM element. The resizable element will be contained to the bounding box of this element.

    String − Possible values for this type are - parent and document.

Syntax

$( ".selector" ).resizable({ containment: "parent" });
9

This option is used to set tolerance or delay in milpseconds. Resizing or displacement will begin thereafter. By default its value is 0.

Option - delay

This option is used to set tolerance or delay in milpseconds. Resizing or displacement will begin thereafter. By default its value is 0.

Syntax

$( ".selector" ).resizable({ delay: 150 });
10

This option disables the resizing mechanism when set to true. The mouse no longer resizes elements, until the mechanism is enabled, using the resizable ("enable"). By default its value is false.

Option - disabled

This option disables the resizing mechanism when set to true. The mouse no longer resizes elements, until the mechanism is enabled, using the resizable ("enable"). By default its value is false.

Syntax

$( ".selector" ).resizable({ disabled: true });
11

With this option, the resizing starts only when the mouse moves a distance(pixels). By default its value is 1 pixel. This can help prevent unintended resizing when cpcking on an element.

Option - distance

With this option, the resizing starts only when the mouse moves a distance(pixels). By default its value is 1 pixel. This can help prevent unintended resizing when cpcking on an element.

Syntax

$( ".selector" ).resizable({ distance: 30 });
12

This option when set to true, a semi-transparent helper element is shown for resizing. This ghost item will be deleted when the mouse is released. By default its value is false.

Option - ghost

This option when set to true, a semi-transparent helper element is shown for resizing. This ghost item will be deleted when the mouse is released. By default its value is false.

Syntax

$( ".selector" ).resizable({ ghost: true });
13

This option is of type Array [x, y], indicating the number of pixels that the element expands horizontally and vertically during movement of the mouse. By default its value is false.

Option - grid

This option is of type Array [x, y], indicating the number of pixels that the element expands horizontally and vertically during movement of the mouse. By default its value is false.

Syntax

$( ".selector" ).resizable({ grid: [ 20, 10 ] });
14

This option is a character string indicating which sides or angles of the element can be resized. By default its values are e, s, se.

Option - handles

This option is a character string indicating which sides or angles of the element can be resized. The possible values are: n, e, s, and w for the four sides, and ne, se, nw, and sw for the four corners. The letters n, e, s, and w represent the four cardinal points (North, South, East, and West).

By default its values are e, s, se.

Syntax

$( ".selector" ).resizable({ handles: "n, e, s, w" });
15

This option is used to add a CSS class to style the element to be resized. When the element is resized a new <span> element is created, which is the one that is scaled (ui-resizable-helper class). Once the resize is complete, the original element is sized and the <span> element disappears. By default its value is false.

Option - helper

This option is used to add a CSS class to style the element to be resized. When the element is resized a new <span> element is created, which is the one that is scaled (ui-resizable-helper class). Once the resize is complete, the original element is sized and the <span> element disappears. By default its value is false.

Syntax

$( ".selector" ).resizable({ helper: "resizable-helper" });
16

This option is used to set the maximum height the resizable should be allowed to resize to. By default its value is null.

Option - maxHeight

This option is used to set the maximum height the resizable should be allowed to resize to. By default its value is null.

Syntax

$( ".selector" ).resizable({ maxHeight: 300 });
17

This option is used to set the maximum width the resizable should be allowed to resize to. By default its value is null.

Option - maxWidth

This option is used to set the maximum width the resizable should be allowed to resize to. By default its value is null.

Syntax

$( ".selector" ).resizable({ maxWidth: 300 });
18

This option is used to set the minimum height the resizable should be allowed to resize to. By default its value is 10.

Option - minHeight

This option is used to set the minimum height the resizable should be allowed to resize to. By default its value is 10.

Syntax

$( ".selector" ).resizable({ minHeight: 150 });
19

This option is used to set the minimum width the resizable should be allowed to resize to. By default its value is 10.

Option - minWidth

This option is used to set the minimum width the resizable should be allowed to resize to. By default its value is 10.

This can be of type −

Syntax

$( ".selector" ).resizable({ minWidth: 150 });

The following section will show you few a working examples of resize functionapty.

Default Functionapty

The following example demonstrates a simple example of resizable functionapty, passing no parameters to the resizable() method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Resizable functionapty</title>
      <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header {
            background:#b9cd6d;
            border: 1px sopd #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
         .ui-widget-content {
            background: #cedc98;
            border: 1px sopd #DDDDDD;
            color: #333333;
         }
         #resizable { width: 150px; height: 150px; padding: 0.5em;
            text-apgn: center; margin: 0; }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#resizable" ).resizable();
         });
      </script>
   </head>

   <body>
      <!-- HTML --> 
      <span id = "resizable" class = "ui-widget-content"> 
         <h3 class = "ui-widget-header">Pull my edges to resize me!!</h3>
      </span>
  </body>
</html>

Let us save the above code in an HTML file resizeexample.htm and open it in a standard browser which supports javascript, you should see the following output. Now, you can play with the result −