How to create a stunning and smooth popup using jQuery


Step 1: What We will do?

A picture is worth a thousand words… so We will learn how to do something like this:
Final result

Step 2: Setting up our simple webpage

So we need a simple HTML webpage with 2 html divisions: popupContact for the popup and backgroundPopup for helps our popup gets more focus and style ;)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">  
  3. <head>  
  4.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  5.     <title>yensdesign.com - How to create a stuning and smooth popup in jQuery</title>  
  6.     <link rel="stylesheet" href="general.css" type="text/css" media="screen" />  
  7.     <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>  
  8.     <script src="popup.js" type="text/javascript"></script>  
  9. </head>  
  10. <body>  
  11.     <center>  
  12.         <a href="http://www.yensdesign.com"><img src="logo.jpg" alt="Go to yensdesign.com"/></a>  
  13.         <div id="button"><input type="submit" value="Press me please!" /></div>  
  14.     </center>  
  15.     <div id="popupContact">  
  16.         <a id="popupContactClose">x</a>  
  17.         <h1>Title of our cool popup, yay!</h1>  
  18.         <p id="contactArea">  
  19.             Here we have a simple but interesting sample of our new stuning and smooth popup. As you can see jQuery and CSS does it easy...  
  20.             <br/><br/>  
  21.             We can use it for popup-forms and more... just experiment!  
  22.             <br/><br/>  
  23.             Press ESCAPE, Click on X (right-top) or Click Out from the popup to close the popup!  
  24.             <br/><br/>  
  25.             <a href="http://www.yensdesign.com"><img src="logo.jpg" alt="Go to yensdesign.com"/></a>  
  26.         </p>  
  27.     </div>  
  28.     <div id="backgroundPopup"></div>  
  29. </body>  
  30. </html>  
Note: If you see the <head> section We included the jQuery hosted in a Google site. Some people do that to save in cache the jQuery library and save loading time in lots of webpage by having the same resource.
And here go the CSS, to add style to our html document and most important to hide our html main divisions: popupContact and backgroundPopup that conforms the “popup core” and We don’t want to see our popup at the begining, right? ;)
  1. html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em,  
  2. font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody,  
  3. tfoot, thead, tr, th, td {  
  4. border:0pt none;  
  5. font-family:inherit;  
  6. font-size:100%;  
  7. font-style:inherit;  
  8. font-weight:inherit;  
  9. margin:0pt;  
  10. padding:0pt;  
  11. vertical-align:baseline;  
  12. }  
  13. body{  
  14. background:#fff none repeat scroll 0%;  
  15. line-height:1;  
  16. font-size12px;  
  17. font-family:arial,sans-serif;  
  18. margin:0pt;  
  19. height:100%;  
  20. }  
  21. table {  
  22. border-collapse:separate;  
  23. border-spacing:0pt;  
  24. }  
  25. caption, th, td {  
  26. font-weight:normal;  
  27. text-align:left;  
  28. }  
  29. blockquote:before, blockquote:after, q:before, q:after {  
  30. content:"";  
  31. }  
  32. blockquote, q {  
  33. quotes:"" "";  
  34. }  
  35. a{  
  36. cursorpointer;  
  37. text-decoration:none;  
  38. }  
  39. br.both{  
  40. clear:both;  
  41. }  
  42. #backgroundPopup{  
  43. display:none;  
  44. position:fixed;  
  45. _position:absolute/* hack for internet explorer 6*/  
  46. height:100%;  
  47. width:100%;  
  48. top:0;  
  49. left:0;  
  50. background:#000000;  
  51. border:1px solid #cecece;  
  52. z-index:1;  
  53. }  
  54. #popupContact{  
  55. display:none;  
  56. position:fixed;  
  57. _position:absolute/* hack for internet explorer 6*/  
  58. height:384px;  
  59. width:408px;  
  60. background:#FFFFFF;  
  61. border:2px solid #cecece;  
  62. z-index:2;  
  63. padding:12px;  
  64. font-size:13px;  
  65. }  
  66. #popupContact h1{  
  67. text-align:left;  
  68. color:#6FA5FD;  
  69. font-size:22px;  
  70. font-weight:700;  
  71. border-bottom:1px dotted #D3D3D3;  
  72. padding-bottom:2px;  
  73. margin-bottom:20px;  
  74. }  
  75. #popupContactClose{  
  76. font-size:14px;  
  77. line-height:14px;  
  78. right:6px;  
  79. top:4px;  
  80. position:absolute;  
  81. color:#6fa5fd;  
  82. font-weight:700;  
  83. display:block;  
  84. }  
  85. #button{  
  86. text-align:center;  
  87. margin:100px;  
  88. }  
The CSS code is so long because I always used to add a little CSS snippet to reset the CSS and it helps me a lot in the layout process in all websites.
So if You follow all steps you may have something like this on your screen:
Did you follow all steps?

Step 3: Adding magic to our popup with jQuery

Here is the core of the tutorial, the jQuery code that will allow Us to do a stunning and smooth window popup with just a few functions in our popup.js file, but first of all we must set up a variable called popupStatus to control the status of our popup:

  1. //SETTING UP OUR POPUP  
  2. //0 means disabled; 1 means enabled;  
  3. var popupStatus = 0;  
Now the function to load our popup:
  1. //loading popup with jQuery magic!  
  2. function loadPopup(){  
  3. //loads popup only if it is disabled  
  4. if(popupStatus==0){  
  5. $("#backgroundPopup").css({  
  6. "opacity""0.7"  
  7. });  
  8. $("#backgroundPopup").fadeIn("slow");  
  9. $("#popupContact").fadeIn("slow");  
  10. popupStatus = 1;  
  11. }  
  12. }  
Function for close/disable our popup:
  1. //disabling popup with jQuery magic!  
  2. function disablePopup(){  
  3. //disables popup only if it is enabled  
  4. if(popupStatus==1){  
  5. $("#backgroundPopup").fadeOut("slow");  
  6. $("#popupContact").fadeOut("slow");  
  7. popupStatus = 0;  
  8. }  
  9. }  
We want to center our popup too…
  1. //centering popup  
  2. function centerPopup(){  
  3. //request data for centering  
  4. var windowWidth = document.documentElement.clientWidth;  
  5. var windowHeight = document.documentElement.clientHeight;  
  6. var popupHeight = $("#popupContact").height();  
  7. var popupWidth = $("#popupContact").width();  
  8. //centering  
  9. $("#popupContact").css({  
  10. "position""absolute",  
  11. "top": windowHeight/2-popupHeight/2,  
  12. "left": windowWidth/2-popupWidth/2  
  13. });  
  14. //only need force for IE6  
  15.   
  16. $("#backgroundPopup").css({  
  17. "height": windowHeight  
  18. });  
  19.   
  20. }  
So if we have a variable popupStatus to control the popup status and functions the functions: loadPopup, disablePopup and centerPopup for load, close and center our popup we need to interact with them by using jQuery events control in the $(document).ready function.
First of all remember that the following code will be into this:
  1. $(document).ready(function(){  
  2. //following code will be here  
  3. });  
We want to activate our popup when the button with id #button is clicked, so:
  1. //LOADING POPUP  
  2. //Click the button event!  
  3. $("#button").click(function(){  
  4. //centering with css  
  5. centerPopup();  
  6. //load popup  
  7. loadPopup();  
  8. });  
Simple eh?, so continue for the closing event. We want to close our popup in 3 different ways: Hitting ESC key, Clicking out from the popup and Clicking on X, so let’s do that:
  1. //CLOSING POPUP  
  2. //Click the x event!  
  3. $("#popupContactClose").click(function(){  
  4. disablePopup();  
  5. });  
  6. //Click out event!  
  7. $("#backgroundPopup").click(function(){  
  8. disablePopup();  
  9. });  
  10. //Press Escape event!  
  11. $(document).keypress(function(e){  
  12. if(e.keyCode==27 &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; popupStatus==1){  
  13. disablePopup();  
  14. }  
  15. });  

Comments

Popular posts from this blog

GROUP BY, CUBE, ROLLUP and SQL SERVER 2005

How to get content of Ckeditor & Fckeditor using Javascript

How to Fix Error- Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out