Equivalente a jQuery.hide () para establecer la visibilidad: hidden


En jQuery, hay métodos .hide() y .show() que establecen la configuración CSS display: none.

¿Hay una función equivalente que establezca el ajuste visibility: hidden?

Sé que puedo usar .css() pero prefiero alguna función como .hide() más o menos. Gracias.

Author: TMS, 2012-03-08

5 answers

Usted podría hacer sus propios plugins.

jQuery.fn.visible = function() {
    return this.css('visibility', 'visible');
};

jQuery.fn.invisible = function() {
    return this.css('visibility', 'hidden');
};

jQuery.fn.visibilityToggle = function() {
    return this.css('visibility', function(i, visibility) {
        return (visibility == 'visible') ? 'hidden' : 'visible';
    });
};

Si desea sobrecargar el jQuery original toggle(), que no recomiendo...

!(function($) {
    var toggle = $.fn.toggle;
    $.fn.toggle = function() {
        var args = $.makeArray(arguments),
            lastArg = args.pop();

        if (lastArg == 'visibility') {
            return this.visibilityToggle();
        }

        return toggle.apply(this, arguments);
    };
})(jQuery);

JsFiddle .

 382
Author: alex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-18 11:15:21

No hay uno incorporado, pero podrías escribir el tuyo con bastante facilidad:

(function($) {
    $.fn.invisible = function() {
        return this.each(function() {
            $(this).css("visibility", "hidden");
        });
    };
    $.fn.visible = function() {
        return this.each(function() {
            $(this).css("visibility", "visible");
        });
    };
}(jQuery));

Entonces puedes llamar así a esto:

$("#someElem").invisible();
$("#someOther").visible();

Aquí hay un ejemplo de trabajo.

 94
Author: James Allardice,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-08 08:22:20

Una forma aún más sencilla de hacer esto es usar el método toggleClass() de jQuery

CSS

.newClass{visibility: hidden}

HTML

<a href="#" class=trigger>Trigger Element </a>
<div class="hidden_element">Some Content</div>

JS

$(document).ready(function(){
    $(".trigger").click(function(){
        $(".hidden_element").toggleClass("newClass");
    });
});
 48
Author: Chaya Cooper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-20 13:47:11

Si solo necesita la funcionalidad estándar de hide only with visibility:hidden para mantener el diseño actual, puede usar la función callback de hide para alterar el css en la etiqueta. Ocultar documentos en jquery

Un ejemplo :

$('#subs_selection_box').fadeOut('slow', function() {
      $(this).css({"visibility":"hidden"});
      $(this).css({"display":"block"});
});

Esto usará la animación normal para ocultar el div, pero después de que la animación termine, configurará la visibilidad en oculto y la pantalla en bloque.

Un ejemplo : http://jsfiddle.net/bTkKG/1/

Sé que no querías the aa ("#aa").solución css (), pero no especificaste si era porque usando solo el método css () perdías la animación.

 21
Author: h3ct0r,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-27 20:24:04

Pure JS equivalente para jQuery hide () / show ():

function hide(el) {
    el.style.visibility = 'hidden';    
    return el;
}

function show(el) {
    el.style.visibility = 'visible';    
    return el;
}

hide(document.querySelector(".test"));
// hide($('.test')[0])   // usage with jQuery

Usamos return el debido a satisfacer interfaz fluida "patrón de diseño".

Aquí está ejemplo de trabajo.


A continuación también proporciono ALTAMENTE no recomendado alternativa, que sin embargo es probablemente más" cerca de la pregunta " respuesta:

HTMLElement.prototype.hide = function() {
    this.style.visibility = 'hidden';  
    return this;
}

HTMLElement.prototype.show = function() {
    this.style.visibility = 'visible';  
    return this;
}

document.querySelector(".test1").hide();
// $('.test1')[0].hide();   // usage with jQuery

Por supuesto esto no implementa jQuery 'each' (dado en @JamesAllardice respuesta) porque usamos js puro aquí.

El ejemplo de trabajo es aquí.

 0
Author: Kamil Kiełczewski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-30 09:48:54