🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# .toggleClass() 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") | [Method Overrides](http://www.css88.com/jquery-ui-api/category/overrides/ "View all posts in Method Overrides") ## .toggleClass( className [, switch ] [, duration ] [, easing ] [, complete ] )Returns: [jQuery](http://api.jquery.com/Types/#jQuery) **Description:** 为每一组匹配的元素添加或移除一个或多个样式类,取决于当前元素是否存在该样式类和switch参数。,而且所有改变的样式以动画的形式展示 * #### [.toggleClass( className [, switch ] [, duration ] [, easing ] [, complete ] )](#toggleClass-className-switch-duration-easing-complete) * **className**Type: [String](http://api.jquery.com/Types/#String)将要被添加到匹配的元素的class属性的一个或多个名称(以空格分开) 。 * **switch**Type: [Boolean](http://api.jquery.com/Types/#Boolean)一个决定是否添加或移除某css类的布尔值。 * **duration** (default: `400`)Type: [Number](http://api.jquery.com/Types/#Number) or [String](http://api.jquery.com/Types/#String)一个字符串或者数字决定动画将运行多久。(愚人码头注:三种预定速度的字符串("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)()在动画完成时执行的函数。 * #### [.toggleClass( className [, switch ] [, options ] )](#toggleClass-className-switch-options) * **className**Type: [String](http://api.jquery.com/Types/#String)将要被添加到匹配的元素的class属性的一个或多个名称(以空格分开) 。 * **switch**Type: [Boolean](http://api.jquery.com/Types/#Boolean)一个决定是否添加或移除某css类的布尔值。 * **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开始**,队列选项也可以接受一个字符串,在这种情况下, 在动画被添加到由该字符串表示的队列中。 类似原生CSS transitions(过渡), jQuery UI的样式动画提供了一个从一个状态到另一个状态平滑过渡, 同时让你 保持那些CSS样式改版的所有细节,和你的javaScript分离。 所有样式动画的方法,包括 `.toggleClass()` 支持自定义的持续时间(durations)和缓冲函数(easing),以及在动画完成时提供一个回调。 并非所有的样式都可以设置动画。 例如,没有办法让背景图像应用动画。不能进行动画的样式,将在动画结束时被改变。 这个插件扩展了jQuery中的[`.toggleClass()`](http://api.jquery.com/toggleClass)方法。 如果没有加载 jQuery UI,调用`.toggleClass()`方法可能不会失败,因为方法仍然存在。 然而,预期的行为(愚人码头注:指平滑过渡动画效果)将不会发生。 ## Example: #### 把类"big-blue"添加到匹配的元素 ``` <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>toggleClass 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-blue { width: 200px; height: 200px; 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></div> <script> $( "div" ).click(function() { $( this ).toggleClass( "big-blue", 1000, "easeOutSine" ); }); </script> </body> </html> ```