6

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.

Mike
  • 1,190
  • 5
  • 13
  • 26

7 Answers7

12

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
  • 1,190
  • 5
  • 13
  • 26
7

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
  • 78,161
  • 20
  • 151
  • 159
  • 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 Jul 01 '09 at 18:22
  • You can easily use the plugins above with dynamic content. – redsquare Jul 01 '09 at 18:42
  • 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 Jul 01 '09 at 19:37
  • thickbox is showing its age now, there are much better alternatives as above – redsquare Jul 01 '09 at 19:48
  • 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 Jul 01 '09 at 20:11
  • 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 Jul 01 '09 at 20:42
3

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
  • 31,807
  • 12
  • 70
  • 78
  • do I just replace var 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 Jul 01 '09 at 18:24
  • 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 Jul 01 '09 at 19:22
  • 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 Jul 01 '09 at 20:09
  • 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 Jul 01 '09 at 20:42
  • 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 Jul 01 '09 at 21:26
  • 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 Jul 01 '09 at 21:43
  • 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 Jul 02 '09 at 14:43
  • Will it be possible for you to put working example on jsbin.com? I will have a look. – SolutionYogi Jul 02 '09 at 15:40
2

A simple solution for me was to set the CSS for the containing element of the modal window to:

position:fixed;

This worked in IE7+, for IE6 you may need the advanced methods above.

Kevinleary.net
  • 8,851
  • 3
  • 54
  • 46
1

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... 
SavoryBytes
  • 35,571
  • 4
  • 52
  • 61
0

here is very good answer for setting position of ModelPopup. May this help you all.

  1. Get the position you want the modal window to show. You can do it using ASP.NET Ajax library using Sys.UI.DomElement.getLocation method like this:

    var location = Sys.UI.DomElement.getLocation($get("div1"));

In this case I used some div control to set the modal windows next to it.

  1. Get the Modal window reference by using it’s panel or div element.

  2. Set the location of the modal window:

    Sys.UI.DomElement.setLocation($get(‘panel1’), location.x,location.y);

Vikash Singh
  • 804
  • 1
  • 10
  • 11
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Oct 16 '13 at 10:16
-2

I don't know why everyone says to use a plugin.. why reinvent the wheel. Why bother learning.. just get someone else to do it. I've been using jquery since 1.3.x and many of the plugins are bloated, not all, but many. I have written the same solution to many plugins I've studied in 1/4 the lines of code, plus they do what I want them to.

and before I forget... a simple solution the the problem of the scroll to top is to add... return false; once your modal or positioning code has run.

Wayne
  • 7