HTML CSS AND JAVA SCRIPT CODE FOR TRAFFIC LIGHT SYSTEM

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Animated Traffic Light System</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: linear-gradient(to bottom, #2c3e50, #4ca1af);
            margin: 0;
            font-family: Arial, sans-serif;
        }

        .traffic-light {
            width: 100px;
            background: #333;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
        }

        .light {
            width: 60px;
            height: 60px;
            background: #222;
            border-radius: 50%;
            margin: 15px auto;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }

        .light.red.active {
            background: red;
            box-shadow: 0 0 20px red;
        }

        .light.yellow.active {
            background: yellow;
            box-shadow: 0 0 20px yellow;
        }

        .light.green.active {
            background: green;
            box-shadow: 0 0 20px green;
        }

        .message {
            font-size: 24px;
            margin-top: 20px;
            color: white;
            animation: fadeInOut 2s infinite;
        }

        .message.stop {
            color: red;
        }

        .message.ready {
            color: yellow;
        }

        .message.go {
            color: green;
        }

        @keyframes fadeInOut {
            0%, 100% { opacity: 0; }
            50% { opacity: 1; }
        }

        .emoji {
            font-size: 50px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="emoji">👮</div>
    <div class="traffic-light">
        <div class="light red"></div>
        <div class="light yellow"></div>
        <div class="light green"></div>
    </div>
    <div class="message"></div>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const lights = document.querySelectorAll('.light');
            const message = document.querySelector('.message');
            let currentIndex = 0;

            function changeLight() {
                lights.forEach(light => light.classList.remove('active'));
                lights[currentIndex].classList.add('active');

                switch (currentIndex) {
                    case 0:
                        message.textContent = 'STOP';
                        message.className = 'message stop';
                        break;
                    case 1:
                        message.textContent = 'READY';
                        message.className = 'message ready';
                        break;
                    case 2:
                        message.textContent = 'GO';
                        message.className = 'message go';
                        break;
                }

                currentIndex = (currentIndex + 1) % lights.length;
            }

            setInterval(changeLight, 20000); // Change light every 20 seconds
            changeLight(); // Initialize the first light
        });
    </script>
</body>
</html>
HTML

Leave a Reply

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

Resize text
Scroll to Top