jQuery UI API - .addClass()
Categories
Effects | Effects Core | Method Overrides
Usage
Description: Adds the specified class(es) to each element in the set of matched elements while animating all style changes.
Returns: jQuery
Parameter | Type | Description | Default Value |
---|---|---|---|
className | String | One or more class names to be added to the class attribute of each matched element, separated by spaces. | |
duration | Number or String | A string or number determining how long the animation will run. | 400 |
easing | String | A string indicating which easing function to use. | swing |
complete | Function() | A function to call once the animation is complete. |
Parameter | Type | Description |
---|---|---|
className | String | One or more class names to be added to the class attribute of each matched element, separated by spaces. |
options | Object | All animation settings. All properties are optional. duration (default: 400)<br> Type: Number or String<br> Description: A string or number determining how long the animation will run.<br> easing (default: swing)<br> Type: String<br> Description: A string indicating which easing function to use.<br> complete<br> Type: Function()<br> Description: A function to call once the animation is complete.<br> children (default: false)<br> Type: Boolean<br> Description: Specifies whether the animation should be applied to all descendants of the matched elements. This feature should be used with caution, as the cost of determining whether an element is an animated descendant is significant and grows linearly with the number of descendants.<br> queue (default: true)<br> Type: Boolean or String<br> Description: A boolean indicating whether to place the animation in the effects queue. If false, the animation will begin immediately. As of jQuery 1.7, the queue option also accepts a string, in which case the animation is added to the queue represented by that string. |
Similar to native CSS transitions, jQuery UI's class animations provide a smooth transition from one state to another while ensuring that all style changes are handled through CSS, not JavaScript. All class animation methods, including .addClass()
, allow customization of animation duration and easing functions, and provide a callback upon completion of the animation.
Not all styles can be animated. For example, animating background images. Any non-animatable styles will change at the end of the animation.
This plugin extends jQuery's built-in .addClass() method. If jQuery UI is not loaded, calling .addClass()
method will not fail directly since the method exists in jQuery. However, the expected behavior will not occur.
Example
Add the class "big-blue" to the matched elements.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>.addClass() Demo</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>
<script>
$(document).ready(function() {
$("#button").click(function() {
$("div").addClass("big-blue", 1000, "swing", function() {
alert("Animation complete.");
});
});
});
</script>
</head>
<body>
<button id="button">Run AddClass</button>
<div></div>
</body>
</html>
<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).addClass("big-blue", 1000, "easeOutBounce");
});
</script>
</body>
</html>