I’m working on a little thing where i need to check if the img.src
is already set or not, in order to prevent it to set continuously, as it does right now. The problem lies in my onMove
. How can i do that?
HTML:
<div data-tail></div>
<button type="button" data-src="./dist/img/1.jpg">
text 1
</button>
<button type="button" data-src="./dist/img/2.jpg">
text 2
</button>
<button type="button" data-src="./dist/img/3.jpg">
text 3
</button>
JS:
const tail = document.querySelector('[data-tail]')
window.addEventListener('mouseenter', onEnter)
window.addEventListener('mousemove', onMove)
onEnter(e) {
let img = new Image()
tail.appendChild(img)
gsap.to(tail, {
autoAlpha: 1,
duration: 0.25
})
}
onMove(e) {
const el = e.target
if (el.type == 'button') {
const img = tail.querySelector('img')
img.src = el.dataset.src
}
gsap.to(tail,{
x:e.clientX,
y:e.clientY
})
}
2
Answers
Yoy may check if the
src
attribute is already set or not, for example:You can use the
mouseover
event, which bubbles: