# .switchClass()
Categories: [Effects](http://www.css88.com/jquery-ui-api/category/effects/ "View all posts in Effects") | [Effects Core](http://www.css88.com/jquery-ui-api/category/effects-core/ "View all posts in Effects Core")
## .switchClass( removeClassName, addClassName [, duration ] [, easing ] [, complete ] )Returns: [jQuery](http://api.jquery.com/Types/#jQuery)
**Description:** 為每一組匹配的元素添加或移除指定的樣式類,而且所有改變的樣式以動畫的形式展示
* #### [.switchClass( removeClassName, addClassName [, duration ] [, easing ] [, complete ] )](#switchClass-removeClassName-addClassName-duration-easing-complete)
* **removeClassName**Type: [String](http://api.jquery.com/Types/#String)將要被從匹配的元素移除的class屬性的一個或多個名稱(以空格分開) 。
* **addClassName**Type: [String](http://api.jquery.com/Types/#String)將要被添加到匹配的元素的class屬性的一個或多個名稱(以空格分開) 。
* **duration** (default: `400`)Type: [Number](http://api.jquery.com/Types/#Number) or [String](http://api.jquery.com/Types/#String)一個字符串或者數字決定動畫將運行多久。(譯者注:默認值: "normal", 三種預定速度的字符串("slow", "normal", 或 "fast")或表示動畫時長的毫秒數值(如:1000) )
* **easing** (default: `swing`)Type: [String](http://api.jquery.com/Types/#String)一個字符串,表示過渡使用哪種[緩動](/easings/) 函數。
* **complete**Type: [Function](http://api.jquery.com/Types/#Function)()在動畫完成時執行的函數。
* #### [.switchClass( removeClassName, addClassName [, options ] )](#switchClass-removeClassName-addClassName-options)
* **removeClassName**Type: [String](http://api.jquery.com/Types/#String)將要被從匹配的元素移除的class屬性的一個或多個名稱(以空格分開) 。
* **addClassName**Type: [String](http://api.jquery.com/Types/#String)將要被添加到匹配的元素的class屬性的一個或多個名稱(以空格分開) 。
* **options**Type: [Object](http://api.jquery.com/Types/#Object)一組包含動畫選項的值的集合。所有屬性是可選擇的。
* **duration** (default: `400`)Type: [Number](http://api.jquery.com/Types/#Number) or [String](http://api.jquery.com/Types/#String)一個字符串或者數字決定動畫將運行多久。(譯者注:默認值: "normal", 三種預定速度的字符串("slow", "normal", 或 "fast")或表示動畫時長的毫秒數值(如:1000) )
* **easing** (default: `swing`)Type: [String](http://api.jquery.com/Types/#String)一個字符串,表示過渡使用哪種[緩動](/easings/) 函數。
* **complete**Type: [Function](http://api.jquery.com/Types/#Function)()在動畫完成時執行的函數。
* **children** (default: `false`)Type: [Boolean](http://api.jquery.com/Types/#Boolean)動畫是否應用到所有匹配的元素的后代元素。應慎使用此功能。 因為要確定哪些后代元素要應用動畫可能會非常好性能,而且 后代元素的數量是線性增長的。
* **queue** (default: `true`)Type: [Boolean](http://api.jquery.com/Types/#Boolean) or [String](http://api.jquery.com/Types/#String)一個布爾值,指示是否將動畫放置在效果隊列中。如果為false時,將立即開始動畫。 **從jQuery1.7開始**,隊列選項也可以接受一個字符串,在這種情況下, 在動畫被添加到由該字符串表示的隊列中。
`.switchClass()`方法允許你在展現動畫的同時添加或移除樣式類。
類似原生CSS transitions(過渡), jQuery UI的樣式動畫提供了一個從一個狀態到另一個狀態平滑過渡, 同時讓你 保持那些CSS樣式改版的所有細節,和你的javaScript分離。 所有樣式動畫的方法,包括 `.switchClass()` 支持自定義的持續時間(durations)和緩沖函數(easing),以及在動畫完成時提供一個回調。
并非所有的樣式都可以設置動畫。 例如,沒有辦法讓背景圖像應用動畫。不能進行動畫的樣式,將在動畫結束時被改變。
這個插件擴展了jQuery中的[`.switchClass()`](http://api.jquery.com/switchClass)方法。 如果沒有加載 jQuery UI,調用`.switchClass()`方法可能不會失敗,因為方法仍然存在。 然而,預期的行為(愚人碼頭注:指平滑過渡動畫效果)將不會發生。
## Example:
#### 向匹配的元素添加樣式類"blue",并且移除樣式類"big"。
```
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>switchClass demo</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<style>
div {
width: 100px;
height: 100px;
background-color: #ccc;
}
.big {
width: 200px;
height: 200px;
}
.blue {
background-color: #00f;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</head>
<body>
<div class="big"></div>
<script>
$( "div" ).click(function() {
$( this ).switchClass( "big", "blue", 1000, "easeInOutQuad" );
});
</script>
</body>
</html>
```
- 索引
- Effects
- .addClass()
- Blind Effect
- Bounce Effect
- Clip Effect
- Color Animation
- Drop Effect
- Easings
- .effect()
- Explode Effect
- Fade Effect
- Fold Effect
- .hide()
- Highlight Effect
- Puff Effect
- Pulsate Effect
- .removeClass()
- Scale Effect
- Shake Effect
- .show()
- Size Effect
- Slide Effect
- .switchClass()
- .toggle()
- .toggleClass()
- Transfer Effect
- Effect Core
- .addClass()
- Color Animation
- .effect()
- .hide()
- .removeClass()
- .show()
- .switchClass()
- .toggle()
- .toggleClass()
- Interactions
- Draggable Widget
- Droppable Widget
- Mouse Interaction
- Resizable Widget
- Resizable Widget
- Selectable Widget
- Sortable Widget
- Method Overrides
- .addClass()
- .focus()
- .hide()
- .position()
- .removeClass()
- .show()
- .toggle()
- .toggleClass()
- Methods
- .disableSelection()
- .effect()
- .enableSelection()
- .focus()
- .hide()
- .position()
- .removeUniqueId()
- .scrollParent()
- .show()
- .toggle()
- .uniqueId()
- .zIndex()
- Selectors
- :data() Selector
- :focusable Selector
- :tabbable Selector
- Theming
- CSS 框架(CSS Framework)
- Icons
- Stacking Elements
- UI Core
- :data() Selector
- .disableSelection()
- .enableSelection()
- .focus()
- :focusable Selector
- .removeUniqueId()
- .scrollParent()
- :tabbable Selector
- .uniqueId()
- .zIndex()
- Utilities
- Easings
- Widget Factory
- Widget Plugin Bridge
- Mouse Interaction
- .position()
- Widgets
- Accordion Widget
- Autocomplete Widget
- Button Widget
- Datepicker Widget
- Dialog Widget
- Menu Widget
- Progressbar Widget
- Slider Widget
- Spinner Widget
- Tabs Widget
- Tooltip Widget