jQuery UI API - .toggleClass()
Category
Effects | Effects Core | Method Overrides
Usage
Description:When animating style changes, add or remove one or more classes to each element in the matched set, based on whether the class exists and the value of the switch parameter.
Returns:jQuery
| Parameters | Type | Description | Default value |
|---|---|---|---|
| ClassName | String | One or more class names to be toggled for each element in the matched set, separated by spaces. | |
| switch | Boolean | A Boolean value specifying whether the class should be added or removed. | |
| duration | Number or String | A string or number specifying how long the animation will run. | 400 |
| easing | String | A string indicating theeasingfunction. | swing |
| complete | Function() | Function to call once the animation is complete. |
| Parameters | Type | Description |
|---|---|---|
| ClassName | String | One or more class names to be toggled for each element in the matched set, separated by spaces. |
| switch | Boolean | A Boolean value specifying whether the class should be added or removed. |
| options | Object | All animation settings. All properties are optional.
|
Similar to native CSS transitions, jQuery UI's class animations provide a smooth transition from one state to another, while ensuring that all style change details are handled through CSS without requiring JavaScript. All class animation methods, including.toggleClass(), allow custom animation duration andeasingeasing function, and also provide a callback when the animation completes.
Not all styles can be animated. For example, background images cannot be animated. Any style that cannot be animated will change at the end of the animation.
This plugin extends jQuery's built-in.toggleClass()method. If jQuery UI is not loaded, calling.toggleClass()method will not fail directly, because the method exists in jQuery. However, the expected behavior will not occur.
Example
Toggle the class "big-blue" for the matched elements.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>.toggleClass() 演示</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<style>
div {
width: 100px;
height: 100px;
background-color: #ccc;
}
.big-blue {
width: 200px;
height: 200px;
background-color: #00f;
}
</style>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<div></div>
<script>
$( "div" ).click(function() {
$( this ).toggleClass( "big-blue", 1000, "easeOutSine" );
});
</script>
</body>
</html>
Other extensions