views:

38

answers:

1

I need to display an image in a resizable dialog (so far all the specific image popup libraries I have tried do not fit my needs).

All I want to do is maintain the aspect ratio during resizing. Sounds easy, but it's not.

I thought something like this might work, but no dice:

var d = $("").dialog({title:title, width:400, height:400});
d.resizable( "option", "aspectRatio", true );

Any pointers greatly appreciated, tks

+1  A: 

Due to the way it's hooked in, I find it easiest to just do the resizable portion yourself, like this:

$("div").dialog({
    title:"Title", 
    width:400, 
    height:400, 
    resizable: false
}).parent().resizable({ 
    handles: 'n,e,s,w,se,sw,ne,nw', 
    aspectRatio: true 
});​​​

You can view a demo here, you can also destroy and re-create it...but that's a bit wasteful, so just create it above, specify the max/min height/width in the resizable instead of the dialog if needed. This should work: .parent().resizable("option", "aspectRatio", true), but it doesn't due to the way the widgets hooked in, so the eaiest solution is to just create the resizable yourself with the options you want when you create the dialog.

Side note: you're using .parent() here because you wan the dialog container that contains the title bar and your content. It's created/wrapped like this when you create the dialog.

Nick Craver
Excellent. Thanks! You've just saved me a great deal of time...
David Semeria
@David - Welcome! If it solved your issue be be sure to accept an answer as correct (same goes for future questions!), it helps everybody out :) If it didn't, please comment so I can help a bit further :)
Nick Craver
@Nick, sorry forgot to mark your answer. I still had to do quite a lot of work to get the contents (rather than just dialog box) to retain a constant AR. Got there by using the 'resize' event from resizable, which allows me to dynamically size the content div. But I would never have got that far without your idea. Thanks again!
David Semeria