Javascript Background Color Switcher Project




Color Switcher Project In Javascript




index.html

 <!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="./style.css">
    <title>Javascript Background Color Switcher</title>
</head>
<body>
    <nav>
        <a href="/" aria-current="page">Home</a>
        <a target="_blank" href="https://codervk.blogsopt.com">Blog</a>
    </nav>
    <div class="canvas">
    <h1>Color Schema Switcher</h1>
    <span class="button" id="grey"></span>
    <span class="button" id="white"></span>
    <span class="button" id="blue"></span>
    <span class="button" id="yellow"></span>
    <h2>
        Try clicking on one of colors above
        <span>to change the background color of the page 📄 </span>
    </h2>
    </div>
    <script src="colorchanger.js"></script>
</body>
</html>

colorchanger.js
const buttons = document.querySelectorAll('.button')
// console.log(buttons)
const body = document.querySelector('body')

buttons.forEach(function (button) {
    console.log(button);
    button.addEventListener('click', function (e) {
        console.log(e);
        console.log(e.target);
        if (e.target.id === 'grey') {
            body.style.backgroundColor = e.target.id
        }
        if (e.target.id === 'white') {
            body.style.backgroundColor = e.target.id
        }
        if (e.target.id === 'blue') {
            body.style.backgroundColor = e.target.id
        }
        if (e.target.id === 'yellow') {
            body.style.backgroundColor = e.target.id
        }
     
    })
});
style.css
/* Reset default styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Body styles */
body {
    font-family: Arial, sans-serif;
    background-color: #f2f2f2;
    /* Default background color */
}

/* Navigation styles */
nav {
    background-color: #333;
    color: #fff;
    padding: 10px;
}

nav a {
    color: #fff;
    text-decoration: none;
    margin-right: 10px;
}

/* Canvas styles */
.canvas {
    text-align: center;
    padding: 20px;
}

h1 {
    margin-bottom: 20px;
}

h2 {
    margin-top: 20px;
}

/* Color button styles */
.button {
    display: inline-block;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    margin: 5px;
    cursor: pointer;
}

#grey {
    background-color: #999;
}

#white {
    background-color: #fff;
}

#blue {
    background-color: #3498db;
}

#yellow {
    background-color: #f1c40f;
}

Post a Comment

0 Comments