[miniProjects] 29_Double Heart Click

๋ณด๋ฆฌยท2023๋…„ 6์›” 18์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
29/47

29_Double Heart Click

๐Ÿ’ป ์ฃผ์ œ : ์ด๋ฏธ์ง€ ์œ„์—์„œ ํด๋ฆญํ•œ ์œ„์น˜์— ํ•˜ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํด๋ฆญ ํšŸ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ธฐ๋Šฅ

const loveMe = document.querySelector('.loveMe')
const times = document.querySelector('#times')

let clickTime = 0
let timesClicked = 0

loveMe.addEventListener('click', (e) => {
  if(clickTime === 0) {
    clickTime = new Date().getTime
  } else {
    // 0.8์ดˆ ์ด๋‚ด๋กœ ํด๋ฆญ๋˜์–ด์•ผ ๋”๋ธ”ํด๋ฆญ์ด๋ผ๊ณ  ๊ฐ„์ฃผ.
    if((new Date().getTime() - clickTime) < 800) {
      createHeart(e)
      clickTime - 0
    } else {
      clickTime = new Date().getTime()
    }
  }
})

const createHeart = (e) => {
  const heart = document.createElement('i')
  heart.classList.add('fas')
  heart.classList.add('fa-heart')

  //ํด๋ฆญ์‹œ x, y ์ขŒํ‘œ์— ํ•˜ํŠธ ์ƒ์„ฑํ•  ๊ฑฐ์ž„.
  const x = e.clientX
  const y = e.clientY

  const leftOffset = e.target.offsetLeft
  const topOffset = e.target.offsetTop

  const xInside = x - leftOffset
  const yInside = y - topOffset
  
  heart.style.top = `${yInside}px`
  heart.style.left = `${xInside}px`

  loveMe.appendChild(heart)

  times.innerHTML = ++timesClicked

  setTimeout(() => heart.remove(), 5000)
} 
  • clickTime์ด 0์ธ ๊ฒฝ์šฐ: ํ˜„์žฌ ์‹œ๊ฐ„์„ clickTime ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
  • clickTime์ด 0์ด ์•„๋‹Œ ๊ฒฝ์šฐ: ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ์ด์ „ ํด๋ฆญ ์‹œ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ 0.8์ดˆ๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ, createHeart ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ•˜ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  clickTime์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ, ํ˜„์žฌ ์‹œ๊ฐ„์„ clickTime ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.
  • createHeart ํ•จ์ˆ˜๋Š” ํด๋ฆญํ•œ ์œ„์น˜์— ํ•˜ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ํด๋ฆญ ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ƒ์„ฑ๋œ ํ•˜ํŠธ๋Š” 5์ดˆ ํ›„์— ์ œ๊ฑฐ๋œ๋‹ค.
profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€