How To Work With jQuery UI Effects

How to work with JQuery UI Effects

In this post I have explained how to work with jQuery effect using jQuery UI plugin. You can see all the tricks that we can do to manipulate HTML using jQuery UI effects. You can refer my article on jQuery UI interactions and UI Widgets to get more understanding.

Effects

  • Color animation
  • Toggle Class, Add Class, Remove Class, Switch Class
  • Show, Hide
  • Easing
  • Effects

Color animation:

JQuery Color plugins are used to provides color animation functions for working with colors such as backgroundColor , borderLeftColor, borderRightColor, borderTopColor, borderBottomColor, Color, OutLineColor, etc.

Syntax 

  1. $("#selector").animate({  
  2.     backgroundColor: "black",  
  3.     color: "Red"  
  4. });   

Example: In this example we are showing the color animation effects through a click of button. You can use the following code.

  1. $(function() {  
  2.     var state = true;  
  3.     $("#button").click(function() {  
  4.         if (state) {  
  5.             $("#effect").animate({  
  6.                 backgroundColor: "green",  
  7.                 color: "#fff",  
  8.                 width: 500  
  9.             }, 1000);  
  10.         } else {  
  11.             $("#effect").animate({  
  12.                 backgroundColor: "green",  
  13.                 color: "#000",  
  14.                 width: 240  
  15.             }, 1000);  
  16.         }  
  17.         state = !state;  
  18.     });  
  19. }); 

Output:


Figure 1:
Before animation effects


  Figure 2:
After animation effects

Add, Remove, Switch, Toggle class

Add class will be used for adding a new CSS class after replacing the old class and Remove class will work for removing the selected class. Also, the switch class work for switching one class to another class. All the classes add, remove, switch, and toggle are shown below:

JQuery Code: 

  1. $(document).ready(function() {  
  2.     $('.button').click(function() {  
  3.         if (this.id == "add") {  
  4.             $('#animTarget').addClass("myClass""fast")  
  5.         } else if (this.id == "toggle") {  
  6.             $('#animTarget').toggleClass("myClass", 1000, "easeOutSine")  
  7.         } else if (this.id == "switch") {  
  8.             $("#animTarget").switchClass("myClass""switchclass""fast")  
  9.         } else {  
  10.             $('#animTarget').removeClass("myClass""fast")  
  11.         }  
  12.     })  
  13. });  
CSS code:

  1. .elemClass {  
  2.   width200px;  
  3.   height50px;  
  4.   background-color#b9cd6d;  
  5.   text-aligncenter;  
  6.   font-sizelarge;  
  7. }  
  8.   
  9. .myClass {  
  10.   font-size70px;  
  11.   background-color#ccc;  
  12.   colorwhite;  
  13.   height150px;  
  14.   width170px;  
  15.   text-aligncenter;  
  16. }  
  17.   
  18. .switchclass {  
  19.   font-size50px;  
  20.   background-color#b9cd6d;  
  21.   colorblue;  
  22.   height150px;  
  23.   width150px;  
  24.   text-aligncenter;  
  25. } 

Html Code:

  1. <div id="animTarget" class="elemClass">  
  2. Hello Mohit!  
  3. </div>  
  4. <button class="button" id="add">Add Class</button>  
  5. <button class="button" id="remove">Remove Class</button>  
  6. <button class="button" id="toggle">Toggle Class</button>  
  7. <button class="button" id="switch">Switch Class</button>   

Output:


Figure 3:
Before addClass effects


 Figure 4:
After addClass effects

 
Figure 5: After switchClass effects.

Easing

JQuery easing can make your animations look more attractive, but sometimes it's hard to choose which easing type to use. The jQuery easing plugin provides 30 different easing methods. Here are some easing types and their use.

Code:

  1. $(document).ready(function() {  
  2.     $('#easing').click(function(event) {  
  3.         $(this)  
  4.             .animate({  
  5.             left: 400  
  6.         }, {  
  7.             duration: 'slow',  
  8.             easing: 'easeOutElastic'  
  9.         })  
  10.             .animate({  
  11.             left: 500  
  12.         }, {  
  13.             duration: 'slow',  
  14.             easing: 'easeOutElastic'  
  15.         });  
  16.     });  
  17. });   

Hide: This is the most important method which is used for hiding and showing the elements. You can use this method like the following syntax: 

  1. .hide(effect[, options][, duration][, complete])  
  2.   
  3. .show(effect[, options][, duration][, complete])   

Summary

In this article we saw how to use jQuery UI methods and their effects.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now