逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
css中opacity在Safari, Firefox, Opera里都可以使用,但是IE中不行,在IE里要使用filter,但是如果使用jQuery就可以解决不同浏览器的兼容
$("#xx").css("opacity", 0.7);

How to create a stunning and smooth popup using jQuery

 September 26th, 2008

Nowadays, websites are more and more rich and interactive and users are becoming more and more critical with all things in websites. Using windows popup to show important information are in the air and We are going to learn how to create a stunning and great window popup from the scratch using jQuery in a simple and clean tutorial, the first one of this new blog. Just enjoy it and tell me uf you can not understand something because my english sucks…

You can test the tutorial working overhere.

Tested in: Firefox, Internet Explorer 6 & 7, Opera (old and 9.52) , Safari & Chrome.

What will We need?

We only need these ingredients for our stunning popup:

  • HTML
  • CSS
  • jQuery Library
  • Desire to learn :D

jQuery is a new javascript library focused on fast and stable development that I am using last months to “add magic” and AJAX in sites as www.plusdeporte.comwww.plusmusica.com and others, in words of their creators:

jQuery is a fast and concise JavaScript Library that simplifies HTML document transversing, event handling, animating and AJax interactions for rapid web developtment.

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

As You can see We will do a simple HTML website with a button that activates our stunning and smooth popup :). So let’s rox guys!

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 andbackgroundPopup 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: loadPopupdisablePopup andcenterPopup 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. });  

Step 4: Trying our stunning and smooth window popup in jQuery!

And that’s all guys, I hope this tutorial helps you. You can try the tutorial online here to see how It is working and download here all sources (use Saving as… if it doesn’t download).

posted on 2010-11-10 04:51 逛奔的蜗牛 阅读(6248) 评论(2)  编辑 收藏 引用 所属分类: 其他编程

评论

# re: JavaScript:使用jQuery解决opacity在不同浏览器的兼容性 2012-07-04 19:23 MARGUERITEBall21
Have no cash to buy a house? Do not worry, because it is real to take the <a href="http://goodfinance-blog.com/topics/mortgage-loans">mortgage loans</a> to resolve such problems. Therefore take a commercial loan to buy everything you need.   回复  更多评论
  

# re: JavaScript:使用jQuery解决opacity在不同浏览器的兼容性 2012-08-21 10:46 custom thesis
That’s not expensive to select the dissertations take the superb outcome connecting with this good topic and use it in thesis writing "topdissertations.com". And the custom writing service will thank you for this!   回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理