<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Project #6 - Color Switcher</title>
</head>
<body>
<p class="color">
rgb(255, 255, 255)
</p>
<button class="switch">바꿔봐!</button>
<!-- --------------------------------- -->
<!-- JS File -->
<script src="app.js"></script>
</body>
</html>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
font-family: Arial, Helvetica, sans-serif;
transition: all 0.3s ease-in-out;
}
.switch {
background-color: #333;
color: #fff;
border: none;
outline: none;
padding: 15px 30px;
font-size: 20px;
margin-top: 40px;
cursor: pointer;
}
.color {
font-size: 50px;
}
const body = document.body;
const switchBtn = document.querySelector(".switch");
const switchPara = document.querySelector(".color");
switchBtn.addEventListener("click", function () {
let color1 = getRandomNum(); //red
let color2 = getRandomNum(); //green
let color3 = getRandomNum(); //blue
const colorString = `rgb(${color1}, ${color2}, ${color3})`;
// console.log(colorString);
body.style.backgroundColor = colorString;
switchPara.innerText = colorString;
});
function getRandomNum() {
return Math.floor(Math.random() * 256);
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.