Ver Mensaje Individual
  #16 (permalink)  
Antiguo 23/03/2010, 12:28
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 2 meses
Puntos: 20
Respuesta: Crear modal box con jquery y efecto fade in/out

He conseguido rescatar el código de un compañero del foro deirdre, que hace con jquery lo que buscábamos, y funciona tanto en IE como en FF. Muchas gracias deirdre.

http://www.forosdelweb.com/f77/conse...howbox-734261/

El código adaptado al ejemplo que os quiero mostrar es el siguiente:

Código HTML:
Ver original
  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">
  3.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4.     <title>Popup suave</title>
  5.     <link rel="stylesheet" href="css/estilo.css" type="text/css" media="screen" />
  6. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
  7.  
  8. <script type="text/javascript">
  9.     var popupStatus = 0;
  10.     function loadPopup() {
  11.     if (popupStatus == 0) {
  12.         $("#backgroundPopup").css({
  13.             "opacity": "0.7"
  14.         });
  15.         $("#backgroundPopup").fadeIn("slow");
  16.         $("#popupContact").fadeIn("slow");
  17.         popupStatus = 1;
  18.     }
  19. };
  20.  
  21. function disablePopup() {
  22.     if (popupStatus == 1) {
  23.         $("#backgroundPopup").fadeOut("slow");
  24.         $("#popupContact").fadeOut("slow");
  25.         popupStatus = 0;
  26.     }
  27. };
  28.  
  29. function centerPopup() {
  30.     var windowWidth = document.documentElement.clientWidth;
  31.     var windowHeight = document.documentElement.clientHeight;
  32.     var popupHeight = $("#popupContact").height();
  33.     var popupWidth = $("#popupContact").width();
  34.     $("#popupContact").css({
  35.         "position": "absolute",
  36.         "top": windowHeight / 2 - popupHeight / 2,
  37.         //"top": 10,
  38.         "left": windowWidth / 2 - popupWidth / 2
  39.     });
  40.     $("#backgroundPopup").css({
  41.         "height": windowHeight
  42.     });
  43. };
  44. $(document).ready(function () {
  45.     $("#button").click(function () {
  46.         centerPopup();
  47.         loadPopup();
  48.     });
  49.     $("#popupContactClose").click(function () {
  50.         disablePopup();
  51.     });
  52.     $("#backgroundPopup").click(function () {
  53.         disablePopup();
  54.     });
  55.     $(document).keypress(function (e) {
  56.         if (e.keyCode == 27 && popupStatus == 1) {
  57.            disablePopup();
  58.         }
  59.     });
  60. });
  61. </head>
  62.     <div id="cab">
  63.         <p>Linea 1</p><br/><p>Linea 2</p><br/><p>Linea 3</p><br/><p>Linea 4</p><br/><p>Linea 5</p><br/><p>Linea 6</p><br/><p>Linea 7</p><br/><p>Linea 8</p><br/><p>Linea 9</p><br/><p>Linea 10</p><br/>
  64.         <p>Linea 1</p><br/><p>Linea 2</p><br/><p>Linea 3</p><br/><p>Linea 4</p><br/><p>Linea 5</p><br/><p>Linea 6</p><br/><p>Linea 7</p><br/><p>Linea 8</p><br/><p>Linea 9</p><br/><p>Linea 10</p><br/>
  65.         <p>Linea 1</p><br/><p>Linea 2</p><br/><p>Linea 3</p><br/><p>Linea 4</p><br/><p>Linea 5</p><br/><p>Linea 6</p><br/><p>Linea 7</p><br/><p>Linea 8</p><br/><p>Linea 9</p><br/><p>Linea 10</p><br/>
  66.         <p>Linea 1</p><br/><p>Linea 2</p><br/><p>Linea 3</p><br/><p>Linea 4</p><br/><p>Linea 5</p><br/><p>Linea 6</p><br/><p>Linea 7</p><br/><p>Linea 8</p><br/><p>Linea 9</p><br/><p>Linea 10</p><br/>
  67.        
  68.         <div id="button">ABRIR MODAL BOX</div>
  69.     </div>
  70.    
  71.     <div id="popupContact">
  72.         <a id="popupContactClose">x</a>
  73.         <h1>Contacto</h1>
  74.         <p>Envíenos su opinión, la tendremos en cuenta. Gracias</p>
  75.         <div id="main_content_single">
  76.         <form method="post" action="contact.php">
  77.                 <label>Nombre:</label>
  78.                 <input class="textfield" type="text" name="nombre" maxlength="64" value="" />
  79.                 <label>E-mail:</label>
  80.                 <input class="textfield" type="text" name="email" maxlength="64" value="" />
  81.                 <label>Mensaje:</label>
  82.                 <textarea class="textarea" name="opinion" rows="8"></textarea>
  83.             <button type="submit" class="button">ENVIAR</button>
  84.         </form>
  85.         </div>
  86.     </div>
  87.     <div id="backgroundPopup"></div>
  88.     <p>Deirdre para Foros del Web</p>
  89. </body>
  90. </html>

Estilo css

Código CSS:
Ver original
  1. /* reset */
  2.  
  3. 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,
  4. 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,
  5. tfoot, thead, tr, th, td {
  6.     border: 0pt none;
  7.     font-family: inherit;
  8.     font-size: 100%;
  9.     font-style: inherit;
  10.     font-weight: inherit;
  11.     margin: 0;
  12.     padding: 0;
  13.     vertical-align: baseline;
  14. }
  15.  
  16. /* fin reset */
  17.  
  18. body {
  19.     background: #fff none repeat scroll 0%;
  20.     line-height: 1;
  21.     font-size: 12px;
  22.     font-family: arial,sans-serif;
  23.     margin: 0;
  24.     height: 100%;
  25. }
  26. a {
  27.     cursor: pointer;
  28.     text-decoration:none;
  29. }
  30. br.both{
  31.     clear:both;
  32. }
  33. #backgroundPopup{
  34.     display: none;
  35.     position: fixed;
  36.     _position: absolute; /* necesario para internet explorer 6 */
  37.     height: 100%;
  38.     width: 100%;
  39.     top: 0;
  40.     left: 0;
  41.     background: #ccc;
  42.     border: 1px solid #cecece;
  43.     z-index: 1;
  44. }
  45. #popupContact{
  46.     display: none;
  47.     position: fixed;
  48.     _position: absolute; /* necesario para internet explorer 6*/
  49.     width: 420px;
  50.     height: 370px;
  51.     background: #FFFFFF;
  52.     border: 12px solid #cecece;
  53.     z-index: 2;
  54.     padding: 12px;
  55.     font-size: 13px;
  56. }
  57. #popupContact h1{
  58.     text-align: left;
  59.     color: #800000;
  60.     font-size: 22px;
  61.     font-weight: 700;
  62.     border-bottom: 1px solid #D3D3D3;
  63.     padding-bottom: 2px;
  64.     margin-bottom: 20px;
  65. }
  66. #popupContactClose {
  67.     font-size: 14px;
  68.     line-height: 14px;
  69.     right: 6px;
  70.     top: 4px;
  71.     position: absolute;
  72.     color: #800000;
  73.     font-weight: 700;
  74.     display: block;
  75. }
  76. .button{
  77.     text-align: center;
  78.     font-size: 11px;
  79. }
  80. #button{
  81.     text-align: center;
  82.     font-size: 13px;
  83.     cursor: pointer;
  84.     width: 100px;
  85.     margin: 0 auto;
  86.     margin-top: 10px;
  87. }
  88. .textfield {
  89.     margin-bottom: 15px;
  90.     background-color: #ccc;
  91.     color: #484848;
  92.     border: 1px solid #383838;
  93.     padding: 4px 3px;
  94.     font-size: 0.9em;
  95.     width: 360px;
  96. }
  97. #main_content_single {
  98.     font-size: 1.0em;
  99.     color: #484848;
  100.     margin: 5px 40px 5px 0;
  101.     line-height: 1.5em;
  102. }
  103. .textarea {
  104.     width: 360px;
  105.     font-family: Arial, Verdana, Tahoma, Geneva, sans-serif;
  106.     font-size: 0.9em;
  107.     background-color: #ccc;
  108.     color: #484848;
  109.     border: 1px solid #383838;
  110.     padding: 4px 3px;  
  111. }
  112. #grafico {
  113.     width: 200px;
  114.     height: 200px;
  115.     background-image:url('../image/noentiendo.png');
  116.     border-bottom: 2px solid #484848;
  117.     margin: 0 auto;
  118. }
  119. #cab {
  120.     width: 100%;
  121.     text-align: center;
  122.     margin-top: 20px;
  123. }

El problema es que no centra la ventana MODAL en mitad de la pantalla, sino que cuenta al parecer los pixels de la ventana del navegador y lo ajusta a ese tamaño. Esto es un problema porque si el tamaño real de la página es mas largo, cuando se abre la ventana queda arriba del todo.

¿Alguna idea?

Muchas gracias de antemano!