๐ŸŽฌ jQuery Effects & Animations + AJAX: Complete Guide with Real-Life Examples

Payal PorwalPayal Porwal
5 min read

๐ŸŒŸ PART 1: Effects and Animations

jQuery ki special strength uske easy-to-use animations hain. Sirf 1 line mein aap kisi bhi element ko hide, show, fade ya slide kar sakte ho!


๐Ÿ”น 1. hide(): Hides the selected element

$("#box").hide();

โœ… Use Case: Form submit karne ke baad form ko hide karna.

๐Ÿงธ Real-life Example:

<p id="note">Please read the instructions.</p>
<button id="hideNote">Hide Note</button>
$("#hideNote").click(function(){
  $("#note").hide();
});

๐Ÿ”น 2. show(): Displays a hidden element

$("#box").show();

๐Ÿงธ Real-life Example:

<button id="showNote">Show Note</button>
<p id="note" style="display:none;">Welcome to the website!</p>
$("#showNote").click(function(){
  $("#note").show();
});

๐Ÿ”น 3. fadeIn(): Fades in the element

$("#popup").fadeIn();

โœ… Use Case: Smoothly show a message or popup.

๐Ÿงธ Real-life Example:

<div id="popup" style="display:none;">๐ŸŽ‰ Success!</div>
<button id="successBtn">Show Success</button>
$("#successBtn").click(function(){
  $("#popup").fadeIn();
});

๐Ÿ”น 4. fadeOut(): Fades out the element

$("#popup").fadeOut();

๐Ÿงธ Real-life Example:

<button id="closePopup">Close</button>
<div id="popup">This is a notification</div>
$("#closePopup").click(function(){
  $("#popup").fadeOut();
});

๐Ÿ”น 5. slideUp(): Slide and hide the element

$("#menu").slideUp();

โœ… Use Case: Hide dropdown menus or FAQ sections.

๐Ÿงธ Real-life Example:

<div id="faq">This is the answer to a question.</div>
<button id="closeFaq">Close FAQ</button>
$("#closeFaq").click(function(){
  $("#faq").slideUp();
});

๐Ÿ”น 6. slideDown(): Slide and show the element

$("#menu").slideDown();

๐Ÿงธ Real-life Example:

<button id="openFaq">Open FAQ</button>
<div id="faq" style="display:none;">This is the answer to a question.</div>
$("#openFaq").click(function(){
  $("#faq").slideDown();
});

๐Ÿ”ธ 7. animate(): Create custom animations by changing CSS properties

$("#box").animate({width: "300px", height: "200px"}, 1000);

โœ… Use Case: Create smooth transitions and effects.

๐Ÿงธ Real-life Example:

<div id="box" style="width:100px; height:100px; background:red;"></div>
<button id="growBox">Grow Box</button>
$("#growBox").click(function(){
  $("#box").animate({width: "300px", height: "300px", opacity: 0.5}, 1500);
});

๐Ÿง  Box size badh jaata hai aur transparent ho jaata hai.


๐ŸŒ PART 2: AJAX with jQuery

AJAX (Asynchronous JavaScript and XML) ka use page reload kiye bina data fetch or send karne ke liye hota hai.

jQuery AJAX se aap easily server ke saath interact kar sakte ho.


๐Ÿง  What is AJAX?

  • AJAX allows web pages to send/receive data from server in background.

  • Used in real-world apps like:

    • Search suggestions (Google)

    • Auto-save (Google Docs)

    • Form submit without refresh


๐Ÿ”น 1. $.get(url, callback): Send GET request

$.get("data.json", function(data){
  console.log(data);
});

โœ… Use Case: Get data from API or file.

๐Ÿงธ Real-life Example:

<button id="loadUser">Load User</button>
<p id="userInfo"></p>
$("#loadUser").click(function(){
  $.get("https://jsonplaceholder.typicode.com/users/1", function(data){
    $("#userInfo").text("Name: " + data.name + ", Email: " + data.email);
  });
});

๐Ÿ”น 2. $.post(url, data, callback): Send POST request

$.post("submit.php", {name: "Amit"}, function(response){
  console.log(response);
});

โœ… Use Case: Submit form data to server.

๐Ÿงธ Real-life Example:

<input type="text" id="userName" placeholder="Enter name">
<button id="submitForm">Submit</button>
<p id="responseMsg"></p>
$("#submitForm").click(function(){
  var name = $("#userName").val();
  $.post("submit.php", {username: name}, function(data){
    $("#responseMsg").text(data);
  });
});

๐Ÿ”น 3. $.ajax(): Full customizable AJAX call

$.ajax({
  url: "api/data",
  type: "GET",
  success: function(result){
    console.log(result);
  }
});

๐Ÿงธ Real-life Example:

<button id="getWeather">Get Weather</button>
<p id="weatherInfo"></p>
$("#getWeather").click(function(){
  $.ajax({
    url: "https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=Delhi",
    type: "GET",
    success: function(data){
      $("#weatherInfo").text("Temp: " + data.current.temp_c + "ยฐC");
    },
    error: function(){
      $("#weatherInfo").text("Error fetching weather.");
    }
  });
});

๐Ÿง  Real-world example of getting weather data from an API.


โœ… Summary

ConceptReal Use Case
hide() / show()Toggle FAQs, alerts, modals
fadeIn() / fadeOut()Popups, tooltips
slideUp() / slideDown()Menus, collapsible sections
animate()Custom loading, transitions
$.get()Fetching data from APIs
$.post()Submitting forms without reload
$.ajax()Full control over API calls (GET/POST/etc.)

Here's a real-life practical example :

โฌ‡๏ธโฌ‡๏ธโฌ‡๏ธ
โœ… HTML
โœ… CSS
โœ… Bootstrap
โœ… JavaScript
โœ… jQuery with hide(), show(), fadeIn(), fadeOut(), slideUp(), slideDown(), animate()
โœ… AJAX with $.get() to fetch live data


๐Ÿ’ก Project: Live User Profile Viewer

This small web app:

  • Shows a user profile section.

  • Animates its appearance.

  • Fetches data using AJAX ($.get() from a public API).

  • Includes buttons to show/hide/fade/slide sections.

  • Looks good with Bootstrap styling.


โœ… Step-by-Step Code:

๐Ÿ”ธ HTML (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>User Profile Viewer</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    #profileCard {
      display: none;
      margin-top: 20px;
      opacity: 0;
    }
  </style>
</head>
<body class="bg-light">
  <div class="container py-5">
    <h2 class="text-center mb-4">๐Ÿ” Fetch & Animate User Profile</h2>

    <div class="text-center">
      <button id="loadUserBtn" class="btn btn-primary">Load Profile</button>
      <button id="hideBtn" class="btn btn-secondary">Hide</button>
      <button id="showBtn" class="btn btn-success">Show</button>
      <button id="slideBtn" class="btn btn-warning">Slide Toggle</button>
    </div>

    <div id="profileCard" class="card shadow mx-auto mt-4 p-3" style="width: 22rem;">
      <img src="" id="avatar" class="card-img-top rounded-circle" alt="Avatar">
      <div class="card-body text-center">
        <h5 class="card-title" id="userName">Loading...</h5>
        <p class="card-text" id="userEmail"></p>
      </div>
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

๐Ÿ”ธ JavaScript & jQuery (script.js)

javascriptCopyEdit$(document).ready(function() {
  // Load user data from API
  $("#loadUserBtn").click(function() {
    $.get("https://reqres.in/api/users/2", function(response) {
      const user = response.data;
      $("#avatar").attr("src", user.avatar);
      $("#userName").text(`${user.first_name} ${user.last_name}`);
      $("#userEmail").text(user.email);

      // Show card with fade & animate effect
      $("#profileCard").fadeIn(500).animate({opacity: 1}, 1000);
    });
  });

  // Hide card
  $("#hideBtn").click(function() {
    $("#profileCard").fadeOut();
  });

  // Show card
  $("#showBtn").click(function() {
    $("#profileCard").fadeIn().animate({opacity: 1}, 500);
  });

  // Slide toggle card
  $("#slideBtn").click(function() {
    $("#profileCard").slideToggle();
  });
});

๐Ÿ–ผ Output Preview:

  • Click "Load Profile" to fetch user info using AJAX.

  • Card appears using fadeIn() + animate().

  • Use "Hide", "Show", and "Slide Toggle" to trigger different effects.


๐Ÿ’ฌ Concepts Used:

jQuery MethodReal Use
$.get()Fetched live user data from API
fadeIn()Smoothly showed the profile card
fadeOut()Hid the card
animate()Gradually changed opacity
slideToggle()Slid the card in and out

This is a clean, reusable project example that you can show in your class or use as a base for larger apps like:

  • Employee Directory

  • Product Viewer

  • Feedback Submissions

Would you like me to add form submission using $.post() or make it a multi-user viewer with pagination? Let me know in comment section and Iโ€™ll extend it!

0
Subscribe to my newsletter

Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Payal Porwal
Payal Porwal

Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: ๐Ÿš€ In-depth explorations of emerging technologies ๐Ÿ’ก Practical tutorials and how-to guides ๐Ÿ”งInsights on software development best practices ๐Ÿš€Reviews of the latest tools and frameworks ๐Ÿ’ก Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letโ€™s connect and grow together! ๐ŸŒŸ