I have written a very simple jQuery popup that you can use it right away. Very simple code, if you know jQuery a little you can understand the code easily. This script contains only 20 lines of code if you remove the comments and spaces!. Tested in all major Browser, Working Perfectly!
You can download the script at the bottom of the page!
1. See the javascript part here:
[code]
<script type=”text/javascript”>
$(document).ready(function(){
//open popup
$(“#pop”).click(function(){
$(“#overlay_form”).fadeIn(1000);
positionPopup();
});
//close popup
$(“#close”).click(function(){
$(“#overlay_form”).fadeOut(500);
});
});
//position the popup at the center of the page
function positionPopup(){
if(!$(“#overlay_form”).is(‘:visible’)){
return;
}
$(“#overlay_form”).css({
left: ($(window).width() – $(‘#overlay_form’).width()) / 2,
top: ($(window).width() – $(‘#overlay_form’).width()) / 7,
position:’absolute’
});
}
//maintain the popup at center of the page when browser resized
$(window).bind(‘resize’,positionPopup);
</script>
All are commented so need to explain the code i think!!.
2. Styles used:
[code]
<style>
#overlay_form{
position: absolute;
border: 5px solid gray;
padding: 10px;
background: white;
width: 270px;
height: 190px;
}
#pop{
display: block;
border: 1px solid gray;
width: 65px;
text-align: center;
padding: 6px;
border-radius: 5px;
text-decoration: none;
margin: 0 auto;
}
</style>
3. HTML Part:
[code]
<a href=”#” id=”pop” >PopUp</a>
<br />
<form id=”overlay_form” style=”display:none”>
<h2> Put your contents here..</h2>
<label>Username: </label><input type=”text” name=”username” /><br /><br />
<label>Password: </label><input type=”text” name=”password” /><br /><br />
<input type=”button” value=”Login” />
<a href=”#” id=”close” >Close</a>
</form>
You can see the demo and if you wish to download you can do it here!
Also check the Demo of Popup with Background Overlay here:
very nice and simple,
Thanks a lot.
shamjith
Thank so much my friend
Excellent ::
Simple and easy….to display the popup
greate
nice…
simple and good …
thank you for sharing….
m a aleem (fahd4007)
web graphic designer
fahd4007.deviantart.com
nice..
good and simple…
m a aleem
web graphic designer
fahd4007.deviantart.com
thank you for this.. this is simple and effective… thanks a lot…
Its nice and easy to use
Thank you for this great little script. I was just wondering how you would open the form if there were several buttons on the page that activated the pop up?
The buttons will be dynamically created so I can’t hardcode different div id’s for them.Thanks for your help!
wonderful, I have made some changes and adapted the styles to my needs.
my version is here: http://www.constructorafraga.cl
thank you very much
Alejandra
Excellent..
Very nice work. Exactly what I needed. Thanks!
Excellent ..Thanks! Dear
Great work. I will try to use this in my CMS 🙂 Thx
Verry good work. I will try to use this with my OC store. Thx
Y you no indent ?
Download the code, its indented!!
very nice popup thanks
Is there a simple tweak in the Jquery to make it load onload? Similiar to:
https://www.kendrascott.com/
Yes, you can, Right now the popup will appear after you click the link (click event). You can just remove the click event to make it appear after the document is ready (loaded fully).
Just comment line no. 5 and no. 8. see below:
$(document).ready(function(){
//open popup
// $(“#pop”).click(function(){
$(“#overlay_form”).fadeIn(1000);
positionPopup();
// });
I am going to give this a whirl! Thanks for your speedy response! You rock!
Love it man! Just what I needed.
can u please tell me how will I get style of writing on the page as https://www.singhjasmeet.com/ … is there any jquery module for this ???
tnx in advance
hi,
here is the script for you https://blog.theonlytutorials.com/simple-jquery-typing-script-demo-download/
Hi,thanks its usefull but i was ask,how to active several popup window for other butttons?pls reply at this email aqilhex@gmail.com
Is there a way to modify the scripting so that the pop up appears next to the button that is launching it rather than in the centre of the page?
Yes, you can adjust the position: Copy the below jquery code, instead of the above.
$(document).ready(function(){
//open popup
$(“#pop”).click(function(){
$(“#overlay_form”).fadeIn(1000);
});
//close popup
$(“#close”).click(function(){
$(“#overlay_form”).fadeOut(500);
});
$(“#overlay_form”).css({
left: 100,
top: 100,
position:’absolute’
});
});
you can position it by changing the left and top values.
hi, how can i close the popup if we click outside the popup
Replace the code(line 1 to 14 from above)
$(document).ready(function(){
//open popup
$("#pop").click(function(){
$("#overlay_form").fadeIn(1000);
positionPopup();
});
//close popup
$("#close").click(function(){
$("#overlay_form").fadeOut(500);
});
$('body').mouseup(function(){
$("#overlay_form").fadeOut(500);
});
$("#overlay_form").mouseup(function(e){
e.stopPropagation();
});
});
This is great! One more addition. How could we set a Timeout, so that the popup closes automatically after 5 seconds? Thanks again for all the help!
good for beginners
Hello I was wondering how you can modify this code so that you can have more than 1 link with a popup box appear, Thank you!
Check the below link and get the source code from it:
https://theonlytutorials.com/demo/jquery_popup2.html
simple and good, thnx bro..
This code is great! I have a question, how do you modify this code so that it can be used for more than one popup on a single page?
Thank you!
Sorry, I didn’t see your reply from earlier. Thank you so much! I greatly appreciate you taking the effort to help me out!!!
great script!
how can i allow the popup to have scrollbars for the content?
i’m actually using it to hold some generated data that takes up a bit of space. but the data ends up going outside the popup div if it’s too long.
i want to limit it to the dimensions given in the css and allow scrollbars if necessary.
is this possible?
thanks!
Never mind I figured it out. Just added
overflow-y: scroll;
to #overlay_form
That’s CSS3 but you can of course use overflow: scroll and just end up with both scroll bars whether you need them or not.
Great Code!
What’s the easiest way to get the page behind the box to fade-out, so the user is more focused on the pop-out?
Thanks!
Hi, check this demo: https://theonlytutorials.com/demo/jquery_popup_with_background_overlay.html
Also I have updated the download file along with the background overlay. Just download the file again or click the below link to download it.
https://theonlytutorials.com/downloads/jquery-popup.zip
Awesome! It works on the page by itself, but I also want to make a few more adjustments to it like, for it to open on launch, and close if you close the background. But when I add those changes I lose the faded background?
Here is what I got from previous comments and the download, Am I missing something? :
$(document).ready(function(){
//open popup
//$(“#pop”).click(function(){
$(“#overlay_form”).fadeIn(1000);
$(“.background_overlay”).fadeIn(500);
positionPopup();
});
//close popup
$(“#close, .background_overlay”).click(function(){
$(“#overlay_form”).fadeOut(500);
$(“.background_overlay”).fadeOut(500);
$(‘body’).mouseup(function(){
$(“#overlay_form”).fadeOut(500);
});
$(“#overlay_form”).mouseup(function(e){
e.stopPropagation();
});
});
//position the popup at the center of the page
function positionPopup(){
if(!$(“#overlay_form”).is(‘:visible’)){
return;
}
$(“#overlay_form”).css({
left: ($(window).width() – $(‘#overlay_form’).width()) / 2,
top: ($(window).width() – $(‘#overlay_form’).width()) / 7,
position:’absolute’
});
}
//maintain the popup at center of the page when browser resized
$(window).bind(‘resize’,positionPopup);
#overlay_form{
position: absolute;
border: 8px solid #545F7A;
padding: 10px;
background: #F0EAB5;
width: 300px;
height: 300px;
z-index: 200;
}
#pop{
display: block;
border: 1px solid gray;
width: 65px;
text-align: center;
padding: 6px;
border-radius: 5px;
text-decoration: none;
margin: 0 auto;
z-index: 100;
}
.background_overlay { position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 1; background:black; opacity: 0.5;}
I also think it would be cool if the box could float in the center of the page if someone where to scroll the main page? Any help doing that?
Thanks again for your help!
I got it mostly figured out now, except the closing now won’t work at all…?
Thanks again man this is sweet!
Everything you wanted is here, just open the below link and copy the source code 🙂
Popup on Load and Fixed on Scroll
AWESOME!!! Thank You!
For something so simple, this was absolutely outstanding. I can use the dialog plug ins and such for jquery, but this was just so much easier and cleaner.
I very rarely comment on posts, but this definitely called for it. Thank you for this, it is much appreciated. Keep up the great work!
Thank theonlytutorials!
Where I have multiple popup, then how?
This is a great popup! Thanks for sharing.
One question… the links for the background overlay are no longer working. Is there any way that you can add that again?
Thanks!
Thanks for noticing it. Please check now. I have just uploaded those files again.
Thanks so much! Appreciate it!
It was a great popup with simple code. Thanks!
I do notice that the background overlay is not fading in IE8. However it works well in FF and Chrome. Any clue on why is it not working in IE8 ??
there is a style used for transparency
opacity: 0.5
Opacity will not work in IE, you need to update them
New style:
.background_overlay {
position: fixed; left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 1;
background:black;
opacity: 0.5;
/*newly added */
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
filter: alpha(opacity=50);
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
}
It works great! The background overlay fade is not working in IE8. Any clue why ??
there is a style used for transparency
opacity: 0.5
Opacity will not work in IE, you need to update them
New style:
.background_overlay {
position: fixed; left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 1;
background:black;
opacity: 0.5;
/*newly added */
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
filter: alpha(opacity=50);
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
}
Thank you very much.. it’s very use full…
I am implementing it here https://www.itegroup.al/produkte
but the popups will not show on the center of the page once you scroll down.
any quick solution?
nevermind, got it through css 🙂
Thanx ya.. vry simple…..
I have my pop ups located beneath an anchor a few divs down the page. When the pop ups are clicked, the page repositions back to the top and centers the pop ups accordingly within that new screen.
Is there a way to keep the pop ups centered on the page no matter where down the page you’re currently viewing?
(exact same issue as elvin@ccwebistes.net, but I can’t figure it out. Heh…)
Hi Robin,
Its very simple, Just change position:’absolute’ to position:’fixed’ in the JavaScript part (line no. 24)
$("#overlay_form").css({
left: ($(window).width() - $('#overlay_form').width()) / 2,
top: ($(window).width() - $('#overlay_form').width()) / 7,
position:'fixed'
});
fabulous………. no doubt….. very very best content and ….. also shortest one.. thanks alot for making this type blog to help people.
About me?? Name: Girdhar singh M: +91-7838441471 feel free to contact for discussion. thanks again.
I have table where i want to show pop ups whenever the elements of the first column are clicked, I used the above code. But the pop up opens only for the first row and not remaining. :-(.
can you share your page so i can help you?
Thanks Can u explain how can i put pop up at the right side of my website……?with pop up box
Thank’s Agurchand Babu.
It is really great
Great – but it come up underneath some elements on the page! I’ve played with z-index but can’t cure it. Any ideas please. Thanks
tnku sir g…..tnku so much …
thanks
simple and nice