Método jQuery alternar()

❮ Métodos de Efeito jQuery

Exemplo

Alterne entre ocultar e mostrar para todos os elementos <p>:

$("button").click(function(){
  $("p").toggle();
});

Definição e uso

O método toggle() alterna entre hide() e show() para os elementos selecionados.

Este método verifica a visibilidade dos elementos selecionados. show() é executado se um elemento estiver oculto. hide() é executado se um elemento estiver visível - Isso cria um efeito de alternância.

Nota : Elementos ocultos não serão exibidos (não afetam mais o layout da página).

Dica: Este método também pode ser usado para alternar entre funções personalizadas.


Sintaxe

$(selector).toggle(speed,easing,callback)

Parameter Description
speed Optional. Specifies the speed of the hide/show effect

Possible values:

  • milliseconds
  • "slow"
  • "fast"
easing Optional. Specifies the speed of the element in different points of the animation. Default value is "swing"

Possible values:

  • "swing" - moves slower at the beginning/end, but faster in the middle
  • "linear" - moves in a constant speed
Tip: More easing functions are available in external plugins
callback Optional. A function to be executed after the toggle() method is completed

To learn more about callback, visit our jQuery Callback chapter


Experimente você mesmo - Exemplos


Como usar o parâmetro speed para especificar a velocidade do efeito ocultar/exibir.


Como usar o parâmetro callback ao alternar entre o efeito ocultar/exibir.


❮ Métodos de Efeito jQuery