jQuery provides a variety of effects to enhance the user experience and add dynamic functionality to web pages. These effects can be used to show/hide elements, create animations, and manipulate visibility.
1. Basic Effects
Method |
Description |
Syntax |
Example |
hide() |
Hides the selected elements. |
$(selector).hide(speed, callback) |
$("#div").hide(1000, function() { alert("Hidden!"); }); |
show() |
Shows the hidden elements. |
$(selector).show(speed, callback) |
$("#div").show(1000, function() { alert("Shown!"); }); |
toggle() |
Toggles the visibility of elements. |
$(selector).toggle(speed, callback) |
$("#div").toggle(1000, function() { alert("Toggled!"); }); |
2. Fading Effects
Method |
Description |
Syntax |
Example |
fadeIn() |
Fades in the selected elements. |
$(selector).fadeIn(speed, callback) |
$("#div").fadeIn(1000, function() { alert("Faded In!"); }); |
fadeOut() |
Fades out the selected elements. |
$(selector).fadeOut(speed, callback) |
$("#div").fadeOut(1000, function() { alert("Faded Out!"); }); |
fadeToggle() |
Toggles the fade effect (in/out). |
$(selector).fadeToggle(speed, callback) |
$("#div").fadeToggle(1000, function() { alert("Fade Toggled!"); }); |
fadeTo() |
Fades an element to a specified opacity. |
$(selector).fadeTo(speed, opacity, callback) |
$("#div").fadeTo(1000, 0.5, function() { alert("Faded to 50% opacity!"); }); |
3. Sliding Effects
Method |
Description |
Syntax |
Example |
slideDown() |
Slides down the selected elements. |
$(selector).slideDown(speed, callback) |
$("#div").slideDown(1000, function() { alert("Slid Down!"); }); |
slideUp() |
Slides up the selected elements. |
$(selector).slideUp(speed, callback) |
$("#div").slideUp(1000, function() { alert("Slid Up!"); }); |
slideToggle() |
Toggles the slide effect (up/down). |
$(selector).slideToggle(speed, callback) |
$("#div").slideToggle(1000, function() { alert("Slide Toggled!"); }); |
4. Custom Animations
Method |
Description |
Syntax |
Example |
animate() |
Creates custom animations. |
$(selector).animate(properties, speed, easing, callback) |
$("#div").animate({ height: "200px", opacity: 0.5 }, 1000, "swing", function() { alert("Animation Complete!"); }); |
5. Stopping and Clearing Effects
Method |
Description |
Syntax |
Example |
stop() |
Stops the currently running animation. |
$(selector).stop(clearQueue, jumpToEnd) |
$("#div").stop(true, true); |
finish() |
Completes all animations in the queue. |
$(selector).finish() |
$("#div").finish(); |
6. Delays and Chaining
Method |
Description |
Syntax |
Example |
delay() |
Adds a delay before the next queued animation. |
$(selector).delay(duration, queueName) |
$("#div").fadeOut().delay(1000).fadeIn(); |
7. Queue Management
Method |
Description |
Syntax |
Example |
queue() |
Manipulates the queue of animations. |
$(selector).queue(queueName, newQueue) |
$("#div").queue(function(next) { alert("Queue Step!"); next(); }); |
dequeue() |
Removes the next function from the queue. |
$(selector).dequeue(queueName) |
$("#div").dequeue(); |
8. Built-In Easing Functions
Easing functions determine the speed at which an animation progresses. The two default easing functions in jQuery are:
swing
(default): Starts slow, speeds up, and then slows down.
linear
: Moves at a constant speed.
To use more easing options, include the jQuery UI library.
Example: Using Multiple Effects
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Effects Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<style>
#box {
width: 100px;
height: 100px;
background-color: teal;
margin: 20px auto;
text-align: center;
line-height: 100px;
color: white;
font-size: 18px;
}
</style>
<script>
$(document).ready(function() {
// Basic Effects
$("#hide").click(function() {
$("#box").hide(1000);
});
$("#show").click(function() {
$("#box").show(1000);
});
// Fading Effects
$("#fadeOut").click(function() {
$("#box").fadeOut(1000);
});
$("#fadeIn").click(function() {
$("#box").fadeIn(1000);
});
// Sliding Effects
$("#slideUp").click(function() {
$("#box").slideUp(1000);
});
$("#slideDown").click(function() {
$("#box").slideDown(1000);
});
// Custom Animation
$("#animate").click(function() {
$("#box").animate({
width: "200px",
height: "200px",
opacity: 0.5
}, 1000, function() {
alert("Animation Complete!");
});
});
});
</script>
</head>
<body>
<div id="box">Box</div>
<button id="hide">Hide</button>
<button id="show">Show</button>
<button id="fadeOut">Fade Out</button>
<button id="fadeIn">Fade In</button>
<button id="slideUp">Slide Up</button>
<button id="slideDown">Slide Down</button>
<button id="animate">Animate</button>
</body>
</html>
Try It Now
Conclusion
jQuery effects make it simple to add engaging and interactive animations to web pages. You can use predefined methods like hide()
, fadeOut()
, and slideToggle()
, or create custom animations with animate()
. Combine these effects with event handling and chaining to create dynamic and user-friendly web applications.