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)
}