JSFiddle - React, Tailwind, and code Playground

by Venkatesh Dematti

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Feedback Box</title>
</head>
<body>
    <!-- Feedback Box HTML Structure -->
    <div id="feedback-box" class="feedback-box">
        <span id="close-btn" class="close-btn">&times;</span>
        <div class="cta">
            <img src="icon.png" alt="Feedback Icon" class="icon">
            <a href="https://example.com/survey" class="text" target="_blank">Your feedback is important!</a>
        </div>
    </div>
</body>
</html>

CSS

/* Basic styling for the feedback box */
        .feedback-box {
            position: relative;
            right: 0;
            bottom: 0;
            margin: 20px; /* Adjust as needed */
            padding: 10px;
            background-color: red;
            box-shadow: 0 0 10px rgba(0,0,0,0.2);
            border-radius: 5px;
            display: none; /* Hidden by default */
            z-index: 1000;
        }
        
        /* Display feedback box when scrolled past top third */
        .feedback-box.active {
            display: block;
        }
        
        .feedback-box .cta {
            display: flex;
            align-items: center;
        }
        
        .feedback-box .cta .icon {
            margin-right: 10px;
        }
        
        .feedback-box .cta .text {
            font-size: 14px;
            color: #007bff;
            text-decoration: none;
        }
        
        /* The close button 'X' */
        .feedback-box .close-btn {
            display: none;
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
            color: #333;
        }
        
        /* Show 'X' on hover */
        .feedback-box:hover .close-btn {
            display: block;
        }
        
        /* Responsive design */
        @media (max-width: 768px) {
            .feedback-box {
                margin-right: 20px;
                margin-bottom: 250px; /* 250px distance on mobile */
            }
        }

        @media (min-width: 769px) {
            .feedback-box {
                margin-right: 20px;
                margin-bottom: 350px; /* 350px distance on desktop */
            }
        }

JavaScript

// JavaScript to handle the visibility of the feedback box
        document.addEventListener('DOMContentLoaded', () => {
            const feedbackBox = document.getElementById('feedback-box');
            const closeButton = document.getElementById('close-btn');

            // Show feedback box when scrolled past the top third
            window.addEventListener('scroll', () => {
                const scrollPosition = window.scrollY + window.innerHeight;
                const documentHeight = document.documentElement.scrollHeight;

                if (scrollPosition > (documentHeight / 3)) {
                    feedbackBox.classList.add('active');
                } else {
                    feedbackBox.classList.remove('active');
                }
            });

            // Handle close button click
            closeButton.addEventListener('click', () => {
                feedbackBox.classList.remove('active');
                localStorage.setItem('feedbackBoxDismissed', 'true');
            });

            // Check if the feedback box was previously dismissed
            if (localStorage.getItem('feedbackBoxDismissed')) {
                feedbackBox.classList.remove('active');
            }
        });