JqueryUI Toggle Class

JqueryUI Toggle Class

This chapter will discuss the toggleClass()method, which is a useful new class for manipulation. toggleClass() method adds or removes one or more classes from each element in the set of matched elements.

Syntax

Added In Version 1.0 of jQueryUI

The toggleClass() method has its basic syntax as follows:

.toggleClass( className [, switch ] [, duration ] [, easing ] [, complete ] )

 

Parameter

Description

className

This is a String and represents the CSS class name, or space-delimited list of class names, to be added, removed, or toggled.

switch

This is of type Boolean and if specified, forces the toggleClass()method to add the class if true, or to remove the class if false.

duration

This is of type Number or String and optionally provides one ofslow, normal, fast, or the duration of the effect in milliseconds. If omitted, the animate() method determines the default. Its default value is 400.

easing

The name of the easing function to be passed to the animate() method.

complete

This is a callback method called for each element when the effect is complete for this element.

Added In Version 1.9 of jQueryUI

With version 1.9, this method now supports achildren option, which will also animate descendant elements.

.toggleClass( className [, switch ] [, options ] )

 

Parameter

Description

className

This is a String and represents the CSS class name, or space-delimited list of class names, to be added, removed, or toggled.

switch

This is of type Boolean and if specified, forces the toggleClass()method to add the class if true, or to remove the class if false.

options

This represents all animation settings. All properties are optional. Possible values are:

durationA string or number determining how long the animation will run.. Its default value is 400.

easingA string indicating which easing function to use for the transition. Its default value is swing. Possible values are here.

completeThis is a callback method called for each element when the effect is complete for this element.

childrenThis is a Boolean and represents whether the animation should additionally be applied to all descendants of the matched elements.

queueThis is of type String/Boolean indicating whether to place the animation in the effects queue.

Examples

The following example demonstrates the use oftoggleClass() method.

<!doctype html>

<html lang="en">

   <head>

      <meta charset="utf-8">

      <title>jQuery UI Switch Class Example</title>

      <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"rel="stylesheet">

      <scriptsrc="http://code.jquery.com/jquery-1.10.2.js"></script>

      <scriptsrc="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

      <!-- CSS -->

      <style>

         .class1 {

            border-width : 10px;

            border-color : grey;

            background-color :#cedc98;

            color : black;

         }

      </style>

      <script>

         function toggle ()

         {

            $("#para").toggleClass("class1", 1000);

         }

      </script>

   </head>

   <body>

      <button onclick=toggle()> Toggle </button>

      <p id="para" style=border-style:solid> Welcome to Tutorials Point </p>

   </body>

</html>

Let's save above code in an HTML filetoggleclassexample.htm and open it in a standard browser which supports javascript, you must see the following output. Now you can play with the result:

 

 

Welcome to Tutorials Point

Click on the Toggle button to see how the CSS classes are changeed for the text.

 

ليست هناك تعليقات:

إرسال تعليق