How to add easing effect to your animation with jQuery?

To add easing effects to your jQuery animations, you need to use the animation speed properly and choose the right easing function to create perfect animations for your web page. The key is knowing when to speed up, slow down, and stop your animations while using the animate() method.

jQuery provides several built-in easing options like "swing" and "linear", or you can use custom easing functions for more advanced effects. The "swing" easing creates animations that start slow, speed up, then slow down at the end, while "linear" maintains a constant speed throughout the animation. The key is to control the timing and smoothness of your animations.

Built-in Easing Types

jQuery includes two default easing functions −

  • linear − Moves at constant speed from start to finish
  • swing − Starts slow, speeds up in the middle, and slows down at the end (default)

Basic Syntax

The basic syntax for adding easing to jQuery animations is −

$(selector).animate(properties, duration, easing, callback);

Where −

  • properties − CSS properties to animate
  • duration − Animation duration in milliseconds
  • easing − Easing function name ("linear" or "swing")
  • callback − Function to execute after animation completes

Visual Comparison of Easing Types

To better understand how different easing functions behave, here's a visual representation of their timing curves −

Time Progress Linear Easing Swing Easing

Complete Example

The following example demonstrates how to add easing effects using jQuery animations with proper timing and smooth transitions. Here we have buttons that reveal content and compare different easing types −

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('#answer').on('click', function() {
                var container = $('.new-answer');
                
                // Set initial state
                container.css({
                    'opacity': '0',
                    'height': '0px',
                    'display': 'block'
                });
                
                // Fade out button with swing easing
                $(this).fadeOut(500, 'swing', function() {
                    // Animate container with easing
                    container.animate({ 
                        height: "200px",
                        opacity: 1
                    }, 800, 'swing').animate({
                        height: "170px" 
                    }, 300, 'linear');
                });
            });
            
            // Demo different easing types
            $('#linearBtn').click(function() {
                $('#linearDemo').animate({left: '250px'}, 1000, 'linear');
            });
            
            $('#swingBtn').click(function() {
                $('#swingDemo').animate({left: '250px'}, 1000, 'swing');
            });
            
            $('#resetDemo').click(function() {
                $('.demo-box').animate({left: '0px'}, 500);
            });
        });
    </script>
    <style>
        .btn {
            width: 150px;
            height: 40px;
            color: #666666;
            background: #f0f0f0;
            border: 1px solid #ccc;
            cursor: pointer;
            margin: 10px;
            padding: 8px 16px;
            border-radius: 4px;
        }
        .btn:hover {
            background: #e0e0e0;
        }
        .new-answer {
            background: #e8f4fd;
            border: 2px solid #3498db;
            border-radius: 5px;
            margin: 20px 0;
            display: none;
        }
        .new-answer .middle, .top {
            padding: 15px;
        }
        .new-answer textarea {
            color: #333;
            font-size: 14px;
            height: 80px;
            padding: 8px;
            width: 100%;
            border: 1px solid #ccc;
            border-radius: 3px;
            box-sizing: border-box;
        }
        .demo-section {
            margin: 30px 0;
            border: 1px solid #ddd;
            padding: 20px;
            background: #f9f9f9;
        }
        .demo-box {
            width: 80px;
            height: 80px;
            position: relative;
            margin: 10px 0;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            border-radius: 5px;
        }
        #linearDemo {
            background: #e74c3c;
        }
        #swingDemo {
            background: #3498db;
        }
    </style>
</head>
<body>
    <h3>Main Easing Demo</h3>
    <button class="btn" id="answer">Add Answer</button>
    <div class="new-answer">
        <div class="top">
            <span><b>Add a new answer below</b></span>
        </div>
        <div class="middle">
            <textarea placeholder="Enter your answer here..."></textarea>
        </div>
    </div>
    
    <div class="demo-section">
        <h3>Compare Easing Types</h3>
        <div class="demo-box" id="linearDemo">Linear</div>
        <div class="demo-box" id="swingDemo">Swing</div>
        <button class="btn" id="linearBtn">Animate Linear</button>
        <button class="btn" id="swingBtn">Animate Swing</button>
        <button class="btn" id="resetDemo">Reset Position</button>
    </div>
</body>
</html>

Key Easing Techniques

In this example, the easing effect is achieved through −

  • Using fadeOut(500, 'swing') to smoothly hide the button with swing easing
  • Chaining multiple animate() methods with different easing types
  • Controlling opacity and height properties for smooth transitions
  • Specifying duration and easing parameters explicitly for better control

Best Practices

When implementing easing effects, consider these guidelines −

  • Use swing easing for natural, user-friendly animations
  • Apply linear easing for consistent, mechanical movements
  • Keep animation durations between 200-800ms for optimal user experience
  • Chain animations carefully to avoid overwhelming users
  • Test animations on different devices to ensure smooth performance

Conclusion

Adding easing effects to jQuery animations enhances user experience by creating smooth, natural-looking transitions. The key is to choose the appropriate easing type and timing for your specific use case, whether you need the natural feel of swing easing or the consistent pace of linear easing. Practice with different combinations to master the art of smooth web animations.

Updated on: 2026-03-13T20:23:28+05:30

675 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements