Modal Popup – Twitter Bootstrap

Just a quick post on how to create a modal popup using twitter bootstrap:

Step #1:

Add the following to a page element such as a link or button in order to open the modal:

     href="modal_content_name"
     data-toggle="modal"

i.e.

Add Image

Step #2:
Create a content div i.e.


And that’s all there is to it, just make sure you’ve included the bootstrap-modal.js file.

Modal Popup - Twitter Bootstrap
Modal Popup – Twitter Bootstrap

Open Twitter Bootstrap Modal Popup on Page Load:
In order to open the modal dialog on page load simply add the following JavaScript:


    $(document).ready(function(){
        $('#test_modal').modal('show');
    });

You can also close the modal popup manually by using the following JavaScript:


$('#test_modal').modal('hide')


Adjust Modal Width:
In order to adjust the modal width you have two options, Javascript or CSS. You can use the following JavaScript mentioned on GitHub:


   $('#test_modal').modal({
        backdrop: true,
        keyboard: true,
        show: false \remove this if you don't want it to show straight away
    }).css({
        width: 'auto',
        'margin-left': function () {
            return -($(this).width() / 2);
        }
    });

Or this CSS from StackOverflow:

body .modal-admin {
    //new custom width
    width: 750px;
    //must be half of the width, minus scrollbar on the left (30px)
    margin: -345px 0 0 -375px;
}

Show modal when the user scrolls past certain div
I’ve received another request on how to show the modal when a user scrolls to a certain div. The following should work, but please let me know if you have any issues.

/* Wait for document to load */
$(document).ready(function(){

$(window).scroll(function(){

//Retrieve scroll distance from top
var top = $(window).scrollTop();

//Retrieve the divs distance from top of page
var div_distance = $('my_div').offset().top;

//Use this if you want a bit of a variable distance, for instance if they scroll 150 pixels past the div
var buffer_distance = 150;

//Check if scrolled past div
if(top >= div_distance && top <= (div_distance + buffer_distance)){

//Show modal - you may want to add checks etc to see if it's already show
$('#test_modal').modal('show');

} else {

//Hide the modal, have scrolled past - you may want to add some checks to see if it's already hidden etc
$('#test_modal').modal('hide');                         
}        
}); 
});

Comments

15 responses to “Modal Popup – Twitter Bootstrap”

  1. Tom Harrison Avatar

    This is helpful in the case where the model is more like a popup (the modal HTML is there, just hidden) — I am trying to make my user edit pages open in a modal. So there are existing Rails routes (/users/edit/1) controllers, and views, then from one page I want to click a link to open the new page, or alternatively call the controller method as AJAX, have it insert the modal HTML and then display it. Any ideas?

    Like

    1. Errors Avatar
      Errors

      Hey Tom, this isn’t too difficult – the easiest way is to simply bind an onclick event to the button you want to use to open the modal and then use this to fetch the partial via ajax. You can then render it using escape_javascript etc.

      I tend to place the modal on the page before hand and then fetch just the form itself via ajax. Just place a loader gif in the modal and then replace it with the partial once it has been fetched. This adds a little extra overhead, however it provides a better ux (in my opinion)

      Like

      1. maddy Avatar
        maddy

        but how to possibale

        Like

    2.  Avatar
      Anonymous

      Hi Tom Harrison, have you got solution for edit user page. Please let me know if you have.

      Like

  2. sonu sindhu Avatar

    very nice and useful

    Like

  3. Bencici Avatar

    Very helpful but I have an error in the code and I can’t fix it http://validator.w3.org/check?uri=http%3a%2f%2fbencici%2eme%2f an idea ?

    Like

  4. cursos Avatar

    Any idea on how to show a dialog when page reaches certain div?
    thanks!

    Like

    1. Chris Owens Avatar
      Chris Owens

      Hey Cursos,

      Sorry about the late reply, have been busy! I’ve updated the post to include a quick guide on how to do it, please let me know if you have any trouble.

      Like

  5. ddnhf Avatar

    When I use this, the box just auto-closes. What am I doing wrong?

    Like

    1. Chris Owens Avatar
      Chris Owens

      Hey Ddnhf,

      Have you got a sample page/code I can take a look at?

      Like

      1. ddnhf Avatar

        http://illuminatichd.com/social/ When you click “Have an account? Login” or “Sign Up” the box pops up for a second. The code is in the source.

        Like

      2. Chris Owens Avatar
        Chris Owens

        Hey Ddnhf,

        Seem to be a few issues with styles there:

        Using Chrome:
        – Hitting the login button the overlay z-index is too high. Not sure if you’ve manually set the z-index somewhere but it is greater than the modal itself. Adjust the “.modal-backdrop” class to have a lower z-index to fix this bit.
        – The sign up button seems to work fine

        And just any FYI, no text is appearing in the modals in IE9. If I’m not looking at the right issues please let me know.

        Cheers,
        Chris

        Like

  6. zuhry Avatar
    zuhry

    i create one modal with remote content, in remote content page have pagination control, when i click the pagination number, my browser directly redirect to the oringinal url content. so could you help me for fix it?

    Like

    1. Chris Owens Avatar
      Chris Owens

      Hey Zuhry,

      Happy to help out where I can, but you’ll need to give me a fair bit more info. Do you have a link or jsfiddle to it running?

      Cheers,
      Chris

      Like

  7. Andrew S Avatar

    This is awesome, I’ve been looking for something to use besides a javascript confirm windom.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: