Latest web development tutorials

jQuery UI API - .switchClass ()

category

Special effects (Effects) | Effects Core (Core Effects) | Method overloading (Method Overrides)

usage

Description: When the animation style changes within the set of matched elements to add and remove each element of the specified Class.

Returns: the jQuery

.switchClass( removeClassName, addClassName [, duration ] [, easing ] [, complete ] )

参数 类型 描述 默认值
removeClassName String 要从每个匹配的元素的 class 属性中移除的一个或多个 class 名称,多个 class 名称用空格分隔 。
addClassName String 要添加到每个匹配的元素的 class 属性中的一个或多个 class 名称,多个 class 名称用空格分隔。
duration Number 或 String 一个字符串或一个数字,指定动画将运行多久。 400
easing String 一个字符串,指示要使用的 easing 函数。 swing
complete Function() 一旦动画完成时要调用的函数。

.switchClass( removeClassName, addClassName [, options ] )

参数 类型 描述
removeClassName String 要从每个匹配的元素的 class 属性中移除的一个或多个 class 名称,多个 class 名称用空格分隔 。
addClassName String 要添加到每个匹配的元素的 class 属性中的一个或多个 class 名称,多个 class 名称用空格分隔。
options Object 所有的动画设置。所有的属性是可选的。
  • duration (默认值: 400
    类型:Number 或 String
    描述:一个字符串或一个数字,指定动画将运行多久。
  • easing (默认值: swing
    类型:String
    描述:一个字符串,指示要使用的 easing 函数。
  • complete
    类型:Function()
    描述:一旦动画完成时要调用的函数。
  • children (默认值: false
    类型:Boolean
    描述:指定动画是否被应用到匹配元素的所有后代。此功能应慎重使用,因为判断元素是否是动画的后代的代价是很大的,会根据后代的数量线性增长。
  • queue (默认值: true
    类型:Boolean 或 String
    描述:一个布尔值,指示是否将动画放在特效队列中。如果是 false,动画将立即开始。 自 jQuery 1.7 起 ,queue 选项也接受一个字符串,在这种情况下,动画添加到由字符串表示的队列中。

.switchClass() method allows you to animated transitions, add Class while removing Class.

Transition similar to the native CSS, jQuery UI's animation class provides a smooth transition from one state to another state of transition, while ensuring that all the details of the style change is done via CSS without using JavaScript. All class animation, comprising .switchClass() , allows custom duration of the animation and easing function, the animation is complete also provides a callback.

Not all styles can be animated to add. For example, the background image animated. Not any animation style will change when the movie ends.

The plug-in extensions from jQuery built .switchClass () method. If jQuery UI is not loaded, call .switchClass() method does not simply fail, because the method exists in jQuery. But it is not expected behavior.

Examples

Add the class "blue" to match the elements and remove the class "big" from the matched elements.

<! Doctype html>
<Html lang = "en">
<Head>
  <Meta charset = "utf-8">
  <Title> .switchClass () 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 {
    width: 200px;
    height: 200px;
  }
  .blue {
    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 class = "big"> </ div>
 
<Script>
$ ( "Div") .click (function () {
  $ (This) .switchClass ( "big", "blue", 1000, "easeInOutQuad");
});
</ Script>
 
</ Body>
</ Html>