const body = document.querySelector('body');
const btn = document.querySelector('.button');
let colors = [
'yellow',
'green',
'pink',
'#dc143c',
'rgba(123, 123, 123, 0.5)'
];
btn.addEventListener('click', colorChange);
function colorChange() {
const random = parseInt(Math.random() * colors.length);
body.style.backgroundColor = colors[random];
};
색을 배열로 만들어서 버튼 클릭하면 배경색이 바뀌는 걸 구현해 보았다
문제점은 body에 분명히 style을 줬지만 일부만 바뀌었다.
해결
body {
height: 100vh;
/* Viewport height의 100% */
margin: 0;
/* 기본 여백 제거 */
}
여백을 없애니 body가 넓어져서 전체 색 변경이 가능하였다.
'sparta > 본캠프' 카테고리의 다른 글
[주말]Counter만들기 (0) | 2024.10.27 |
---|---|
[textContent] (1) | 2024.10.26 |
[reduce] (1) | 2024.10.26 |
[Spread 및 Rest 연산자] (1) | 2024.10.26 |
[구조분해할당] (0) | 2024.10.26 |