ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

imgList


https://unsplash.com/documentation#get-a-random-photo

 

Unsplash API Documentation | Free HD Photo API | Unsplash

Getting started This document describes the resources that make up the official Unsplash JSON API. If you have any problems or requests, please contact our API team. Creating a developer account To access the Unsplash API, first join. Registering your appl

unsplash.com

๋‹ค์Œ๊ณผ ๊ฐ™์ด API๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ Unsplash ์‚ฌ์ดํŠธ์—์„œ ๋žœ๋ค์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ณ , ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ํด๋ฆญํ•˜์˜€์„ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€ ๊ด€๋ จ ์ •๋ณด๋“ค์ด ๋‹ด๊ธด

ํŒ์—…์ฐฝ์œผ๋กœ ํ™”๋ฉด ์ƒ์— ๋„์šฐ๋„๋ก ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

for(let i = 0; i < data.length; i++) {
        imageLists.innerHTML += `
            <li class="image-list"
                data-user=${data[i].user}
                data-image=${data[i].urls.full}
            >
                <img src=${data[i].urls.full} alt=${data[i].alt_description}>
            </li>
        `
    }

์ด๋ฏธ์ง€ ์š”์†Œ๋ฅผ ํด๋ฆญํ•˜์˜€์„ ๋•Œ JS์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด๋‘” ํ•ด๋‹น ์ด๋ฏธ์ง€ ์š”์†Œ์˜ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ํ† ๋Œ€๋กœ ํŒ์—…์ฐฝ์˜ ๋‚ด์šฉ์„ ๊ตฌ์„ฑํ•˜๋ ค๊ณ  ํ•˜์˜€์œผ๋‚˜

๋ฐ์ดํ„ฐ ๋‚ด์— user ํ”„๋กœํผํ‹ฐ๋Š” ๊ฐ์ฒด์˜€๊ณ  ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ๋งˆํฌ์—…์— ์ €์žฅํ•˜๋‹ค ๋ณด๋‹ˆ, ์Šคํฌ๋ฆฝํŠธ ์ƒ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด ํ˜•์‹์ด

์•„๋‹Œ Object ๋ฌธ์ž ๊ทธ ์ž์ฒด๋กœ ์ €์žฅ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

ํ•ด๊ฒฐ

for(const img of imgs) {
        const {urls, alt_description, user} = img;
        const {name, instagram_username, profile_image: {medium}} = user;
        const li = document.createElement('li');

        li.classList.add('image-list');
        li.dataset.user = JSON.stringify({name, instagram_username, medium});
        li.dataset.image = `${urls.regular}`;
        li.innerHTML = `<img src="${urls.regular}" alt="${alt_description}">`;
        imageLists.appendChild(li);

        li.addEventListener('click', function() {
            const obj = {
                image: this.dataset.image,
                ...JSON.parse(this.dataset.user)
            }
            modal.classList.add('active');
            thumbnail.src = obj.image;
            profileImg.src = obj.medium;
            userName.textContent = obj.name;
            insta.textContent = obj.instagram_username;
        })
    }

์š”์†Œ์˜ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•˜์˜€๋‹ค.

  1. fetchํ•œ data์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋งŽ๊ฑฐ๋‚˜ ์ค‘์ฒฉ ๊ฐ์ฒด์ผ ๊ฒฝ์šฐ์—๋Š” ๋””์ŠคํŠธ๋Ÿญ์ณ๋ง ํ• ๋‹น์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜ํ™”ํ•˜์—ฌ ์‹๋ณ„๋˜๋„๋ก ํ•œ๋‹ค.
  2. ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— ํ–ฅํ›„ ์ฐธ์กฐ๋  ๋ฐ์ดํ„ฐ๋ฅผ ์ ์žฌํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—๋Š” HTMLElement.dataset์„ ์‚ฌ์šฉํ•œ๋‹ค.
  3. ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์„œ๋ฒ„๋กœ ๊ฐ์ฒด๋ฅผ ์ „์†กํ•˜๋ ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ดํ™”ํ•ด์•ผ ๋œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด JSON.stringify ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ JSON ํฌ๋งท์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ง๋ ฌํ™” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค.
  4. ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํด๋ผ์ด์–ธํŠธ์—์„œ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ dataset์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ „์†กํ•˜๋Š”๋ฐ, ์ด๋•Œ ์ „์†ก๋œ JSON ๋ฐ์ดํ„ฐ๋Š” ๋ฌธ์ž์—ด์ด๋‹ค. ์ด ๋ฌธ์ž์—ด์„ ๊ฐ์ฒด๋กœ์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด JSON.parse ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด์„ ๊ฐ์ฒดํ™”ํ•˜๋Š” ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค.
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ