Blog

jQuery Bubble Popup

jQuery is my new web obsession. I have fallen in love with it; its simplicity, its power. I can find plugins that do nearly everything I could imagine, and things I would never want to do. One thing that I was trying to do is create a simple popup bubble. When a button is clicked, the bubble will pop up and will disappear when the user clicks away from it. This is much like the Google Maps bubbles.

Below is the code that I eventually got to work.

jQuery

JavaScript:
  1. $(document).ready(function(){
  2.     $("#button").bind("click", function(e) {
  3.         $("#bubble").css({
  4.                 left:$(this).offset().left-5,
  5.                 top:$(this).offset().top-$("#bubble").height()-35
  6.             }).show();
  7.         e.stopPropagation(); // Stops the following click function from being executed
  8.         $(document).one("click", function(f) {
  9.             $("#bubble").hide();
  10.         });
  11.     });
  12. });

As you can see from the previous code, an event is fired when a button ("#button") is pressed and displays a bubble ("#bubble") at the location of the button that was clicked. This can easily be change to the location of the mouse, or a different location completely.

The key to this example is the `e.stopPropagation()` code which will stop the event from continuing onto the next important element: `$(document).one("click", func);`. This will bind a function to the document to be handled once. Its simple really, but unless you know all the right functions and objects, it can be quite confusing.

One last bit of code allows the user to click on the bubble without making it disappear. It uses the same stopPropagation() function as before. This is personal preference, but I like to have it.

Share and Enjoy:
  • Digg
  • Facebook
  • Technorati
  • del.icio.us
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon

3 Responses to “jQuery Bubble Popup”

  1. grigory Says:

    (ICS Calendar “bug” fix)
    Hello!

    Sorry for posting this here, but my email to studio@fullimpact.net was rejected.

    I found a solution to a bug that some people encountered while working on a client’s website and was wondering if you could maybe incorporate it into the next version of ICS Calendar.

    the bug itself and my somewhat trivial solution: http://wordpress.org/support/topic/172180?replies=5#post-765533

    cheers :)
    Grigory.

  2. Janos Says:

    Thanks for this, I’ve implemented a slightly changed version on one of my sites.

  3. Eli Says:

    Hi! and thanks,
    i want to also have a link in the popup that will also close the popup!

    here is the code

    Some text
    some text

    Show popup
    also close from here This is a test. When you click away, it will disappear.

    jQuery(document).ready(function(){
    jQuery(”#button”).bind(”click”, function(e) {
    jQuery(”#bubble”).css({
    left:jQuery(this).offset().left-5,
    top:jQuery(this).offset().top-jQuery(”#bubble”).height()-35
    }).show();
    e.stopPropagation(); // Stops the following click function from being executed
    jQuery(document).one(”click”, function(f) {
    jQuery(”#bubble”).hide();

    });
    });
    // STOP the menu from disappearing when it is clicked
    jQuery(”#bubble”).bind(”click”, function(e) { e.stopPropagation(); });
    });

    what and where do i put the code for id=”cls”
    Thanks! :}

Leave a Reply