๋ฐ์ํ
[์ถ์ฒ-์ ํ๋ธ ๋ ธ๋ง๋ ์ฝ๋ Nomad Coders]
๋จผ์ ์ฌ์ฉํ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ฐ๋๋ค
-์ด๋ฏธ์ง ๋๋ค์ผ๋ก ๋ถ๋ฌ์ค๊ธฐ
bg.js
const body = document.querySelector("body");
const IMG_NUMBER = 3;
function paintImage(imgNumber){
const image = new Image();
image.src = `/images/${imgNumber + 1}.jpg`;
body.appendChild(image);
}
function genRandom(){
const number = Math.floor(Math.random() * IMG_NUMBER);
return number;
}
function init(){
const randomNumber = genRandom();
paintImage(randomNumber);
}
init();
index.css
body {
color: #34495e;
background-color: #2c3e50;
}
.btn {
cursor: pointer;
}
h1 {
color: #34495e;
transition: color 0.5s ease-in-out;
}
.clicked {
color: #7f8c8d;
}
.form,
.greetings {
display: none;
}
.showing {
display: block;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 0;
}
}
.bgImage{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
animation: fadeIn 0.5s linear;
}
bg.js
const body = document.querySelector("body");
// ์ด๋ฏธ์ง ๊ฐฏ์๋ฅผ ๋ณ์๋ก ์ค์ ํด๋์ผ๋ฉด ๋์ค์ ํจ์์์ ์์ ํ ํ์ ์์ด
// ์ฌ๊ธฐ์ ์ซ์ ๋ฐ๋ก ์์ ํ๋ฉด ๋จ!
const IMG_NUMBER = 3;
function paintImage(imgNumber){
// ์ด๋ฏธ์ง ๊ฐ์ฒด ์์ฑ
const image = new Image();
image.src = `/images/${imgNumber + 1}.jpg`;
//image ํ๊ทธ์ bgImage๋ผ๋ class ์ถ๊ฐ
image.classList.add('bgImage');
//body์ image ํ๊ทธ ์ถ๊ฐ
//parentNode.prepend(tag) : ๋ถ๋ชจ๋
ธ๋์ first child ์์ ์์ ์ถ๊ฐ
body.prepend(image);
}
function genRandom(){
const number = Math.floor(Math.random() * IMG_NUMBER);
return number;
}
function init(){
//getRandom ์คํํด์ ๋๋คํ ์ด๋ฏธ์ง๋ฒํธ๋ฅผ ์ป๊ณ
const randomNumber = genRandom();
//๊ทธ๊ฑธ paintImage ํจ์์ ๋ฃ์ด ํด๋น ์ด๋ฏธ์ง๋ฅผ ์์
paintImage(randomNumber);
}
init();
๋ฐ์ํ
๋๊ธ