
if(10 > 5){
console.log('ok')}
else{
console.log('no')}; // ok
상수를 선언할 때 대문자를 사용하는 경우는 일반 변수와 차이를 두기 위함.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="index.css" />
<title>Welcome</title>
</head>
<body>
<h1 id="width-log">Try resize</h1>
<!-- js -->
<script src="0311.js" defer></script>
</body>
</html>
css
.small {
background-color: blue;
}
.medium {
background-color: yellow;
}
.large {
background-color: green;
}
js
// 윈도우 사이즈 표시
const widthLog = document.querySelector("#width-log");
window.addEventListener('resize' , () => {
widthLog.innerHTML = `${window.innerWidth}px`
}
)
// 윈도우 사이즈 변화 시 body 컬러값 변경
const body = document.querySelector("body");
const SMALL_SIZE = "small";
const MEDIUM_SIZE = "medium";
const LARGE_SIZE = "large";
function resizeColor(){
const currentSize = window.innerWidth;
if(currentSize >= 900){
body.className = LARGE_SIZE
} else if(currentSize >= 600){
body.className = MEDIUM_SIZE
} else {
body.className = SMALL_SIZE
}
}
window.addEventListener('resize' , resizeColor)
실습 링크 : https://codesandbox.io/s/empty-blueprint-forked-eloff?file=/src/index.js