English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery effect toggle() method

jQuery Effect Methods

The toggle() method inhide()andshow()Switch between methods.

This method checks the visibility of the selected element:

  • If the element was initially displayed, it will be hidden

  • If the element was initially hidden, it will be displayed

Note:Hidden elements will no longer affect the page layout.

Syntax:

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

Example

Toggle between hiding and showing all <p> elements when the button is clicked:

$("button").click(function(){
  $("p").toggle();
});
Test to see‹/›

UsedurationParameters:

$("button").click(function(){
  $("p").toggle(1500);
});
Test to see‹/›

UsecallbackParameters:

$("button").click(function(){
  $("div").toggle(1500, function(){
    alert("toggle() effect completed!!!");
  });
});
Test to see‹/›

Create animations for all spans (for example, words) to quickly hide/Display, and in2Complete each animation within 00 milliseconds:

$("button").click(function(){
  $("span:first-child").toggle("fast", function(){
    $(this).next().toggle("fast", arguments.callee);
  });
});
Test to see‹/›

Parameter Value

ParameterDescription
durationAn optional value to determine the hiding/How long the display effect will run. The preset value is400 milliseconds

Possible values:

  • milliseconds (e.g.,100,500,2(e.g., 1000, 2000, etc.)

  • "fast"

  • "slow"

easingAn optional string specifying the speed of the element at different points in the animation. The default value is "swing"

Possible values:

  • "swing"-At the beginning/Move slower at the end and faster in the middle

  • "linear"-Move at a constant speed

callbackAn optional function called after the toggle() method completes, called once for each selected element

jQuery Effect Methods