Skip to content

jQuery Animation and Effects

jQuery provides rich built-in animation effects that create smooth transitions and visual effects for web page elements. These animation features simplify complex JavaScript animation programming, enabling developers to easily create engaging user interfaces. This chapter covers jQuery's animation and effects functionality.

Basic Animation Methods

1. show() and hide() Methods

Basic methods for showing and hiding elements:

javascript
// Show element
$('#myElement').show();

// Hide element
$('#myElement').hide();

// Animated show and hide
$('#myElement').show(1000); // Show in 1000ms
$('#myElement').hide('slow'); // Hide slowly
$('#myElement').show('fast'); // Show quickly

// Animation with callback
$('#myElement').hide(1000, function() {
    console.log('Element hidden');
});

2. toggle() Method

Toggle element visibility:

javascript
// Toggle show/hide
$('#myElement').toggle();

// Animated toggle
$('#myElement').toggle(1000);

// Toggle with callback
$('#myElement').toggle('slow', function() {
    console.log('Toggle complete');
});

Slide Animations

1. slideDown() Method

Show element with sliding animation:

javascript
// Slide down to show
$('#myElement').slideDown();

// Slide down with duration
$('#myElement').slideDown(1000);

// Slide down with callback
$('#myElement').slideDown('slow', function() {
    console.log('Slide down complete');
});

2. slideUp() Method

Hide element with sliding animation:

javascript
// Slide up to hide
$('#myElement').slideUp();

// Slide up with duration
$('#myElement').slideUp(1000);

// Slide up with callback
$('#myElement').slideUp('fast', function() {
    console.log('Slide up complete');
});

3. slideToggle() Method

Toggle element with sliding animation:

javascript
// Toggle slide
$('#myElement').slideToggle();

// Toggle with duration
$('#myElement').slideToggle(1000);

// Toggle with callback
$('#myElement').slideToggle('slow', function() {
    console.log('Slide toggle complete');
});

Fade Animations

1. fadeIn() Method

Show element with fade-in animation:

javascript
// Fade in
$('#myElement').fadeIn();

// Fade in with duration
$('#myElement').fadeIn(1000);

// Fade in with callback
$('#myElement').fadeIn('slow', function() {
    console.log('Fade in complete');
});

2. fadeOut() Method

Hide element with fade-out animation:

javascript
// Fade out
$('#myElement').fadeOut();

// Fade out with duration
$('#myElement').fadeOut(1000);

// Fade out with callback
$('#myElement').fadeOut('fast', function() {
    console.log('Fade out complete');
});

3. fadeToggle() Method

Toggle element with fade animation:

javascript
// Toggle fade
$('#myElement').fadeToggle();

// Toggle with duration
$('#myElement').fadeToggle(1000);

// Toggle with callback
$('#myElement').fadeToggle('slow', function() {
    console.log('Fade toggle complete');
});

4. fadeTo() Method

Fade element to specified opacity:

javascript
// Fade to specified opacity
$('#myElement').fadeTo(1000, 0.5); // Fade to 50% opacity in 1000ms

// Fade with callback
$('#myElement').fadeTo('slow', 0.3, function() {
    console.log('Fade to complete');
});

Custom Animations

animate() Method

Create custom animation effects:

javascript
// Basic syntax
$(selector).animate({params}, speed, callback);

// Move element
$('#myElement').animate({
    left: '250px',
    top: '100px'
}, 1000);

// Change dimensions
$('#myElement').animate({
    width: '300px',
    height: '200px'
}, 'slow');

// Change multiple CSS properties
$('#myElement').animate({
    opacity: '0.5',
    left: '250px',
    height: '150px',
    width: '150px'
}, 2000);

// Use relative values
$('#myElement').animate({
    left: '+=150px',
    top: '+=50px'
}, 1000);

// Use predefined values
$('#myElement').animate({
    height: 'toggle'
}, 1000);

Queued Animations

jQuery animations automatically queue:

javascript
// Animation queue example
$('#myElement')
    .animate({ left: '250px' }, 1000)
    .animate({ top: '100px' }, 1000)
    .animate({ width: '200px' }, 1000)
    .animate({ height: '200px' }, 1000);

Animation Options

animate() method supports additional options:

javascript
// Using options object
$('#myElement').animate({
    width: '300px',
    height: '200px'
}, {
    duration: 2000,
    easing: 'swing', // or 'linear'
    complete: function() {
        console.log('Animation complete');
    },
    step: function(now, fx) {
        console.log('Animation step: ' + now);
    }
});

Animation Control

1. stop() Method

Stop current animation:

javascript
// Stop current animation
$('#myElement').stop();

// Stop and jump to end state
$('#myElement').stop(true);

// Stop all animations and jump to end state
$('#myElement').stop(true, true);

2. finish() Method

Stop all animations and jump to end state:

javascript
// Finish all animations
$('#myElement').finish();

3. delay() Method

Delay animation execution:

javascript
// Delay animation
$('#myElement')
    .delay(1000)
    .fadeIn(1000)
    .delay(2000)
    .fadeOut(1000);

Easing Functions

jQuery provides two built-in easing functions:

1. swing (Default)

Slower at start and end, faster in middle:

javascript
$('#myElement').animate({
    left: '250px'
}, 1000, 'swing');

2. linear

Constant speed animation:

javascript
$('#myElement').animate({
    left: '250px'
}, 1000, 'linear');

3. Custom Easing Functions

More easing functions available through plugins:

javascript
// Requires jQuery UI or other easing plugins
$('#myElement').animate({
    left: '250px'
}, 1000, 'easeInOutQuad');

Complete Example

Here's a complete example demonstrating jQuery animation and effects:

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery Animation and Effects Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        .container { border: 1px solid #ccc; padding: 20px; margin: 10px 0; }
        .animation-box {
            width: 100px; height: 100px;
            background-color: #3498db;
            margin: 20px 0; position: relative; cursor: pointer;
        }
        .control-panel {
            margin: 10px 0; padding: 10px;
            background-color: #f8f9fa; border-radius: 5px;
        }
        button {
            margin: 5px; padding: 8px 15px;
            background-color: #007bff; color: white;
            border: none; border-radius: 4px; cursor: pointer;
        }
        button:hover { background-color: #0056b3; }
        .demo-element {
            padding: 10px; margin: 10px 0;
            background-color: #e9ecef; border-radius: 4px;
        }
    </style>
</head>
<body>
    <h1>jQuery Animation and Effects Example</h1>
    
    <div class="container">
        <h2>Basic Show/Hide Animation</h2>
        <div class="demo-element" id="basicElement">Basic show/hide element</div>
        <div class="control-panel">
            <button id="showBasic">Show</button>
            <button id="hideBasic">Hide</button>
            <button id="toggleBasic">Toggle</button>
        </div>
    </div>
    
    <div class="container">
        <h2>Slide Animation</h2>
        <div class="demo-element" id="slideElement">Slide animation element</div>
        <div class="control-panel">
            <button id="slideDown">Slide Down</button>
            <button id="slideUp">Slide Up</button>
            <button id="slideToggle">Slide Toggle</button>
        </div>
    </div>
    
    <div class="container">
        <h2>Fade Animation</h2>
        <div class="demo-element" id="fadeElement">Fade animation element</div>
        <div class="control-panel">
            <button id="fadeIn">Fade In</button>
            <button id="fadeOut">Fade Out</button>
            <button id="fadeToggle">Fade Toggle</button>
            <button id="fadeTo">Fade to 50%</button>
        </div>
    </div>
    
    <div class="container">
        <h2>Custom Animation</h2>
        <div class="animation-box" id="customElement"></div>
        <div class="control-panel">
            <button id="moveElement">Move</button>
            <button id="resizeElement">Resize</button>
            <button id="complexAnimation">Complex</button>
            <button id="stopAnimation">Stop</button>
            <button id="resetElement">Reset</button>
        </div>
    </div>
    
    <script>
        $(function() {
            // Basic show/hide
            $('#showBasic').click(function() { $('#basicElement').show(1000); });
            $('#hideBasic').click(function() { $('#basicElement').hide(1000); });
            $('#toggleBasic').click(function() { $('#basicElement').toggle(1000); });
            
            // Slide animation
            $('#slideDown').click(function() { $('#slideElement').slideDown(1000); });
            $('#slideUp').click(function() { $('#slideElement').slideUp(1000); });
            $('#slideToggle').click(function() { $('#slideElement').slideToggle(1000); });
            
            // Fade animation
            $('#fadeIn').click(function() { $('#fadeElement').fadeIn(1000); });
            $('#fadeOut').click(function() { $('#fadeElement').fadeOut(1000); });
            $('#fadeToggle').click(function() { $('#fadeElement').fadeToggle(1000); });
            $('#fadeTo').click(function() { $('#fadeElement').fadeTo(1000, 0.5); });
            
            // Custom animation
            $('#moveElement').click(function() {
                $('#customElement').animate({ left: '+=100px', top: '+=50px' }, 1000);
            });
            
            $('#resizeElement').click(function() {
                $('#customElement').animate({ width: '+=50px', height: '+=50px' }, 1000);
            });
            
            $('#complexAnimation').click(function() {
                $('#customElement')
                    .animate({ left: '200px' }, 500)
                    .animate({ top: '100px' }, 500)
                    .animate({ width: '150px' }, 500)
                    .animate({ opacity: '0.5' }, 500);
            });
            
            $('#stopAnimation').click(function() { $('#customElement').stop(); });
            $('#resetElement').click(function() {
                $('#customElement').stop(true, true).removeAttr('style');
            });
        });
    </script>
</body>
</html>

Through this chapter, you should have mastered how to create various animations and effects using jQuery. In the next chapter, we'll learn about jQuery's AJAX functionality and asynchronous request handling.

Content is for learning and research only.