2012-05-01 16 views
16

¿Cuál es la mejor manera de mostrar un div cuando se hace clic en un botón y luego ocultarlo con un botón de cerrar?visibilidad: visible/oculto div

Mi código jQuery es la siguiente:

$(".imageIcon").click(function(){ 
$('.imageShowWrapper').css("visibility", 'visible'); 
}); 
$(".imageShowWrapper").click(function(){ 
$('.imageShowWrapper').css("visibility", 'hidden'); 
}); 

excepto el problema que estoy teniendo es que se cierra automáticamente y sin ningún tipo de clics. Carga todo bien, se muestra durante aproximadamente 1/2 segundo y luego se cierra. ¿Algunas ideas?

Respuesta

25

Puede utilizar los show y hide métodos:

$(".imageIcon").click(function() { 
    $('.imageShowWrapper').show(); 
}); 

$(".imageShowWrapper").click(function() { 
    $(this).hide(); 
}); 
+0

hey gracias por su respuesta, quiero que el div se oculte primero, necesitaría usar la misma función hide() o puedo simplemente usar la propiedad de visibilidad CSS –

+2

@Scott Robertson, de nada, puedes ocultar el div fuera del controlador de eventos, ocultar sets 'display: none' a un elemento. – undefined

+0

hmm tal vez no lo implementé correctamente Todavía estoy aprendiendo muchas de estas cosas. En mi archivo CSS coloqué 'display: none' en las propiedades de imageShowWrapper, pero ahora el Jquery no afectará al div –

0

Puede conseguir una transición más suave utilizando los métodos de fundido:

var imageIcon = $(".imageIcon"), 
    imageShowWrapper = $(".imageShowWrapper"); 

imageIcon.on("click", function(){ 
    imageShowWrapper.stop().fadeIn(); 
}); 

imageShowWrapper.on("click", function(){ 
    $(this).stop().fadeOut(); 
}); 

Demostración: http://jsbin.com/uhapom/edit#javascript,html,live

0
$(".imageIcon, .imageShowWrapper").click(function(){ 
    $('.imageShowWrapper').toggle(); 
}); 
2

Otra opción :

$(".imageIcon, .imageShowWrapper").click(function() { 
    $(".imageShowWrapper").toggle($(this).hasClass('imageIcon'));  
}); 

También puede utilizar fadeToggle y slideToggle

Cuestiones relacionadas