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

์˜ค๋ฅ˜

const img = new Image();
img.src = images[0]; //image src๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด
console.log(img.width);

 

์ด๋ฏธ์ง€์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ฐธ์กฐํ•ด์•ผ ๋˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Image ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์— ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด ์ค€ ๋’ค, ์ด๋ฏธ์ง€์˜

๊ฐ€๋กœ ์‚ฌ์ด์ฆˆ๋ฅผ ์ฐธ์กฐํ•˜๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ์›๋ž˜ ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ๊ฐ€ ์•„๋‹Œ 0 ๊ฐ’์ด ๊ณ„์† ๋‚˜์˜ค๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

 

๊ฒ€์ƒ‰ํ•ด์„œ ํ™•์ธํ•ด ๋ณด๋‹ˆ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค์™€ ๊ฐ™์ด ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ์€ ๋น„๋™๊ธฐ ์ž‘์—…์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค๋Š” ์‚ฌ์‹ค์„ ํ™•์ธํ•˜์˜€๋‹ค.

์ฆ‰, ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„๋ฅผ ์š”์ฒญํ•  ๋•Œ, img.width์˜ ๊ฐ’์€ ์•„์ง ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ค์ง€ ์•Š์€ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์—(๋น„๋™๊ธฐ ์ž‘์—…์€ ๋‹ค๋ฅธ ์ž‘์—…์ด ๋™๊ธฐ์ ์œผ๋กœ ๋ชจ๋‘ ์‹คํ–‰์ด ๋งˆ์นœ ๋’ค์— ์‹คํ–‰) ๊ฐ’์ด 0์ด ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค.

const img = new Image();
img.src = images[0];
img.onload = e => console.log(img.width);

์œ„์˜ ์‚ฌ์‹ค์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด load ์ด๋ฒคํŠธ๋ฅผ ์ด๋ฏธ์ง€ ์ธ์Šคํ„ด์Šค์— ๋“ฑ๋กํ•ด์ฃผ๊ณ  ๊ทธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ ๋„ˆ๋น„๋ฅผ ์ถœ๋ ฅํ•ด ๋ดค๋”๋‹ˆ

์ด๋ฏธ์ง€์˜ ์ œ๋Œ€๋กœ ๋œ ๋„ˆ๋น„ ๊ฐ’์ด ๋‚˜์™”๋‹ค.

ํด๋กœ์ €๋กœ ์ธํ•ด ์ƒ์œ„ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์„ ์–ธ๋œ img ๋ณ€์ˆ˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ฐธ์กฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํ•ด๋‹น ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ƒ์œ„ ๋ณ€์ˆ˜์— ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ์ƒ์œ„ ํ•จ์ˆ˜ ๋‚ด์—์„œ๋Š” ๊ทธ ๋ณ€์ˆ˜ ๋‚ด์— ๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค.

(ํ•ด๋‹น ๊ฐ’์ด ๋“ค์–ด๊ฐ„ ์‹œ์ ์€ ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์œ„ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋œ ์‹œ์ ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค)

load event: css ๋ฐ image ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๋“ค์ด ๋ชจ๋‘ ์™„๋ฃŒ๋œ ๋’ค์— ํ˜ธ์ถœ ๋ฐ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

DomContentLoaded์™€ onLoad ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

Promise๋‚˜ async/await๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ ํ›„์† ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๋ฐฉ์‹๋ง๊ณ ๋„ DomcontentLoaded์™€ onLoad ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์œ„์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์šฐ์„  ๋‹ค์Œ๊ณผ ๊ฐ™์ด img ํƒœ๊ทธ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋งˆํฌ์—…์„ innerHTML ์ƒ์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด ์ค€๋‹ค.์ด๋•Œ DOMContentLoaded ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์œ„์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰์ค€๋‹ค๋ฉด ์šฐ์„ ์ ์œผ๋กœ ๊ฒฝ๋กœ๋ฅผ ์ •ํ•ด ์ค€ imgํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

DOMContentLoaded: HTML ํŒŒ์‹ฑ๋งŒ ์™„๋ฃŒ๋˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ๋ฐ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.
document.addEventListener('DOMContentLoaded', () => {
    $container.innerHTML = `
      <div class="carousel-slides">
        ${[images[images.length - 1], ...images, images[0]].map(url => `<img src="${url}" />`).join('')}
      </div>
      <button class="carousel-control prev">&laquo;</button>
      <button class="carousel-control next">&raquo;</button>
    `;

    $carouselSlides = document.querySelector('.carousel-slides');
  });

 

๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์—์„œ ํ•ด๋‹น ์‹ค์ œ ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์„ load ์ด๋ฒคํŠธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•ด ์ค€๋‹ค. ๊ฒฝ๋กœ๊ฐ€ ์ •ํ•ด์ง„ img ํƒœ๊ทธ๋Š” DomContentLoaded ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด์—์„œ ๋™์ ์œผ๋กœ ์ด๋ฏธ ์ƒ์„ฑํ•ด ๋‘” ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ํƒœ๊ทธ ์„ ํƒ์ž๋ฅผ ํ†ตํ•ด์„œ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

window.onload = () => {
    const { offsetWidth } = $carouselSlides.querySelector('img');
    $container.style.width = `${offsetWidth}px`;
    $container.style.opacity = 1;

    move(++currentSlide);
  };

https://choi95.tistory.com/3?category=857834 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์‹ฑ๊ณผ ์‹คํ–‰

 Window load ํฌ๊ฒŒ ๋„ค ๊ฐ€์ง€์˜ API๊ฐ€ ์žˆ๋‹ค.  1. DOMContentLoaded: HTML ํŒŒ์‹ฑ๋งŒ ์™„๋ฃŒ๋˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ๋ฐ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.  2. load: css ๋ฐ image ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๋“ค์ด ๋ชจ๋‘ ์™„๋ฃŒ๋œ ๋’ค์— ํ˜ธ์ถœ ๋ฐ ์‹ค..

choi95.tistory.com

 

'JS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

insertAdjacentElement๋กœ ์š”์†Œ ์ถ”๊ฐ€  (0) 2021.07.13
์บ๋Ÿฌ์…€(Carousel)  (0) 2021.07.10
JS ์ƒ์—์„œ HTML ๋™์  ์ƒ์„ฑ  (0) 2021.07.07
createDocumentFragment()๋ฅผ ํ†ตํ•œ ์š”์†Œ ์ƒ์„ฑ  (0) 2021.07.05
stopwatch  (0) 2021.07.05
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ