bootbox icon indicating copy to clipboard operation
bootbox copied to clipboard

center bootbox vertically

Open PepeVasquez opened this issue 13 years ago • 16 comments

I use the below script to automatically center any bootstrap modal I use in my web site, and it works fine, but it does not work with bootbox even though bootbox uses the 'modal' class. Is bootbox somehow overwriting the show event? If so, is there a work-around to vertically center bootbox? Thanks.

$(".modal").on("show", function () { $(this).css({ 'top': '50%', 'margin-top': function () { return -($(this).height() / 2); } }); });

PepeVasquez avatar Oct 16 '13 21:10 PepeVasquez

Hey,

No, the show event isn't being overridden, but I suspect it's due to the way you're listening for the event directly on .modal - you need to use event delegation instead. This is a jQuery / JavaScript issue rather than anything to do with Bootbox. Check out the docs for $.on and search for the section starting 'event handlers are bound only...' and you'll find a far better explanation than I could give you :).

makeusabrew avatar Oct 17 '13 07:10 makeusabrew

Thank you very much for pointing me in the right direction. I changed the way I was listening to the 'show' event and now bootbox is centered vertically in the screen. This is how it looks now.

$("body").on("show", ".modal", function () { $(this).css({ 'top': '50%', 'margin-top': function () { return -($(this).height() / 2); } }); });

PepeVasquez avatar Oct 17 '13 15:10 PepeVasquez

No problem, glad that sorted it.

makeusabrew avatar Oct 17 '13 15:10 makeusabrew

Didn't have luck using the above method with 4.3.0. Any tip?

cvsguimaraes avatar Aug 25 '14 21:08 cvsguimaraes

@cvsguimaraes check those events from bootstrap documentation: http://getbootstrap.com/javascript/#modals

$("body").on("shown.bs.modal", ".modal", function() {
    $(this).css({
        'top': '50%',
        'margin-top': function () {
            return -($(this).height() / 2);
        }
    });
});

tarlepp avatar Aug 28 '14 16:08 tarlepp

This didn't work for me. For some reason it shifts the whole page down:

broke

I then tried a different approach:

$("body").on("shown.bs.modal", ".modal", function() {
    $(this).find('div.modal-dialog').css({
        'top': '50%',
        'margin-top': function () {
            return -($(this).height() / 2);
        }
    });
});

...which kinda works - but not in Chrome for some reason:

broke2

Any suggestions?

youradds avatar Jan 30 '15 08:01 youradds

Use the method suggested by @tarlepp but pay attention to the parent (probably the document body) positioning. Check if you used some absolute or fixed positioning.

cvsguimaraes avatar Jan 30 '15 14:01 cvsguimaraes

Mmm nope, didn't have that. Its weird, as the modal-wrapper looks like it has scrollbars (but starting part way down the page)

Anyway, I managed to get it going with this "hacky" solution :)

$("body").on("shown.bs.modal", ".modal", function() {
    $(this).find('div.modal-dialog').css({
        'margin-top': function () {
            var modal_height = $('.modal-dialog').first().height();
            var window_height = $(window).height();
            return ((window_height/2) - (modal_height/2));
        }
    });
});

youradds avatar Jan 31 '15 08:01 youradds

Hello, how could I get this on angular??

diegomesata avatar Feb 06 '15 21:02 diegomesata

@diegomesata use directive for that.

tarlepp avatar Feb 06 '15 21:02 tarlepp

Just had this issue after I made a top fixed header. The fix I did was to add a css class in the bootbox config with a high z-index as in:

bootbox.dialog({ className: "dialogFixedtop"......

and the class is

.dialogFixedtop {
z-index: 9999 !important; }

Also tried to move the top margin, but the shadow became an issue.

rasmuschristensen avatar Apr 10 '15 12:04 rasmuschristensen

Hi, A little late to the party but the way I do it is var box = bootbox.dialog({ ,,, });

box.css({
  'top': '50%',
  'margin-top': function () {
    return -(box.height() / 2);
  }
});

Hope this helps :)

IanIT avatar May 21 '15 09:05 IanIT

thank you makeusabrew its worked for me.

hamdiceylan avatar Sep 12 '15 07:09 hamdiceylan

so what's the final solution?

ghost avatar Dec 23 '15 19:12 ghost

IanIT's comment worked for me.

alexmahterian avatar Mar 07 '16 13:03 alexmahterian

Huy guys this would fix all guys :)

var box = bootbox.dialog({ .... }); dialog = box.find('.modal-dialog'); box.css('display', 'block');
dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2));

jbmatias avatar Jul 17 '17 17:07 jbmatias