VotingSystem

HTML

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="Style.css" />
</head>
<body>
    <div class="centered-container">
        <div id="results">
            <div class="result-item">
                <span class="option">Option 1:</span>
                <span class="percentage" id="option1-percentage">0%</span>
                <div id="option1" class="result-bar">
                </div>
            </div>
            <div class="result-item">
                <span class="option">Option 2:</span>
                <span class="percentage" id="option2-percentage">0%</span>
                <div id="option2" class="result-bar">
                </div>
            </div>
            <div class="result-item">
                <span class="option">Option 3:</span><span class="percentage" id="option3-percentage">0%</span>
                <div id="option3" class="result-bar">
                </div>
            </div>
            <div class="result-item">
                <span class="option">Option 4:</span><span class="percentage" id="option4-percentage">0%</span>
                <div id="option4" class="result-bar">
                </div>
            </div>
        </div>
    </div>
    <div id="vote-options">
        <button onclick="vote('Option 1')">Option 1</button>
        <button onclick="vote('Option 2')">Option 2</button>
        <button onclick="vote('Option 3')">Option 3</button>
        <button onclick="vote('Option 4')">Option 4</button>
    </div>
    <div id="timer">Time Remaining: <span id="time">15</span> seconds</div>
    <div id="result-message"></div>

    <!-- Overlay for displaying the winner -->
    <div id="overlay" class="overlay">
        <div class="overlay-content">
            <span id="winner-message"></span>
            <button onclick="closeOverlay()">Close</button>
        </div>
    </div>

    <script...

CSS

/* styles.css */
body {
    font-family: Arial, sans-serif;
    text-align: center;
    margin-top:100px;
    background-color:linen
}

.centered-container {
    width: 600px;
    margin: 0 auto;
    background-color: white;
    border-radius: 20px;
}

#results {
    text-align: left;
    border: 3px solid black;
    border-radius: 20px;
    padding: 10px 7px 15px 7px;
}

.result-item {
    display: flex;
    flex-direction: column; /* Change to column layout */
    align-items: flex-start; /* Align items to the start */
    margin: 5px 0;
}

    .result-item span {
        width: auto; /* Allow the text to take its natural width */
    }

.result-bar {
    border-radius: 10px;
    margin: 5px 5px 0 5px;
    padding: 10px;
    position: relative;
    width: 0; /* Start with 0 width */
    max-width: 540px;
    height: auto; /* Height for the bars */
    transition: width 0.5s; /* Smooth transition for width change */
}

/* Position percentage at the top right of the bar */
.percentage {
    position: relative; /* Change to relative */
    margin-left: 10px; /* Add some margin for spacing */
    color: black;
}

.option {
    margin: 5px 0 0 10px;
}

/* Specific Colors for Each Option */
#option1 {
    background-color: #FF5733; /* Vibrant red */
}

#option2 {
    background-color: #28A745; /* Bright green */
}

#option3 {
    background-color: #FFC107; /* Bright yellow */
}

#option4 {
    background-color: #007BFF; /* Bright blue */
}

#timer {
    margin-top: 20px;
    font-weight: bold;
}

#result-message {
    margin-top: 20px;
    font-size: 1.2em;
    color: green; /* Optional: Change color for better visibility */
}

/* Improved Button Styles */
#vote-options{
    margin-top:20px;
}
    #vote-options button {
        background-color: #551db0; /* Blue button */
        color: white; /* White text */
        border: none; /* No border */
        border-radius: 5px; /* Rounded corners */
        padding: 10px 15px; /* Padding for button */
        margin:...

JavaScript

// script.js
let votes = {
    'Option 1': 0,
    'Option 2': 0,
    'Option 3': 0,
    'Option 4': 0,
};

let totalVotes = 0;
let timer;
let timeRemaining = 15; //timer
let uservote = localStorage.getItem('userVote');
if (uservote) {
    localStorage.clear();
    if (uservote == "option1") {
        vote('Option 1');
    }
    else if (uservote == "option2") {
        vote('Option 2');
    }
    else if (uservote == "option3") {
        vote('Option 3');
    }
    else if (uservote == "option4") {
        vote('Option 4');
    }
}
function vote(option) {
    votes[option]++;
    totalVotes++;
    updateResults();
}

function updateResults() {
    for (let option in votes) {
        let percentage = (votes[option] / totalVotes) * 100 || 0;
        document.getElementById(option.toLowerCase().replace(' ', '')).style.width = percentage + '%';
        document.getElementById(`${option.toLowerCase().replace(' ', '')}-percentage`).innerText = Math.round(percentage) + '%';
    }
}

function startTimer() {
    timer = setInterval(() => {
        timeRemaining--;
        document.getElementById('time').innerText = timeRemaining;

        if (timeRemaining <= 0) {
            clearInterval(timer);
            announceWinner();
        }
    }, 1000);
}

function announceWinner() {
    let maxVotes = 0;
    let winningOptions = [];

    for (let option in votes) {
        if (votes[option] > maxVotes) {
            maxVotes = votes[option];
            winningOptions = [option];
        } else if (votes[option] === maxVotes) {
            winningOptions.push(option);
        }
    }

    // Prepare the winner message
    let message = '';
    if (winningOptions.length === 1) {
        message = `The winning option is: ${winningOptions[0]}!`;
    } else {
        message = 'The winning options are: ' + winningOptions.join(', ') + '!';
    }

    // Display the overlay with the winner message
    document.getElementById('winner-message').innerText = message;
    const...