views:

3682

answers:

5

Alright. I am using a Modal box pop up window to display business details of a dynamic table. This table is very long. Everything works right with the modal box, but if they are say scrolled to the bottom of the page, it always opens the Modal box at the very top of the page. So they would have to do a lot of scrolling back down this way.

I am currently using this code to center my modal box.

function centerPopup(x){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popup" + x).height();
    var popupWidth = $("#popup" + x).width();
    //centering
    $("#popup" + x).css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/4
    });
    //only need force for IE6

    $('#backgroundPopup').css({
        "height": windowHeight
    });

}

I don't know if there is something in this code that is effecting it. A work around would be to have to scroll down to where they were previously but I couldn't find much documentation on .position.

+3  A: 

Why re-invent the wheel when there are a multitude of modal plugins where the authors have already done the hard work. Check out blockui, impromptu, jqmodal plugins. Even if you dont use them you can view the source script and see examples of how to achieve what you want.

redsquare
I wish I could use a simple plugin, but unfortunately I'm pulling modal boxes from a database using ruby on rails. Each link is a business name which opens up a modal box containing the business details. I haven't found anything like that out there. Those plugins are for hard coded HTML like alerts and such Unless I'm completely wrong.
Mike
You can easily use the plugins above with dynamic content.
redsquare
If you look at ThickBox, it can get the content via Ajax and show it in the Modal Popup.http://jquery.com/demo/thickbox/
SolutionYogi
thickbox is showing its age now, there are much better alternatives as above
redsquare
do you know of any good sites or tutorials where I can see someone implementing dynamic content into one of those modal boxes? Maybe even ruby on rails?
Mike
maybe try http://polyrails.com/2008/11/11/steps-to-unobtrusive-rails-with-jquery/. I wouldnt go for livequery plugin but it should get you started
redsquare
+1  A: 

Here you go:

var scrolledX = document.body.scrollLeft || document.documentElement.scrollLeft || self.pageXOffset;
var scrolledY = document.body.scrollTop || document.documentElement.scrollTop || self.pageYOffset;

var screenWidth = document.body.clientWidth || document.documentElement.clientWidth || self.innerWidth;
var screenHeight = document.body.clientHeight || document.documentElement.clientHeight || self.innerHeight;

var left = scrolledX + (screenWidth - $("#popup" + x).width())/2;
var top = scrolledY + (screenHeight - $("#popup" + x).height())/2;

//Use the top and left variables to set position of the DIV.

Though I agree with redsquare that there is no point in reinventing the wheel, use existing plugins.

EDIT: Your final code should look like this:

function centerPopup(x)
{

    var scrolledX = document.body.scrollLeft || document.documentElement.scrollLeft || self.pageXOffset;
    var scrolledY = document.body.scrollTop || document.documentElement.scrollTop || self.pageYOffset;

    var screenWidth = document.body.clientWidth || document.documentElement.clientWidth || self.innerWidth;
    var screenHeight = document.body.clientHeight || document.documentElement.clientHeight || self.innerHeight;

    var left = scrolledX + (screenWidth - $("#popup" + x).width())/2;
    var top = scrolledY + (screenHeight - $("#popup" + x).height())/2;

    //centering
    $("#popup" + x).css({
        "position": "absolute",
        "top": top,
        "left": left
    });
    //only need force for IE6

    $('#backgroundPopup').css({
        "height": screenHeight
    });

}
SolutionYogi
do I just replacevar windowWidth = document.documentElement.clientWidth; var windowHeight = document.documentElement.clientHeight; var popupHeight = $("#popup" + x).height(); with your code? I tried that and the link did nothing at all. can you tell me exactly how to implement your code into mine? var popupWidth = $("#popup" + x).width();
Mike
You will have to copy all 6 lines and use top and left variable to set the TOP and LEFT of the popup div. I have edited my post with final code.
SolutionYogi
Unfortunately the only thing that happens is that the background darkens like it's suppose to but no box pops up when I try entering that code. And it goes to the top of the page when the screen darkens as well.
Mike
Can you try to step through the code and see what kind of values are getting calculated for 'top' and 'left'. Try to do it in non-scrolling scenario first and then with scrolling scenario.
SolutionYogi
I clicked on the first link and my top was 7295.5 and left was 427.5. So what I found out is the my page is just over 14,000 pixels high. Your code is putting the modal box in the exact center of the page but you still get forced to the top of the page. So the screen darkens and you get sent to the top of the page. So we still have the problem where wer get sent to the top of the page, but instead of the box being in the middle of the screen it's now in the middle of the page.
Mike
What do you mean by get forced to the top of the page? Are you calling this from an anchor link? Make sure to prevent the default link behavior otherwise it will scroll the page up.
SolutionYogi
Well, I'm dumb because all I had to add was a return false to my code to stop it from shooting to the top of the page. Now I just need the window to appear in the middle of me current screen instead of the middle of the page.
Mike
Will it be possible for you to put working example on jsbin.com? I will have a look.
SolutionYogi
+1  A: 

Here's nice way to extend the jQuery UI dialog plugin to have a new 'sticky' option...

// This code block extends the ui dialog widget by adding a new boolean option 'sticky' which,
// by default, is set to false. When set to true on a dialog instance, it will keep the dialog's
// position 'anchored' regardless of window scrolling - neato.

// Start of ui dialog widget extension...
(function(){
    if($.ui !== undefined && $.ui.dialog !== undefined)
    {
        var UIDialogInit = $.ui.dialog.prototype._init;
        $.ui.dialog.prototype._init = function()
        {
            var self = this;
            UIDialogInit.apply(this, arguments);

            //save position on drag stop for sticky scrolling
            this.uiDialog.bind('dragstop', function(event, ui)
            {
                if (self.options.sticky === true)
                {
                    self.options.position = [(Math.floor(ui.position.left) - $(window).scrollLeft()),
                                             (Math.floor(ui.position.top) - $(window).scrollTop())];
                }
            });

            //we only want to do this once
            if ($.ui.dialog.dialogWindowScrollHandled === undefined)
            {
                $.ui.dialog.dialogWindowScrollHandled = true;
                $(window).scroll(function(e)
                {
                    $('.ui-dialog-content').each(function()
                    {   //check if it's in use and that it is sticky
                        if ($(this).dialog('isOpen') && $(this).dialog('option', 'sticky'))
                        {
                            $(this).dialog('option', 'position', $(this).dialog('option','position'));
                        }
                    });
                });
            }
        };

        $.ui.dialog.defaults.sticky = false;
    }
})();
// End of ui dialog widget extension...
MyWhirledView
+1  A: 

http://www.west-wind.com/weblog/posts/459873.aspx

This guy built a plugin that centers any elements in the page by adding a .centerInClient. Very slick. Awesome time saver.

Mike
A: 

How can I use the sticky extension?

Martin Nyborg