Implementing a floating action button for mobile

Loading

That’s a huge amount of detail for a floating action button (FAB), but I can certainly provide an extensive guide. Below is a comprehensive, step-by-step explanation on implementing a Floating Action Button (FAB) for mobile using jQuery, covering everything from design principles to best practices and performance optimizations.


Introduction to Floating Action Buttons (FAB)

A Floating Action Button (FAB) is a UI component commonly used in mobile applications. It is a circular button that hovers above the content, typically positioned at the bottom-right corner of the screen. FABs are used for primary actions like:

  • Adding a new item (e.g., Compose Email in Gmail)
  • Starting a chat (e.g., WhatsApp new chat button)
  • Uploading a file
  • Navigating to a key feature

In this guide, we’ll build a FAB using jQuery, HTML, CSS, and JavaScript, ensuring that it is:

Responsive (works well on different screen sizes)
Animated (for smooth interaction)
Accessible (keyboard-friendly, screen reader support)
Lightweight (optimized for performance)


Step 1: Setting Up the Basic HTML Structure

We start by creating a simple HTML layout for the floating action button.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Floating Action Button with jQuery</title>
    <link rel="stylesheet" href="styles.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

    <div class="fab-container">
        <button class="fab">
            +
        </button>
        <div class="fab-menu">
            <button class="fab-item">📷</button>
            <button class="fab-item">📝</button>
            <button class="fab-item">📎</button>
        </div>
    </div>

    <script src="script.js"></script>
</body>
</html>

Explanation:

  • We create a <div class="fab-container"> to hold the main floating button (<button class="fab">+ </button>) and a menu (<div class="fab-menu">) containing additional action buttons.
  • The fab-menu contains three sub-buttons for different actions (camera, notes, and attachments).

Step 2: Styling the Floating Action Button (FAB) with CSS

We will now style the FAB and the menu items to create a floating effect.

CSS Code (styles.css)

/* General Page Styles */
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f5f5f5;
}

/* FAB Container */
.fab-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Main FAB Button */
.fab {
    width: 56px;
    height: 56px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease-in-out;
}

/* FAB Hover Effect */
.fab:hover {
    background-color: #0056b3;
}

/* FAB Menu */
.fab-menu {
    display: none;
    flex-direction: column;
    position: absolute;
    bottom: 70px;
    right: 5px;
}

/* FAB Menu Items */
.fab-item {
    width: 40px;
    height: 40px;
    background-color: white;
    border: none;
    border-radius: 50%;
    font-size: 20px;
    margin: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease-in-out;
}

/* FAB Item Hover */
.fab-item:hover {
    background-color: #ddd;
}

Explanation:
✔ The FAB is styled to be a circular button with a shadow effect.
✔ The FAB menu items are hidden by default (display: none;).
✔ When the user clicks the FAB, the menu will slide out (handled in JavaScript).


Step 3: Adding Interactivity with jQuery

We now use jQuery to handle the FAB interactions like opening/closing the menu.

JavaScript Code (script.js)

$(document).ready(function() {
    $(".fab").click(function() {
        $(".fab-menu").slideToggle(300); // Toggle the menu visibility
    });

    $(".fab-item").click(function() {
        let action = $(this).text();
        alert("Action selected: " + action);
    });

    // Close menu when clicking outside
    $(document).click(function(event) {
        if (!$(event.target).closest(".fab-container").length) {
            $(".fab-menu").slideUp(300);
        }
    });
});

Explanation:
FAB Click Event: Toggles the menu open/close with a smooth sliding animation.
Menu Item Click Event: Shows an alert when a user selects an option.
Click Outside FAB: Hides the menu when clicking outside the button.


Step 4: Enhancing UX with Animations

To improve user experience, let’s animate the FAB menu items.

Modify the script.js file:

$(document).ready(function() {
    $(".fab").click(function() {
        if ($(".fab-menu").is(":visible")) {
            $(".fab-item").each(function(index) {
                $(this).delay(100 * index).fadeOut(200);
            });
            setTimeout(() => $(".fab-menu").slideUp(300), 400);
        } else {
            $(".fab-menu").slideDown(300, function() {
                $(".fab-item").each(function(index) {
                    $(this).delay(100 * index).fadeIn(200);
                });
            });
        }
    });
});

Each menu item fades in/out sequentially for a smooth effect.
Delays create a staggered animation for a polished look.


Step 5: Making the FAB Mobile-Friendly

To optimize the FAB for mobile devices, update the CSS styles:

@media (max-width: 768px) {
    .fab {
        width: 50px;
        height: 50px;
        font-size: 20px;
    }
    
    .fab-item {
        width: 35px;
        height: 35px;
        font-size: 16px;
    }
}

Reduced FAB size for smaller screens.
Smaller menu items to fit mobile UI better.


We have successfully implemented a floating action button (FAB) using jQuery with:
Smooth animations
Event handling (menu toggle, outside click)
Mobile responsiveness
Optimized UX

This FAB can be customized for different applications (shopping cart, chat, social media, etc.).


Want more features? 🤔 Try:
🔹 Icons instead of text
🔹 Custom animations
🔹 Integrate with APIs

Let me know if you need further enhancements!

Leave a Reply

Your email address will not be published. Required fields are marked *