MooResize adds three handles to any image or other DOM Element. With these handles, you can resize your element on a easy way. It has three events, onStart, onResize and onComplete so it is very flexible too. Also it is very easy to set a ratio by the setRatio method.
First you have to include the javascript file in the head of your document
#HTML
<script src="../Source/MooResize.js" type="text/javascript"></script>
In the body
#HTML
<img id="resizeMe" src="image.jpg" alt="Look, you can resize me" />
Then add the following javascript
#JS
new resize = new MooResize('resizeMe');
And you're done!
#JS
var resize = new MooResize('img1',{
handleSize: 15,
handleStyle: {
background: 'blue'
},
onStart: function(){
document.id(this).setStyle('opacity',0.5);
},
onComplete: function(size){
document.id(this).setStyle('opacity',1);
alert(size.x+' '+size.y);
},
minSize: {
x: 100,
y: 50
}
});
#JS
new MooResize(element[,options]);
- element - (string,element) The element to be resized
- options - (object,optional) The MooResize Options. See below
- handleSize - (number: defaults to 10) The size of the handles
- minSize - (object) The minimum size. You can set an object for the minimum size e.g. {x: 100, y: 75}
- maxSize - (object) The maximum size. You can set an object for the maximum size e.g. {x: 400, y: 300}
- ratio - (boolean,float: defaults to false) Should it keep a ratio: true or false. If you set a float, e.g. 1.5 you can set the width/height ratio
- dragOptions: (object) The options of MooTools More's Drag
- handleStyle: (object) Style the handles
- start - (function) Fires when you start resizing
- resize - (function) When you are resizing
- complete - (function) When you're ready with resizing
Set the size of the element (including the new position of the handles)
#JS
resize.setSize(width,height);
// Or
resize.setSize({x: 500, y: 400});
- width - (number,object) - The new width. Or an object like {x: 300, y: 200}
- height - (number) - The new height. This is optional if the first argument is an object
- (MooResize) - MooResize instance
Get the size of the element
#JS
resize.getSize(); // {x: 256, y: 643}
- (object) - An object with x and y
Set a ratio
#JS
resize.ratio(ratio);
- ratio - (boolean,float) - Just true or false, or a width/height number
- (MooResize) - MooResize instance
Get the current ratio as width/height
#JS
resize.getRatio(); // 1.25
- (float) - The ratio
Remove the handles
#JS
resize.dispose();
Get your element back
#JS
document.id(resize);