nomadcoders - 크롬 앱 background & quotes

sang one leee·2023년 1월 30일

nomadcoders

목록 보기
3/10

background.js

const images = [
    "img1.jpeg", "img2.jpeg","img3.jpeg","img4.png","img5.jpeg"]

const chosenImage = images[Math.floor(Math.random() * images.length)]

const bgImage = document.createElement("img")

bgImage.src = `img/${chosenImage}`

document.body.appendChild(bgImage)

quotes.js

const quotes = [...]

const quote = document.querySelector("#quote span:first-child")
const author = document.querySelector("#quote span:last-child")

const todaysQuote = quotes[Math.floor(Math.random() * quotes.length)]

quote.innerText = todaysQuote.quote
author.innerText = todaysQuote.author

랜덤한 배경과 명언을 위해 Math.random * .length를 하고 Math.floor로 정수를 만들어 배경과 명언 배열에 있는 모든 원소들이 선택되게 한다. HTML 문서에서, .createElement() 메서드는 지정한 tagName의 HTML 요소를 만들어 반환한다. .createElement를 통하여 생선된 이미지는 bgImage에 저장하고 bgImage는 .src를 통해 저장한 이미지들을 받아오게 한다. Node.appendChild() 메소드는 한 노드를 특정 부모 노드의 자식 노드 리스트 중 마지막 자식으로 붙인다.

profile
코린이 화이팅

0개의 댓글