Foros del Web » Programando para Internet » Jquery »

[jQuery] Seleccionar capa padre para cerrar ventana modal

Estas en el tema de [jQuery] Seleccionar capa padre para cerrar ventana modal en el foro de Jquery en Foros del Web. Hola, tengo una ventana modal hecha con CSS y me gustaría que se cerrase al hacer click en el fondo. Como la capa está anidada ...
  #1 (permalink)  
Antiguo 08/02/2012, 10:12
Avatar de uikekarallo  
Fecha de Ingreso: diciembre-2009
Ubicación: Galicia
Mensajes: 338
Antigüedad: 14 años, 3 meses
Puntos: 16
[jQuery] Seleccionar capa padre para cerrar ventana modal

Hola, tengo una ventana modal hecha con CSS y me gustaría que se cerrase al hacer click en el fondo. Como la capa está anidada dentro del fondo, no me funciona.

Para explicarme mejor, el código:

Código HTML:
Ver original
  1. <div id="fondoTable">
  2.         <div id="fondoTableCell">
  3.             <div class="thick">
  4.  
  5.                 <div class="cerrar"><p>cerrar (esc)</p></div>
  6.  
  7.                 <div class="thickImg"></div>
  8.  
  9.                 <div class="sep"></div>
  10.  
  11.                 <div class="galeria">
  12.                
  13.                     <img src="img/1.gif" alt="foto 1" />
  14.                     <img src="img/2.jpg" alt="foto 2" />
  15.                     <img src="img/3.jpg" alt="foto 3" />
  16.                     <img src="img/4.jpg" alt="foto 4" />
  17.                     <img src="img/8.jpg" alt="foto 5" />
  18.                     <img src="img/6.jpg" alt="foto 5" />
  19.                     <img src="img/5.jpg" alt="foto 5" />
  20.                     <img src="img/7.jpg" alt="foto 5" />
  21.  
  22.                 </div>
  23.  
  24.                 <div class="comentario"></div>
  25.                
  26.                 <div class="clear"></div>
  27.             </div>
  28.         </div>
  29.     </div>
  30.  
  31.     <div><img class="abreThick" src="img/8.jpg" alt="" width="50" height="50" /></div>

Código Javascript:
Ver original
  1. $(document).ready(function () {
  2.  
  3.     $('.abreThick').click(function () {
  4.  
  5.         img = new Image();
  6.         img.src = $(this).attr('src');
  7.         img.alt = $(this).attr('alt');
  8.  
  9.         if (img.width > 1024) { width = 800; } else { width = img.width; }
  10.         if (img.height > 768) { height = 600; } else { height = img.height; }
  11.  
  12.         $('<img alt="' + img.alt + '" src="' + img.src + '" width="' + width + '" height="' + height + '" />').appendTo('.thickImg');
  13.  
  14.         $('#fondoTableCell .thick .comentario').text(img.alt);
  15.  
  16.         delete img;
  17.  
  18.         $('#fondoTableCell .thick .galeria img').click(function () {
  19.  
  20.             img = new Image();
  21.             img.src = $(this).attr('src');
  22.             img.alt = $(this).attr('alt');
  23.  
  24.             if (img.width > 1024) { width = 800; } else { width = img.width; }
  25.             if (img.height > 768) { height = 600; } else { height = img.height; }
  26.  
  27.             $('.thickImg img').remove();
  28.  
  29.             $('<img alt="' + img.alt + '" src="' + img.src + '" width="' + width + '" height="' + height + '" />').fadeIn(500).appendTo('.thickImg');
  30.  
  31.             $('#fondoTableCell .thick .comentario').empty().append('<p>' + img.alt + '</p>');
  32.  
  33.             delete img;
  34.  
  35.         });
  36.  
  37.         $('#fondoTableCell .thick .cerrar').click(function () {
  38.  
  39.             $('#fondoTable').fadeOut(500, function () { $(this).css('display', 'none'); $('.thickImg img').remove(); });
  40.  
  41.         });
  42.  
  43.         $(document).keyup(function (e) {
  44.  
  45.             if (e.keyCode == 27) { $('#fondoTable').fadeOut(500, function () { $(this).css('dispaly', 'none'); $('.thickImg img').remove(); }); }
  46.  
  47.         });
  48.  
  49.  
  50.         /* aquí es donde no encuentro la manera
  51.  
  52.             $('.thick:not(:parent)').click(function () {
  53.  
  54.             $('#fondoTable').fadeOut(500, function () { $(this).css('dispaly', 'none'); $('.thickImg img').remove(); });
  55.  
  56.         });
  57. */
  58.  
  59.         $('#fondoTable').fadeIn(500).css('display', 'table'); // muestra la capa
  60.  
  61.     });
  62.  
  63. });


Código CSS:
Ver original
  1. #fondoTableCell
  2.     {
  3.         position: relative;
  4.         display: table-cell;
  5.         width: 100%;
  6.         height: 100%;
  7.         vertical-align: middle;
  8.         text-align: center;
  9.     }
  10.    
  11.         #fondoTableCell .thick      /* capa principal */
  12.         {
  13.             position: relative;
  14.             width: 800px;
  15.             margin: 0 auto;
  16.             padding: 62px;
  17.             border-radius: 15px;
  18.             background-color: white;
  19.         }
  20.        
  21.             #fondoTableCell .thick .cerrar      /* botón cerrar */
  22.             {
  23.                 cursor: pointer;
  24.                 position: absolute;
  25.                 display: block;
  26.                 top: -10px;
  27.                 right: 62px;
  28.                 width: 142px;
  29.                 height: 44px;
  30.                 border-radius: 2px 2px 15px 15px;
  31.                 background-color: #666666;
  32.             }
  33.            
  34.                 #fondoTableCell .thick .cerrar p    /* texto botón cerrar */
  35.                 {
  36.                     font-size: 16px;
  37.                     color: White;
  38.                     text-align: center;
  39.                     line-height: 44px;
  40.                 }
  41.                
  42.             #fondoTableCell .thick .thickImg    /* capa de la imagen principal */
  43.             {
  44.                 margin: 0;
  45.                 padding: 0;
  46.             }
  47.            
  48.             #fondoTableCell .thick .sep     /* separador */
  49.             {
  50.                 height: 1px;
  51.                 margin: 10px 0;
  52.                 background-color: #CCC;
  53.             }
  54.            
  55.             #fondoTableCell .thick .galeria     /* capa de la galería */
  56.             {
  57.                 width: 60%;
  58.                 float: left;
  59.                 text-align: left;
  60.             }
  61.            
  62.                 #fondoTableCell .thick .galeria img     /* imágenes de la galería */
  63.                 {
  64.                     cursor: pointer;
  65.                     width: 60px;
  66.                     height: 50px;
  67.                     margin-right: 8px;
  68.                 }
  69.            
  70.             #fondoTableCell .thick .comentario      /* capa del comentario o pie de foto */
  71.             {
  72.                 width: 40%;
  73.                 height: 50px;
  74.                 float: right;
  75.             }
  76.            
  77.                 #fondoTableCell .thick .comentario p        /* texto comentario o pie de foto */
  78.                 {
  79.                     line-height: 50px;
  80.                     font-size: 11px;
  81.                     text-align: right;
  82.                 }


Lo ideal sería solucionarlo sin modificar las capas, ya que está así para lograr un centrado perfecto sin utilizar javascript.

Gracias de antemano.

Etiquetas: cerrar, funcion, javascript, modal, padre, ventanas, capas
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:45.