Lorem ipsum dolor sit amet, consectetuer adipiscing elit Suspendisse auctor ligula vel odio Nam et sem vitae nibh convallis euismod Aenean vitae urna quis augue adipiscing hendrerit Nam faucibus Phasellus eros Ut bibendum eros at nibh Sed erat Aenean id enim vitae leo aliquet rutrum Mauris fringilla euismod orci Morbi tempus purus eget felis Sed dui eros, tempor id, iaculis vel, pretium eget, nunc Pellentesque vehicula tincidunt arcu Ut a velit In dapibus, lacus vitae lobortis dictum, libero pede venenatis magna, eu sagittis nunc erat sed ante Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas Phasellus est dolor, mollis congue, ullamcorper eu, auctor ut, augue.
//function to execute when doc ready $(function() { //create config object var resizeOpts = { maxWidth: 500, maxHeight: 500, minWidth: 100, minHeight: 100 [ 279 ] Resizing } //make specified element resizable $(".resize").resizable(resizeOpts); }); Save this as resizable5.html This time, the configuration object uses the dimension boundary properties to specify minimum and maximum heights and widths that the resizable may be adjusted to These properties take simple integers as their values, which are then converted to pixels by the component There is one thing you may notice now that our resizable has content in it If you shrink the resizable element so that it is smaller than the content it contains, the content itself is still visible and simply overlaps the boundaries of the resizable We can fix this easily enough by adding the style rule overflow:hidden to our stylesheet This is really the only sensible value to give the overflow style attribute when we use the resizable component Setting overflow to none or automatic does nothing in this example, and setting it to scroll adds the highly unattractive standard OS scrollbars to the resizable element You should note however that Internet Explorer will break the resizable if we use overflow:hidden and there is overflow content We could reduce the amount of content within the resizable or make the resizable bigger by default to overcome this difficulty Try the page out and you should notice the resizable will now keep the dimensional properties that we specified in our configuration object Resize ghosts Ghost elements are very similar to the proxy element we used when we looked at the draggable and droppable components in the previous chapter They can also play a part in the resizables component as well A ghost element can be enabled with the configuration of just one property Let's see how this is done Change resizable5 html to this: [ 280 ] Chapter jQuery UI Resizable Example 6Lorem ipsum dolor sit amet, consectetuer adipiscing elit Suspendisse auctor ligula vel odio Nam et sem vitae nibh convallis euismod Aenean vitae urna quis augue adipiscing hendrerit Nam faucibus Phasellus eros Ut bibendum eros at nibh Sed erat Aenean id enim vitae leo aliquet rutrum Mauris fringilla euismod orci Morbi tempus purus eget felis Sed dui eros, tempor id, iaculis vel, pretium eget, nunc Pellentesque vehicula tincidunt arcu Ut a velit In dapibus, lacus vitae lobortis dictum, libero pede venenatis magna, eu sagittis nunc erat sed ante Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas Phasellus est dolor, mollis congue, ullamcorper eu, auctor ut, augue.
//function to execute when doc ready $(function() { //create config object var resizeOpts = { ghost: "true" } //make specified element resizable $(".resize").resizable(resizeOpts); }); Save this file as resizable6.html All that is needed to enable a resize ghost is to set the ghost property to true No additional underlying HTML or styling is required Everything is handled by the component for us [ 281 ] Resizing The next screenshot shows how the resizable ghost will appear while it is visible: Constraining the resize and maintaining ratio The component makes it easy to ensure that a resized element is constrained to its container element This is great if we have other content on the page that we don't want moving around all over the page during a resize interaction We can also ensure that the specified element is resized symmetrically along both the x and y axis This is known as maintaining its aspect ratio and makes the component very useful when resizing images We can make use of both properties in our next example Change resizable6.html so that it appears as follows: [ 282 ] Chapter jQuery UI Resizable Example 7 //function to execute when doc ready $(function() { //create config object var resizeOpts = { containment:".container", aspectRatio: true } //make specified element resizable $("#resize").resizable(resizeOpts); }); Save this as, you've guessed it, resizable7.html Before we look at the properties used in this example, I should point out that we also use some different CSS for this example In a new file in your text editor, add the following code: container { width:600px; height:600px; border:1px solid #66cc00; padding:10px; } #resize { width:300px; height:300px; } Save this as resizeContainer.css in the styles folder Now, about those configuration properties The containment property allows us to specify a container for the resizable which will limit how large the resizable can be made, forcing it to stay within its boundaries We specify a jQuery selector as the value of this property [ 283 ] Resizing The other property that we've used in this example is aspectRatio, which makes each side of the resizable stay the same size This ensures our resizable element will always be a square as opposed to a rectangle When you run this page in your browser, you should see that the original aspect ratio of the image is preserved, and that the image cannot be made bigger than its container: Resizable animations The resizable API exposes three properties related to animations, which are the animate, animateDuration, and animateEasing properties By default, animations are switched off However, we can easily enable them to see how they enhance the component Create the following new page in your text editor: [ 284 ] Chapter jQuery UI Resizable Example 8 //function to execute when doc ready $(function() { //define config object var resizeOpts = { helper: "proxy", knobHandles: true, animate: true, animateDuration: "fast" }; //make specified element resizable $("#resize").resizable(resizeOpts); }); Save this as resizable8.html This example is based on a as this can be a useful element to make resizable The configuration object we use in this example starts with the helper property When using animations, the resizable element is not resized until after the resize interaction has ended The helper property is useful to show the user the new size of the resizable while the resize is taking place The value we give to the helper property becomes the class name that is applied to the helper element, which we can use to target with some minimal styles In principle, the resizable helper is very similar to the ghost, but it does not show the inner content of the resizable [ 285 ] ... type="text/javascript" src="jqueryui1.6rc2 /jquery- 1.2.6.js"> ... type="text/javascript" src="jqueryui1.6rc2 /jquery- 1.2.6.js"> ... type="text/javascript" src="jqueryui1.6rc2 /jquery- 1.2.6.js">