views:

237

answers:

2

Is there a way that I could trigger an onresize() event for my window in javascript/jQuery?

I tried:

$(window).resize();

but that didn't seem to work exactly as expected. The simplemodal jQuery plugin gives you the option to autoPosition your modal window. Sometimes in IE, the modal shows up a little off center but as soon as I manually make a change to the window size it centers perfectly. So I want to just trigger the onresize event so this will automatically center with the autoPosition of simplemodal set to true.

Ok, I think part of my problem was that I was trying to make the height of the modal dynamic, if the content would change in the modal by showing or hiding elements, so would the size respectively.

Heres my modal call:

$(selector).modal({
    //autoPosition: true,
    //autoResize: true,
    onShow: function(dlg) {
        $(dlg.container).css('height', 'auto') // dynamic height based on content
    },
    onOpen: function(dialog) {
        dialog.overlay.fadeIn('slow', function() {
            dialog.data.hide();
            dialog.container.fadeIn('slow', function() {
                dialog.data.slideDown('slow');
            });
        });
    },
    onClose: function(dialog) {
        dialog.data.fadeOut('slow', function() {
            dialog.container.hide('slow', function() {
                dialog.overlay.slideUp('slow', function() {
                    $.modal.close();
                });
            });
        });
    }
});

If I comment out the onShow attribute, things work again. So I guess maybe that was part of the issue. Is there a way to resize the modal dynamically based on a content, that won't creat this problem ?

Using simplemodal-1.3.4

A: 

as far as i know, you should use

$(window).resize(function() {
  // your code
});

didn't it work for you? what exactly were you expecting?

j.
+1  A: 

Your approach should work, but may not trigger the simplemodal event you want, example:

$(window).resize(function() {
  alert("resized");
});
$(window).resize(); //alerts "resized"

A solution for your particular problem might be:

$(window).trigger('resize.simplemodal');
Nick Craver
I am seeing this in both Firefox 3.6 and IE8 actually. Sometimes the modal will have the bottom half cut off until i resize manually, then it centers perfectly.
gmcalab
@Nick Craver, check out my updated question there, I think I found part of the issue.... maybe you can shed some more light on the update portion.
gmcalab
@Nick Craver, yes I tried trigger, didn't have any effect.
gmcalab
@Nick Craver, The negative would be that if I have lets say a drop box that toggles showing and hiding elements. The height would grow/shrink. So I would want the modal to follow that. If I comment it out, and the height needs to grow, the elements overflow out of the modal and same vice versa, the theres lots of left over space if we remove elements. All that does is make the modal's height dynamic based on toggling elements visibility that live inside the modals real estate.
gmcalab
@gmcalab - Are you using many of these, or just 1 on the page? If you store a reference to in onShow like this: `var mod = dlg`, you should be able to call `mod.setPosition();` later, I don't see any way to get at the dialog object that Eric exposes normally.
Nick Craver
Yea that doesnt seem to work. I can use the `onShow: function(dlg) { $(dlg.container).css('height', 'auto') // dynamic height based on content }` in version 1.3 but when I use 1.3.4 it creates this issue...
gmcalab