bPopup
29.10.11: New release, bPopup version 0.6.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.
Contents
Usage
This is a really simplified example. For using bPopup I recommend that you're familiar with HTML and CSS.
Scripts to include:
<script type="text/javascript" src=" http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript" src="jquery.bpopup-0.6.0.min.js"></script>
HTML:
<html>
<head> HeadContent ... </head>
<body>
BodyContent
...
<a id="click" href="some url">Click here</a>
<div id="div_to_popup">Content of Popup</div>
</body>
</html>
CSS:
#div_to_popup {display:none;}
The magic:
$(document).ready(function(){
$("#click").bind('click', function(){
$("#div_to_popup").bPopup();
return false
});
});
Or with custom settings
$(document).ready(function(){
$("#click").bind('click', function(){
$("#div_to_popup").bPopup({appendTo:'form', zIndex: 2});
});
});
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'. |
| 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 |
| duration [int/string] ('normal') | Animation speed. Removed in version 0.3.6 |
| escClose [boolean] (true) | Should popup close when press on escape? |
| fadeSpeed [int/string] (250) | Animation speed on fadeIn/out. Version 0.3.6 |
| follow [boolean,boolean,boolean] ([true,true,false]) | Should the popup follow the screen vertical and/or horizontal on scroll/resize? [vertical, horizontal, fixed on screen] Version 0.3.6. Changed in version 0.5.0 and again in version 0.6.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 |
| minTop [int] (20) | Minimum distance from top to popup. Removed in version 0.3.6 |
| modal [boolean] (true) | Should there be a modal overlay behind the popup? |
| modalClose [boolean] (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, [vertical, horizontal] Version 0.5.0 |
| scrollBar [boolean] (true) | Should scrollbar be visible? |
| 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') |
| zIndex [int] (9999) | Popup z-index, modal overlay = popup z-index - 1. |
| Events | |
| onOpen | Event fires before the popup opens. Usage: $(selector).bPopup({ onOpen: function(){ //doMagic }}); Version 0.5.0 |
| onClose | Event fires after the popup closes. Usage: $(selector).bPopup({ onClose: function(){ //doMagic }}); Version 0.5.0 |
| Public Function | |
| close() | Closes the popup (Usage: $(selector).bPopup().close()). Version 0.4.0 |
Changelist
0.6.0 change-list:
- 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:
- Fixed issue: Added a duration time to the hide() call so onOpen and loadUrl always will be trigged when needed.
- Fixed issue: 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:
- 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.
- Fixed issue: 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:
- 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:
- 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
$.fnnamespace. - 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:
- New options: fadeSpeed, follow, followSpeed, vStart
- Deleted: duration, minTop
- Included position:absolute styling on popup in plugin
0.3.5 change-list:
- 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.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
Nød du denne artikel?
dinbror blog
Forfølg mig her
Nye indlæg
- CSS: Alt du bør vide omkring centrering
- Tips & tricks til bedre CSS og markup
- bPopup
- CSS: Sådan bruger du @font-face
- Sådan laver du hurtigere websider







23. maj, 2011 - 22:28
IE9 problem is corrected by using a valid <!DOCTYPE
24. maj, 2011 - 03:30
Almost got it! My problem is will not center correctly if position is set to auto. If i resize the browser window, seems to find center. Same problem on both IE and FF
Thanks,
1. juni, 2011 - 13:54
Thanks for the great plugin. I have found a problem that seems to be cross-browser (only tested IE9, FF4 and IE6). If the scrollwheel is moved whilst the bpopup is fading in, the div can appear with a low opacity or even set to zero (the overlay loads OK). Any further calls to the bpopup will show the same level of fade. The bpopup is there as mouseover indicates its presence but strangely the callback does not execute either.
I know not many people will scroll the site whilst loading but it also occurs if the page is scrolled to the bottom and then reloaded (the browser will jump to the bottom whilst the popup is fading in) and bpopup is called on document.ready.
It also happens on your examples page if you can click and move the mousewheel fast enough!
Any insights would be great. The only workaround I have is to set fadeSpeed:0 but it obviously loses the nice effect.
9. juli, 2011 - 08:18
how to close the pop up use button sir??
I am adding function on click, but only disabble the popup.. not disammble the background dark..
function hide()
{
document.getElementById(‘my_form_name’).style.display=’none’;
return false
}
9. juli, 2011 - 20:00
Very slim and nice feature.
But it doesn´t work correctly in some safari-browsers, especially if using with the iPad.
Hope that someone can find the problem.
10. juli, 2011 - 00:49
Hi, I found a possible solution to fix the issue centering the popup in Chrome & Safari
Use:
$(window).load(function() {
$(“#div_to_popup”).bPopup();
});
The root problem is that WebKit browsers (Safari and Chrome) load JavaScript and CSS information in parallel. Thus, JavaScript may execute before the styling effects of CSS have been computed, returning the wrong answer and this happend when we call to .outerHeight() and .outerWidth() .
See ya!
13. juli, 2011 - 19:12
The following code successfully opens on first click but on second click it opens the link as if JS was disabled:
$(“a[class*='popupbox-link']“).live(“click”,function () {
if($(“#popupbox”).length == 0){
$(‘body’).append(‘Stäng‘);
}
var url = this.href;
$(“#popupbox”).bPopup({contentContainer:’#pContent’, loadUrl: url });
return false
});
Why it this?
29. juli, 2011 - 08:35
Thank you for helping out web developers with your efforts.
I saw an issue with IE8 though. Not sure if it is me or IE8.
Whenever the height of the popup div was larger than the available height of the browser window, the popup window did not render. What I did,
1. All I had was on the html page was the link to popup the modal window.
2. I set the height of the modal window to 400px
3. I resized the browser window to something less that 400px. The scroll bars on the browser window would not show as the html page itself has very little height since it contains only a link as the displayable elements
4. If I click on the link, the modal window did not show.
5. Then I resize the bowser window to more than 400px, reload the html page. This time the modal window renders fine
6. If I put a div around the link (to popup the modal) and set a minimum width of the div to larger than the modal winodw (say 500px) and I resize the browser window, the scroll bars now show. Now if I click to display the modal, it shows up.
I tested this on Chrome and it was OK. I have not tried this on other browsers.
15. august, 2011 - 18:18
Would be an amazing plugin if only it supported multiple instances.
24. august, 2011 - 09:44
not work with jquery 1.6.2….
24. august, 2011 - 10:05
sorry..my mistake..it works
22. september, 2011 - 21:07
Hi,
I use bPopup for loading php files in it with .bPopup({loadUrl
It has no modal, so while the popup is open you can click anywhere else.
Now if you click on another link that should be loaded in the popup it should close and open again with the new content.
So how do I check whether the popup is already open or not?
Or do you know a good way how to realize this?
Thanks in advance!
28. september, 2011 - 18:48
I love the plugin, but I ran into a problem with its implementation– the method of loading the modal window is destructive, meaning that once a modal has been opened subsequent clicks on the modal trigger yield no result.
I wanted users to be able to reopen the window in case they closed it prematurely. To get around the destructive issue, I used this bit of code:
$(“a.form”).click(function(){
var form = $(this).next(“div”).clone();
$(this).next(“div”).bPopup({closeClass:’close’});
$(form).insertAfter(this);
return false
});
And voila! Infinite re-opens of the modal window. I thought this would be useful to anyone seeking to accomplish the same.
30. september, 2011 - 23:59
I am having just a small problem with have a contact form inside this popup. Everything works great but the submit button closes the popup without processing. Am I doing something wrong here?? Any suggestions are more than welcomed.
16. oktober, 2011 - 19:49
HI,
Simple question I hope. How can I make the popup window bigger ie increase the width and the height.
Thanks
19. oktober, 2011 - 20:41
Hi all.
bPopup version 0.6.0 is almost ready for release. It’ll add support for multiple popups on same time, a fixed follow behaviour and more. You can follow the release log at my twitter account.
I’ll look through all the unanswered question when the new version is released. If you still have issues please repost and I’ll start with them. It’s also much easier for me to help if you provide a live example or add it in a jsfiddle.
30. oktober, 2011 - 19:34
When you use the close function on 0.6.0, $(“#…..”).bPopup().close(); it removed the popup box, but not the modal greyed out background. Using Chrome 15.0.874.106 m. Not sure if this is a bug or if I’m doing something wrong.
Thanks
31. oktober, 2011 - 22:59
Hello everyone:
I’m doing some test with this great plugin, but when y load ajax pages and then I close them a pair of times, this error arises o.contentContainer.empty is not a function.
The code I use is the next
$(“#popup2″).bPopup({ contentContainer: ‘#pContent’, loadUrl: “pages/trabajo/caso.html”, position: [40, 450], modalColor:’black’, modal: true, closeClass: “bClose”, opacity:1.0 });
Thank you and best regards.
5. november, 2011 - 02:56
On my website, using version 0.6.0, calling “$([my selector]).bPopup().close()” does not work. The popup will disappear but the gray background will remain.
Clicking the gray background or hitting escape (both before the error occurs or after the error occurs) works fine.
Version 0.5.1 works fine on my website. I’m happy to provide additional info as needed.
(Clicking the gray background reaches line 76 in jquery.bpopup-0.6.0.js. Calling close() manually does not.)
8. november, 2011 - 20:30
@Paul + Drek: Thanks for your input. It was a bug. I forgot to update the close function when I did multiple popups possible. Good catch. The new download (still 0.6.0) fixes the issue.
@Shun: Hmm the code looks fine. (No need for setting the closeClass, modalColor and modal since you’re using the defaults;)). Can you maybe provide a live example or insert your code in a jsfiddle.net where you can reproduce the error?
Do you also get an error after closing my example 4b on the demo page a couple of times?
10. november, 2011 - 11:08
How can i close bpopup from inside iframe ?
15. november, 2011 - 15:02
I can reproduce the “o.contentContainer.empty is not a function” error by immediately loading a popup after an ajaxstop. Once you close the popup this error comes up hundreds of times in firebug. I’m trying something like this i.e.
$(“.ui-tabs-panel”).ajaxStop(function() {
$(“#worklistDealSummaryContainer”).bPopup({
appendTo: ‘#worklistResults’,
contentContainer: ‘#worklistDealSummary’,
loadUrl: ‘contract/${sessionScope.contract}/dealsummary’,
position: ['auto', '12%']
});
});
16. november, 2011 - 05:37
Hey, for those who are struggling on the IE9 align issue, I found the cause of the problem. Just need to remove the css: “margin: 0 auto” from your modal container, guess it conflicts with the default center position from the plugin. hope this could work for you guys~! btw, thanks dinbror for the great plugin, very simple and elegant.
26. november, 2011 - 20:57
Hi dinbror,
When I do multiple popups. The first time works without any problems. It creates a div with bPopup2 class. When I close the second popup and create a new popup again. It create a div with bPopup1 ignoring the first popup that was still open. So I see two divs with bPopup1 class.
17. december, 2011 - 10:25
How can i close bpopup from inside iframe ?
22. december, 2011 - 19:33
Any way to stop tabbing behind the modal?