Hvem glor – din bror Den ikke-så-personlige blog af Bjørn Klinggaard

14jul/10184

bPopup

21.04.12: New release, bPopup version 0.7.0

bPopup is a learning and exploring jQuery project. It's a lightweight cross browser jQuery popup plugin. It's not creating your popup but doing all the logic as opening, closing, centering on resize & scroll, creating a modal overlay etc. It can open any container you create with all kinds of content. bPopup has been tested in IE67-9, FF2-7, Opera 9-10, Safari 4-5 and Chrome 4-15.

DEMODownload

Contents

  1. How to use bPopup
  2. Options (API)
  3. Changelist
  4. What can you do?
  5. Downloads

How to use bpopup

Basic example that will bind a click event which will trigger bPopup when fired. Add a reference to the jquery core lib (newer than 1.3.x) and bPopup. Please don't hotlink to bPopup:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="jquery.bpopup-x.x.x.min.js"></script>

Markup:

    <html>
        <head> ... </head>
        <body>
          ...
            <!-- Button that triggers the popup -->
            <button>POP IT UP</button>
            <!-- Element to pop up -->
            <div>Content of popup</div>
          ...
        </body>
    </html>

CSS:

    element_to_pop_up { display:none; }

The magic:

    // Semicolon (;) to ensure closing of earlier scripting
    // Encapsulation
    // $ is assigned to jQuery
    ;(function($) {

         // DOM Ready
        $(function() {

            // Binding a click event
            // From jQuery v.1.7.0 use .on() instead of .bind()
            $('button').bind('click', function(e) {

                // Prevents the default action to be triggered. 
                e.preventDefault();

                // Triggering bPopup when click event is fired
                $('element_to_pop_up').bPopup();

            });

        });

    })(jQuery);

Or with custom settings

    ;(function($) {
        $(function() {
            $('button').bind('click', function(e) {
                e.preventDefault();
                $('element_to_pop_up').bPopup({
                    appendTo: 'form'
                    , zIndex: 2
                    , modalClose: false
                });
            });
         });
     })(jQuery);

Enjoy

Options

API – name [type] (default value)
amsl [int] (50) Above Mean Sea Level. Vertical distance from the middle of the window, + = above, - = under.
appendTo [string] (’body’) Element to append popup (and modal overlay) to. For asp.net sites append to 'form'.
appending [bool] (true) Should the popup append to an element or just open where it is?  Version 0.7.0
closeClass [string] ('bClose') Class to bind the close event to. Version 0.3.3
content [string] ('ajax') Content of bpopup. Types: ['ajax', 'iframe', 'xlink']. If loadUrl isn't defined it'll not use content type. Version 0.4.1
contentContainer [string] (null) Element which content should be added to. If undefined/null it will add it to $(this). Usage contentContainer:'.element' or contentContainer:'#element'.  Version 0.4.1
escClose [bool] (true) Should popup close when press on escape?
fadeSpeed [int/string] (250) Animation speed on fadeIn/out. Version 0.3.6
follow [bool,bool] ([true,true]) Should the popup follow the screen vertical and/or horizontal on scroll/resize? [horizontal, vertical, fixed on screen (see positionStyle instead)] Version 0.3.6. Changed in version 0.5.0 and again in version 0.6.0 and again in version 0.7.0.
followSpeed [int/string] (500) Animation speed for the popup on scroll/resize. Version 0.3.6
loadUrl [string] (null) External page or selection to load in popup.  Version 0.3.4
modal [boolean] (true) Should there be a modal overlay behind the popup?
modalClose [bool] (true) Should the popup close on click on overlay? Version 0.4.0
modalColor [string] ('#000') What color should the overlay be? Version 0.3.5
opacity [float] (0.7) Transparency, from 0.1 to 1.0 (filled). Version 0.3.3
position [int,int] (['auto','auto']) Defines the position where the popup should pop up. 'auto' = center, [horizontal, vertical]  Version 0.5.0. Changed in version 0.7.0, swapped positions so it's now x, y instead of y, x
positionStyle [string] ('absolute') How you the popup follow? Position 'absolute' or 'fixed'  Version 0.7.0
scrollBar [bool] (true) Should scrollbar be visible?
zIndex [int] (9999) Popup z-index, modal overlay = popup z-index - 1.
Events
callback Event fires after the popup has loaded.

Usage: $('element_to_pop_up').bPopup({ //options }, function(){ //callback }); Version 0.3.5

onOpen Event fires before the popup opens.

Usage: $('element_to_pop_up').bPopup({ onOpen: function(){ //doMagic }});  Version 0.5.0

onClose Event fires after the popup closes.

Usage: $('element_to_pop_up').bPopup({ onClose: function(){ //doMagic }}); Version 0.5.0

Public Function
close() Closes the popup.

Usage: $('element_to_pop_up').bPopup().close(); Version 0.4.0

Recycle Bin - not there anymore
duration[int/string] ('normal') Animation speed.Removed in version 0.3.6 - See fadeSpeed and followSpeed.
minTop [int] (20) Minimum distance from top to popup. Removed in version 0.3.6
vStart [int] (null) Vertical start position for popup. Version 0.3.6 Removed in version 0.5.0 - See Position.
xLink [boolean] (false) Is the popup a xlink popup? Version 0.3.4, Removed in version 0.4.1 use content type instead (content:'xlink')

Changelist

0.7.0 change-list (21.04.12):

  • Bugfix: Multiple popup count fix (Thanks to Michael for reporting)
  • Bugfix: Callback now returns 'this' instead of 'window' (Thanks to Jeppe for reporting)
  • Bugfix: Scroll before popup is loaded opacity fix
  • Bugfix: Moved initialization of popup to after onOpen event so it will center properly when adding stuff onOpen.
  • Changed: Important note - Swapped arguments in Position so it now takes [x, y] instead of [y, x] like everything else in the world.
  • Changed: Important note - Also swapped arguments in Follow so it also takes [x, y] instead of [y, x] and removed third argument. See positionStyle instead.
  • New option: appending: true. Defines if the popup should append to an element or just open where it is.
  • New option: positionStyle: 'absolute'. Defines the position style which has impact on how the popup follows on scroll and resize. Absolute or fixed.
  • Removed private function, getDistanceToBodyFromLeft().

0.6.0 change-list (29.10.11):

  • New feature: Supports multiple bPopups. Now you can open all the bPopups you want on top of each other.
  • New feature: Won't follow if popup is bigger then the window.
  • Changed: Added 3rd parameter to follow, default false. If true the popup will be fixed on the screen.
  • Removed: Browser detecting is bad why I removed the check for IE6. IE6 doesn't support position: fixed but I don't support IE6 any more. I'm afraid I killed it. It's easy to add if you still need to support IE6.

0.5.1 change-list (13.04.11):

  • Bugfix: Added a duration time to the hide() call so onOpen and loadUrl always will be trigged when needed.
  • Bugfix: Added frameborder="0" and scrolling="no" to iframe to remove borders in IE. Removed width="100%" and height=100%" from iframe so you have full control over the iframe
  • Code update: updated the logic regarding follow and position

0.5.0 change-list (12.03.11):

  • bPopup, now with more control and features!
  • New events: onOpen and onClose
  • New option: Position['auto','auto']. Replacing vStart. Gives you full control where the popup should be.
  • Changed option: follow changed to array of booleans, follow[true,true]. Defines if the popup should follow vertical and/or horizontal.
  • Changed option: default value for amsl changed from 150px to 50px.
  • Removed option: vStart, see Position.
  • Removed feature: the popup doesn't auto reset input fields or auto focus any more. If you need this feature do the magic inside the onOpen event.
  • Removed feature: xlink removed from content type. Instead do the xlink stuff inside the onOpen event.
  • Bugfix: Modal overlay fits whole screen on an iphone in safari.
  • Known issue: The popup doesn't center properly when zooming on an iphone in safari.

0.4.1 change-list (17.10.10):

  • New option: content, now it's possible to load content into the popup through Ajax or in an iframe. It takes the url from loadUrl.
  • New option: contentContainer, gives you the possibility to load content into a small area of the popup e.g. a div at the bottom.
  • Removed option: xLink, use content type instead (content:'xlink')!

0.4.0 change-list (22.09.10):

  • Attention: In this update there has been changes that affect how you interact with the popup!
  • New option: modalClose (should the popup close on click on overlay?)
  • New way to create the popup.You should now call $(selector).bPopup() instead of $(selector).openPopup().
  • New way to force popup to close. Force close by calling the public function close(), $(selector).bPopup().close(), instead of triggering the close event with $('.bClose').closePopup(). This will stop cluttering up the $.fn namespace.
  • Changed default value for opacity from 0.5 to 0.7
  • Ensures that the same popup can't be opened twice at a time.
  • Next update will include support for iframes, generic xlink support and more.

0.3.6 change-list (14.07.10):

  • New options: fadeSpeed, follow, followSpeed, vStart
  • Deleted: duration, minTop
  • Included position:absolute styling on popup in plugin

0.3.5 change-list (05.07.11):

  • New option: modalColor
  • bPopup can now take callback functions

0.3.4 change-list:

  • New option: loadUrl
  • The popup can now open an url inside the popup
  • Implemented x-link feature
  • Refactored code, created private help functions
  • Changed default value for scrollBar to true

0.3.3 change-list:

  • New options: opacity, closeClass
  • Changed modal overlay to position fixed in supported browsers. Non position fixed supported browsers still uses position absolute.
  • Added default close event
  • Fixed issue with overlay in IE6+7 and body positioned relative with a width

0.3.2 change-list:

  • Modal overlay width miscalculation on resize.
  • Auto textfield focus
  • Resetting textfields before showing popup

0.3.1 change-list:

  • New options: escClose, zIndex and minTop.
  • Fixed bug with modal overlay when body has position: relative and a width. Now calculates the distance from the left to the body.

0.3.0 change-list:

  • Unbind events on close.
  • Centering popup on resize.
  • Centering popup on horizontal scroll.
  • Refactor code.

What can you do?

  • Report bugs
  • Help optimizing code
  • Give suggestions

Downloads

v 0.7.0 minified

v 0.6.0 developer

v 0.6.0 minified

v 0.5.1 developer

v 0.5.1 minified

v 0.5.0 developer

v 0.5.0 minified

v 0.4.1 developer

v 0.4.1 minified

v 0.4.0 developer

v 0.4.0 minified

v 0.3.6 developer

v 0.3.6 minified

v 0.3.5 developer

v 0.3.5 minified

Kommentarer (184) Trackbacks (7)
  1. Hello!

    I love this script but I’m a bit of a JS noob – I’m trying to make it so that one of the modal popups happens about two seconds after the page loads… I have no idea how to do it.

    Please save me!

    :D

  2. Hello, sorry for my english and congratulations for the script… i have a select item in the popup, and i need that when the popup close, the value of that select item, pass to a input item in the main page…

    Thanks for all!

  3. how to make this work… i have everything… it doesn’t pop up…. please help!

  4. Does not Open in center on first call, but moves to center after first call..

    Any idea

  5. Is It possible to stop auto scrolling..

  6. @Nitesh
    $(‘#selector’).bPopup({follow:false});

  7. Hello,
    very nice work!
    But i have a problem, when using loadUrl, then horizontal align works bad. Any ideas for this? :) i know the problem, firstly it shows empty <div and centers it, after loading page then align somehow must work.
    Thanks

  8. Can this be used in commercial projects? we are developing a wordpress plugin and would like to use this script.

  9. Hello,
    first of all, very nice plug-in!!

    Now the problem :P I like the “follow” option, but i have a pop-up than can increase vertically its size. If the popup is bigger than the screen, then is imposible to see the bottom of the pop-up :_(
    Finally i have to use “follow: false” to use the scroll.

    Thanks!

  10. Hi,

    In IE9 the box aligns to the left rather than center, any ideas why? – It works fine in every other browser.

    Thanks

  11. Hi again,

    i have a form in the pop-up. When i open the pop-up all the inputs are cleared. I hava had to comment this:

    c.find(“input[type=text]“).val(“”)

    Maybe it can be parametrizable (Maybe it is yet? :P )

    Regards

  12. @ViPeRII + tksgqn: Use the latest version of bPopup 0.5.0. It has an onClose event.

    @Brad: Set a timeout.
    window.setTimeout($("#div_to_popup").bPopup(), 2000);

    @gonself: Use the latest version of bPopup 0.5.0. It has an onClose event. Pass the value to your input item there.

    @angelo rodelas: Try to look at my demo page http://dinbror.dk/bpopup. Otherwise post some code. Hard to help without.

    @Nitesh: Maybe your popup is missing a width? Post some code if I should help.

    @DDs: You have to define a width for now. Will look into a fix in the next release.

    @Kunal: Yes, as long as you keep my name and url in the script! What wordpress plugin are you doing?

    @David: A good rule is that if your popup is bigger than your screen, do not use a popup. Otherwise you have to set the vertical follow to false in your case.

    @Ben: Haven’t seen the issue yet. Will investigate!

    @David: Removed in latest release, bPopup 0.5.0

  13. Thanks for this helpful script.

    I have a problem… I am using modal with url calling from Iframe. It gives error as ‘Access Denied’ when I consume asp.net webservice at the time of unloading (click on ‘X’)

  14. Hi,

    Any further developments with the IE9 issue at all please?

    Thanks.

  15. @Ben: I just tested my demo page http://dinbror.dk/bpopup in IE9 and the popup aligns fine.
    Have you tested with my demo page? Which version of IE9 are you using, not a beta release?

  16. @Titus: Could you show me your code or even better a live example? Hard to help otherwise!

  17. how to have the popup close automatically in a certain time?

  18. about my question:
    “how to have the popup close automatically in a certain time?”

    i found the solutions:
    setTimeout($(“.buttonClose”).click(), 1000);

    bpopup is CooL :-bd

  19. Hi, I’m sorry for posting a comment, I can’t really find an appropriate contact form (language barrier). I am using the bPopup and have an issue with needing links in the popup to stay in the popup (for event registration/confirmation). Can you email me directly and I can provide examples and maybe figure out a way to get this to work for us? Thnx in advance.

  20. Hi,

    For some reason, it doesn’t look right on IE8.

    http://www.gracebasilio.com/corfu44/

    It doesn’t cover the whole space, the modal appears after the first div. Is it because of the div?

    What could I do?

  21. @Ryan: Do you have a live example I can see? Otherwise you may find the “contentContainer” attribute interesting!

    @Grace Basilio: I can see that your example is in quirks mode! Serve a properly document type and the problem is fixed.

  22. Hi I am just wondering how I can have it so the b close link is the only thing that closes the pop up, as at the moment if I click outside onto the overlay the box will disappear.

    thanks

    Ad

  23. Nice library. Works well for simple popups, but I’m having an issue with a form in the popup. I have a popup with a few form fields and asp.net postback button. The button doesn’t fire the postback. Is there anything I have to do above what’s been documented to get that to work. If I put a validator control on the form, the button does fire the validator, but doesn’t postback. I removed the validator to make sure that wasn’t the issue and the form won’t post back with a simple submit button. Thanks in advance.

  24. I got it. I forgot to add the “appendTo: ‘form’” api. Once I did that, worked like a champ.

  25. @Adam Bourne: You can set escClose and modalClose to false. They’re true by default!

    @Mike: Coolio

  26. The pop only centers after its called or clicked for the second time. Any ideas on how to fix this?

  27. Outstanding work. Absolutely outstanding!

    I wish I’d found this HOURS ago. After losing 10+ hours fiddling first with FancyBox’s lame iframes, then moving to buggy-FaceBox which (in some ways) is even worse!

    This is a breath of fresh air! Wow I can’t wait to try it on my Ajax calls!

    Terrific Job.

  28. What am I doing wrong?

    I’ve got the following code in my script:

    $(‘#foo’).live(‘click’, function() {

    $(‘body’).append(”);

    $(“#popup”).bPopup({
    appendTo:’body’,
    loadUrl: ‘http://google.com’,
    zIndex: 2,
    onClose: function() {
    $(‘#popup’).remove();
    }
    });
    return false;
    });

    It works just fine but ONCE. If you try clicking that same link a second time, it acts normally… No popup! I get an error in my console:

    $(“#popup”).bPopup is not a function
    [Break On This Error] onClose: function() {

    Help!

  29. I should say… If you click it a THIRD time, (after the error) it ‘resets’ – giving you a popup, followed by an error (if you click it) again..

  30. All I really want to do is use the script to load ajax content. Is there something I’m not understanding?

  31. Just looked at your source. Seems you’ve got a whole bunch of ‘display:none’ divs at the bottom of your code. Sorry about all these comments! I’ve been at this for 10+ hours between 3 scripts and yours is by far the most promising! THere’s gotta be a way to generate/remove containers whenever the trigger is clicked, that way I don’t have to create all my selectors/containers in advance!

    Ok that’s it :)

  32. Nevermind.. figured it out :)

    SUGGESTION: You NEED to add an onBeforeOpen event! *VERY* important as this allows you to initialize/attach any plugins, states, classes, etc. for situations where content is being loaded via ajax or your loader function.

    WITHOUT this event, the popup first opens + Looks like crap! and THEN (using the callback) you see the classes/changes applied. This is definitely an oversight!

  33. @Christopher:
    Hard to tell without any code, but if you add a width to the element which pops up it should fix it.

    @Daniel:
    Thanks for the fine words. Did you solve everything?
    The issue with your code example is that you remove the element $(“#popup”) from the DOM on close and next time your trigger a click it’ll try to open an element which doesn’t exists any more. Use .empty() instead.
    Regarding the onBeforeOpen suggestion, have your tried the onOpen event? It’s triggered before the callback and before the popup pops up. That should do the trick.

  34. Do you have a working example of using iframe? I can’t make it work and I am using all of the instructions above

  35. Hi,

    I have tried to use this on my aspx page but it doesnt work. .bPopup (“bla”) doenst run.Could you help me?

    thank you

  36. @esk: Hard to help without seeing any code! But are you appending bPopup to the form like I wrote in the API? (.bPopup({appendTo: ‘form’});)

  37. There is no smooth transition or fading in IE9. All the examples on you website have nice smooth fading and transitions in Firefox, but in IE9 they don’t and they look bad.

  38. I have used the code below:

    $(‘#popup’)
    .bPopup({fadeSpeed:1, followSpeed:1, opacity:0.8, modalClose:false});
    just like on your demo page.How should I use .bPopup method?

  39. Awesome script! I’ve been looking for one that works this well and is this flexible for a long time.
    It looks great in Firefox but in IE9 the transitions don’t work and for some reason the text gets a blue underline. Can you help?

    http://www.waterscenery.com/info.php
    Click the thumbnail near the bottom of the page.

    Thanks

  40. @esk:
    As I wrote you MUST USE appendTo: ‘form’ when playing with aspx pages:


    $('#popup')
    .bPopup({fadeSpeed:1, followSpeed:1, opacity:'0.8', modalClose:false, appendTo: 'form'});

    @Steve and Stephen:
    Working fine at my computer, windows 7 prof, IE version: 9.0.8112.16421.
    Which browser version and operating system are you using?

  41. Nice job! I have some difficulty to use it to open an modal popup in which there is a form to be submit. When I click the submit button, no response. Can u help? or an example??

  42. Thank you very much for your library. I just have a question: I’ve used your popup with an iFrame inside a div container. The problem’s whenever I trigger popup to show. The iFrame’ll reload again. Is there anyway to stop the inside iFrame from reloading?

    Thanks again for your great work :)
    Dratwister

  43. I’m also using IE version: 9.0.8112.16421 but my operating system is Vista Home Premium. I don’t think your script is the problem. I’ve noticed web pages don’t scroll smoothly so I must have other issues.

    Thanks anyway

  44. Sorry for late response but I still couldnt solve the problem.Maybe it is my fault, I dont know.Besides I have skipped this and decided not to use.But very very good job mate.I ll use it in my further project probably.Thank you for your help.

  45. I think there is a bug…..

    .button_bottom {float:right;display: block;margin: 0 -9px 25px 0;padding: 0; }
    .button_bottom input {border:none;}
    .button_remove {padding: 0;float: right;border: none;height: 40px;width: 140px;
    background: url(‘/ofImg/remove_button.gif’) no-repeat top right;
    margin:-2px 9px 0 0;}
    .button_remove:hover {background: url(‘/ofImg/remove_button2.gif’) no-repeat top right;
    cursor:pointer;}


    Are you sure to remove following item(s)?

    Yes
    No

    $(document).ready(function(){
    $(“#btndelYN”).bind(“click”,function() {

    $(“#confirmyn”).bPopup({
    opacity: 0.28
    });
    event.preventDefault();

    // return false /* */
    }); /* */
    $(“#deleteYes”).bind(“click”,function() {
    $(‘#main_form’).append(”);
    $(“#main_form”).submit();
    }); /* */
    $(“#deleteNo”).click(function() {
    $(“#removeYN”).bPopup().close();
    }); /* */
    });

    If you click no button…..

  46. @dratwister:
    If you use the build in iframe creater, it’ll create the iframe every time it opens. If you want it to be cached you can create your iframe by yourself in the markup:


    < div id = "bPopup" >
    < iframe ... / >
    < /div >

    $(‘#bPopup’).bPopup();

    @Joe:
    Can you specify your issue? Or do you have a live link?
    - Is it an aspx side? Are you appending to the form?
    - When using the bPopup close function you must call it on the same element you open which in this case is $(“#confirmyn”) so call it like this $(“#confirmyn”).bPopup().close() or add the bClose class to the #deleteNo button.

  47. Thanks for your quick response. I will check it again! It is a nice plugin.

  48. Hi, It’s a nice script.
    Just a few little isses:
    Information about license is missing.
    It will be nice to have a not minified version available for download. Ok the not minified version is avaialble here http://dinbror.dk/downloads/jquery.bpopup-0.5.1.js, but then I am not sure if it’s the latest one becasue file 1.5.1 says @version: 0.5.0

  49. Thank you very much for your response. I sure will try your suggestion.
    Best regard! Best plugin either ^^


Leave a comment

(required)