Category: Effects

  • Callback Functions

    jQuery Callback Function is a function that will be executed only after the current effect gets completed. This tutorial will explain you what are jQuery Callback Functions and why to use them.

    Following is a simple syntax of any jQuery effect method:

    $(selector).effectName(speed, callback);

    If we go in a little more detail then a jQuery callback function will be written as follows:

    $(selector).effectName(speed,function(){<!--function body -->});

    Example without Callback Function

    First let’s take a jQuery program which does not make use of callback function so here alert message is being displayed even before the hide effect is getting completed.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("div").click(function(){$(this).hide(1000);alert("I'm hidden now");});});</script><style>
       div{ margin:10px;padding:12px; border:2px solid #666; width:60px; cursor:pointer}</style></head><body><p>Click on any of the squares to see the result:</p><div>Hide Me</div><div>Hide Me</div><div>Hide Me</div></body></html>

    jQuery Callback Functions

    jQuery callback functions are required due to asynchronous nature of Javascript (jQuery) code execution. jQuery effects may take sometime to complete, so there is a chance that the next lines of code may get executed while the effects are still being executed. To handle asynchronous execution of the code, jQuery allows to pass a callback in all the effect methods and the purpose of this callback function is to be executed only when the effect gets completed.

    Example

    Let’s re-write the above example once again and this time we make use of a callback function which is executed after the hide effect is completed:.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("div").click(function(){$(this).hide(1000,function(){alert("I'm hidden now");});});});</script><style>
       div{ margin:10px;padding:12px; border:2px solid #666; width:60px; cursor:pointer}</style></head><body><p>Click on any of the squares to see the result:</p><div>Hide Me</div><div>Hide Me</div><div>Hide Me</div></body></html>

    Callback with Animation

    jQuery animate() method also gives provision to make use of a callback functions.

    Example

    The following example makes use of a callback function which is executed after the animate effect is completed:.

    <html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#right").click(function(){$("div").animate({left:'250px'},1000,function(){alert("I have reached to the right");});});$("#left").click(function(){$("div").animate({left:'0px'},1000,function(){alert("I have reached to the left");});});});</script><style>
       button{width:100px;cursor:pointer}
       #box{position:relative;margin:3px;padding:12px;height:100px; width:180px;background-color:#9c9cff;}</style></head><body><p>Click on Left or Right button to see the result:</p><div id="box">This is Box</div><button id="right">Right Move</button><button id="left">Left Move</button></body></html>
  • Chaining

    Before we discuss about jQuery Chaining of Methods, Consider a situation when you want to perform the following actions on an HTML element:

    • 1 – Select a paragraph element.
    • 2 – Change the color of the paragraph.
    • 3 – Apply FadeOut efect on the paragraph.
    • 4 – Apply FadeIn effect on the paragraph.

    Following is the jQuery program to perform the above mentioned actions:

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p").css("color","#fb7c7c");$("p").fadeOut(1000);$("p").fadeIn(1000);});});</script><style>
       button{width:100px;cursor:pointer;}</style></head><body><p>Click the below button to see the result:</p><button>Click Me</button></body></html>

    jQuery Method Chaining

    jQuery method chaining allows us to call multiple jQuery methods using a single statement on the same element(s). This gives a better performance because while using chaining, we do not need to parse the whole page every time to find the element.

    To chain different methods, we simply need to append the method to the previous method. For example our above program can be written as below:

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("button").click(function(){$("p").css("color","#fb7c7c").fadeOut(1000).fadeIn(1000);});});</script><style>
       button{width:100px;cursor:pointer;}</style></head><body><p>Click the below button to see the result:</p><button>Click Me</button></body></html>

    Animation with Chaining

    We can take advantage of jQuery Method Chaining while writing jQuery animation programs. Following is a simple animation program written with the help of chaining:

    <html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("button").click(function(){$("div").animate({left:'250px'}).animate({top:'100px'}).animate({left:'0px'}).animate({top:'0px'});});});</script><style>
       button {width:125px;cursor:pointer}
       #box{position:relative;margin:3px;padding:10px;height:20px; width:100px;background-color:#9c9cff;}</style></head><body><p>Click on Start Animation button to see the result:</p><div id="box">This is Box</div><button>Start Animation</button></body></html>

  • Animation

    Let’s learn how to use jQuery animate() method to create custom animations on a web pages or any other jQuery (Javascript) Application.

    jQuery animate() Method

    The jQuery animate() method is used to create custom animations by changing the CSS numerical properties of a DOM element, for example, width, height, margin, padding, opacity, top, left, etc.

    Following is a simple syntax of animate() method:

    $(selector).animate({ properties },[speed, callback]);

    jQuery animate() method can not be used to animate non-numeric properties such as color or background-color etc. Though you can use jQuery plugin jQuery.Color to animate such properties.

    You can apply any jQuery selector to select any DOM element and then apply jQuery animate() method to animate it. Here is the description of all the parameters which gives you a complete control over the animation −

    • properties − A required parameter which defines the CSS properties to be animated and this is the only mandatory parameter of the call.
    • speed − An optional string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − An optional parameter which represents a function to be executed whenever the animation completes.

    Animation Pre-Requisite

    (a) – The animate() method does not make hidden elements visible as part of the effect. For example, given $(selector).hide().animate({height: “20px”}, 500), the animation will run, but the element will remain hidden.

    (b) – To manipulate the position of a DOM element as a part of the animation, first we need to set it’s position to relativefixed, or absolute because by default, all HTML elements have a static position, and they cannot be moved using animate() method.

    Example

    The following example shows how to use animate() method to move a <div> element to the right, until it has reached a left property of 250px. Next when we click left button, the same <div> element returns to it’s initial position.

    <!doctype html><html><head><title>The jQuery Example</title><script src ="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#right").click(function(){$("div").animate({left:'250px'});});$("#left").click(function(){$("div").animate({left:'0px'});});});</script><style>
       #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
       #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}</style></head><body><p>Click on Left or Right button to see the result:</p><div id="box"  style="background-color:#9c9cff;">This is Box</div><button id="right" style="background-color:#fb7c7c;">Right Move</button><button id="left" style="background-color:#93ff93;">Left Move</button></body></html>

    Animation with Custom Speed

    We can animate different CSS numerical properties (for example, width, height, or left) of a DOM element with different speed.

    Example

    Let’s re-write above example, where we will animate <div>’s right movement with a speed parameter of 1000 milliseconds, and left movement with a speed parameter of 5000 milliseconds.

    <!doctype html><html><head><title>The jQuery Example</title><script src ="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#right").click(function(){$("div").animate({left:'250px'},1000);});$("#left").click(function(){$("div").animate({left:'0px'},5000);});});</script><style>
       #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
       #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}</style></head><body><p>Click on Left or Right button to see the result:</p><div id="box"  style="background-color:#9c9cff;">This is Box</div><button id="right" style="background-color:#fb7c7c;">Right Move</button><button id="left" style="background-color:#93ff93;">Left Move</button></body></html>

    Animation with Pre-defined Values

    We can use strings ‘show’‘hide’, and ‘toggle’ as the value of CSS numeric properties.

    Example

    Following is an example where we are setting left property of an element to either hide or show with the help of two buttons.

    Please note setting ANY of the numeric CSS properties with these values will produce the same result. For example if you will set element’s width or height at hide then it will hide the element regardless you set it’s width property or height property.

    <!doctype html><html><head><title>The jQuery Example</title><script src ="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#right").click(function(){$("div").animate({left:'hide'});});$("#left").click(function(){$("div").animate({left:'show'});});});</script><style>
       #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
       #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}</style></head><body><p>Click on Left or Right button to see the result:</p><div id="box"  style="background-color:#9c9cff;">This is Box</div><button id="right" style="background-color:#fb7c7c;">Right Move</button><button id="left" style="background-color:#93ff93;">Left Move</button></body></html>

    Animation with Multiple Properties

    jQuery animate() allows us to animate multiple CSS properties of an element at the same time.

    Example

    Following is an example to animate multiple CSS properties of a <div> element. When we click on Right Move button, this <div> starts moving towards the right direction till it reaches a left property value to 250px, at the same time element’s opacity reduces to 0.2 and width & height of the box decreases to 100px. Next, when we click on the Left Move button, this box returns to its initial position and size.

    <!doctype html><html><head><title>The jQuery Example</title><script src ="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#right").click(function(){$("div").animate({left:'250px', width:'100px', height:'100px', opacity:0.2});});$("#left").click(function(){$("div").animate({left:'0px',width:'180px', height:'100px', opacity:1.0});});});</script><style>
       #left, #right{margin:3px;border:2px solid #666; height:30px; width:100px;cursor:pointer;}
       #box{position:relative;margin:3px;padding:12px;border:2px solid #666;  height:100px; width:180px;}</style></head><body><p>Click on Left or Right button to see the result:</p><div id="box"  style="background-color:#9c9cff;">This is Box</div><button id="right" style="background-color:#fb7c7c;">Right Move</button><button id="left" style="background-color:#93ff93;">Left Move</button></body></html>

    Animation with Queue Functionality

    Consider a situation where you need to apply multiple animations which means you need to call animate() method multiple times one after the other. In such situation, jQuery handles these animation requests through a fist-in-fist-out (FIFO) queue and allows to create interesting animations based on your creativity.

    Example

    Following is an example where we call animate() methods 4 times to take a <div> into multiple directions one by one.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("button").click(function(){$("div").animate({left:'250px'});$("div").animate({top:'100px'});$("div").animate({left:'0px'});$("div").animate({top:'0px'});});});</script><style>
       button {margin:3px;border:2px solid #666; height:30px; width:180px;cursor:pointer;}
       #box{position:relative;margin:3px;padding:2px;border:2px solid #666;  height:30px; width:170px;}</style></head><body><p>Click on Start Animation button to see the result:</p><div id="box"  style="background-color:#9c9cff;">This is Box</div><button style="background-color:#93ff93;">Start Animation</button></body></html>

    jQuery Effects Reference

    You can get a complete reference of all the jQuery Effect Methods at the following page: jQuery Effects Reference.

  • Effects

    jQuery effects add an X factor to your website interactivity. jQuery provides a trivially simple interface for doing various kind of amazing effects like show, hide, fade-in, fade-out, slide-up, slide-down, toggle etc. jQuery methods allow us to quickly apply commonly used effects with a minimum configuration. This tutorial covers all the important jQuery methods to create visual effects.

    jQuery Effect – Hiding Elements

    jQuery gives simple syntax to hide an element with the help of hide() method:

    $(selector).hide([speed, callback]);

    You can apply any jQuery selector to select any DOM element and then apply jQuery hide() method to hide it. Here is the description of all the parameters which gives you a solid control over the hiding effect −

    • speed − This optional parameter represents one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.

    The default speed duration ‘normal’ is 400 milliseconds. The strings ‘fast’ and ‘slow’ can be supplied to indicate durations of 200 and 600 milliseconds, respectively. Higher values indicate slower animations.

    Example

    Following is an example where a <div> will hide itself when we click over it. We have used 1000 as speed parameter which means it will take 1 second to apply the hide effect on the clicked element.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("div").click(function(){$(this).hide(1000);});});</script><style>
       div{ margin:10px;padding:12px; border:2px solid #666; width:60px; cursor:pointer}</style></head><body><p>Click on any of the squares to see the result:</p><div>Hide Me</div><div>Hide Me</div><div>Hide Me</div></body></html>

    jQuery Effect – Show Elements

    jQuery gives simple syntax to show a hidden element with the help of show() method:

    $(selector).show([speed, callback]);

    You can apply any jQuery selector to select any DOM element and then apply jQuery show() method to show it. Here is the description of all the parameters which gives you a control over the show effect −

    • speed − An optional string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.

    Example

    Following is an example where we will play with a Box with the help of two buttons. We will use these two buttons to show and hide this Box. We have used different speeds for the two effects hide(5000) and show(1000) to show the difference in effect speed.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#show").click(function(){$("#box").show(1000);});$("#hide").click(function(){$("#box").hide(5000);});});</script><style>
       button{cursor:pointer;}
       #box{margin-bottom:5px;padding:12px;height:100px; width:125px; background-color:#9c9cff;}</style></head><body><p>Click on Show and Hide buttons to see the result:</p><div id="box">This is Box</div><button id="hide">Hide Box</button><button id="show">Show Box</button></body></html>

    jQuery Effect – Toggle Elements

    jQuery provides toggle() methods to toggle the display state of elements between revealed or hidden. If the element is initially displayed, it will be hidden; if hidden, it will be shown.

    $(selector).toggle([speed, callback]);

    You can apply any jQuery selector to select any DOM element and then apply jQuery toggle() method to toggle it. Here is the description of all the parameters which gives you a solid control over the toggle effect −

    • speed − An optional string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.

    Example

    Following is an example where we will play with a Square Box with the help of a single Toggle button. When we click this button for the first time, square box becomes invisible, and next time when we click the button then square box becomes visible. We have used 1000 as speed parameter which means it will take 1 second to apply the toggle effect.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#button").click(function(){$("#box").toggle(1000);});});</script><style>
       button{margin:3px;width:125px;cursor:pointer;}
       #box{margin:3px;padding:12px; height:100px; width:100px;background-color:#9c9cff;}</style></head><body><p>Click on the Toggle Box button to see the box toggling:</p><div id="box">This is Box</div><button id="button">Toggle Box</button></body></html>

    jQuery Effect – Fading Elements

    jQuery gives us two methods – fadeIn() and fadeOut() to fade the DOM elements in and out of visibility.

    $(selector).fadeIn([speed, callback]);$(selector).fadeOut([speed, callback]);

    The jQuery fadeIn() method is used to fade in a hidden element where as fadeOut() method is used to fade out a visible element. Here is the description of all the parameters which gives you a control over the fading effects −

    • speed − An optional string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.

    Example

    Following is an example where we will play with a Box with the help of two buttons. We will use these two buttons to show and hide this Box. We have used 1000 as speed parameter which means it will take 1 second to apply the effect.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#show").click(function(){$("#box").fadeIn(1000);});$("#hide").click(function(){$("#box").fadeOut(1000);});});</script><style>
       button{cursor:pointer;}
       #box{margin-bottom:5px;padding:12px;height:100px; width:150px; background-color:#9c9cff;}</style></head><body><p>Click on fadeOut and fadeIn buttons to see the result:</p><div id="box">This is Box</div><button id="hide">fadeOut Box</button><button id="show">fadeIn Box</button></body></html>

    jQuery Effect – Toggle with Fading

    jQuery provides fadeToggle() methods to toggle the display state of elements between the fadeIn() and fadeOut() methods. If the element is initially displayed, it will be hidden (ie. fadeOut()); if hidden, it will be shown (ie. fadeIn()).

    $(selector).fadeToggle([speed, callback]);

    This method gives the same functionality what we can have using toggle() method, but additionally, it gives fade in and fade out effect while toggling the element.

    Here is the description of all the parameters which gives you more control over the effect −

    • speed − An optional string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.

    Example

    Following is an example where we will play with a Square Box with the help of a single button. When we click this button for the first time, square box fades out (hidden), and next time when we click the button then square box fades in (visible). We have used 1000 as speed parameter which means it will take 1 second to apply the toggle effect.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#button").click(function(){$("#box").fadeToggle(1000);});});</script><style>
       button{margin:3px;width:125px;cursor:pointer;}
       #box{margin:3px;padding:12px; height:100px; width:100px;background-color:#9c9cff;}</style></head><body><p>Click on the Toggle Box button to see the box toggling:</p><div id="box">This is Box</div><button id="button">Toggle Box</button></body></html>

    Try using jQuery $(selector).toggle() and $(selector).fadeToggle() methods to understand the minor difference between these two methods.

    jQuery Effect – Sliding Elements

    jQuery gives us two methods – slideUp() and slideDown() to slide up and slide down the DOM elements respectively. Following is the simple syntax for these two methods:

    $(selector).slideUp([speed, callback]);$(selector).slideDown( speed,[callback]);

    The jQuery slideUp() method is used to slide up an element where as slideDown() method is used to slide down. Here is the description of all the parameters which gives you more control over the effects −

    • speed − An optional string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.

    Example

    Following is an example where we will play with a Box with the help of two buttons. We will use these two buttons to show and hide this Box. We have used 1000 as speed parameter which means it will take 1 second to apply the toggle effect.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#show").click(function(){$("#box").slideDown(1000);});$("#hide").click(function(){$("#box").slideUp(1000);});});</script><style>
       button{cursor:pointer;}
       #box{margin-bottom:5px;padding:12px;height:100px; width:120px; background-color:#9c9cff;}</style></head><body><p>Click on slideUp and slideDown buttons to see the result:</p><div id="box">This is Box</div><button id="hide">slideUp </button><button id="show">slideDown </button></body></html>

    jQuery Effect – Toggle with Sliding

    jQuery provides slideToggle() methods to toggle the display state of elements between the slideUp() and slideDown() methods. If the element is initially displayed, it will be hidden (ie. slideUp()); if hidden, it will be shown (ie. slideDown()).

    $(selector).slideToggle([speed, callback]);

    This method gives the same functionality what we can have using toggle() method, but additionally, it gives slide up and slide down effect while toggling the element.

    Here is the description of all the parameters which gives you more control over the effect −

    • speed − An optional string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
    • callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.

    Example

    Following is an example where we will play with a Square Box with the help of a single button. When we click this button for the first time, square box fades out (hidden), and next time when we click the button then square box fades in (visible). We have used 1000 as speed parameter which means it will take 1 second to apply the toggle effect.

    <!doctype html><html><head><title>The jQuery Example</title><script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script><script>$(document).ready(function(){$("#button").click(function(){$("#box").slideToggle(1000);});});</script><style>
       button{margin:3px;width:125px;cursor:pointer;}
       #box{margin:3px;padding:12px; height:100px; width:100px;background-color:#9c9cff;}</style></head><body><p>Click on the Toggle Box button to see the box toggling:</p><div id="box">This is Box</div><button id="button">Toggle Box</button></body></html>

    Try using jQuery $(selector).toggle()$(selector).slideToggle() and $(selector).fadeToggle() methods to understand the minor difference among these three methods.

    jQuery Effects Reference

    This tutorial covered only a few most frequently used jQuery effects, You can get a complete reference of all the jQuery Effect Methods at the following page: jQuery Effects Reference.