我正在做一件小事,我需要检查是否img.src
已经设置,以防止它像现在一样连续设置。问题出在我的onMove
. 我怎样才能做到这一点?
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
})
}
Yoy 可以检查该
src
属性是否已经设置,例如:更新:我想你想要这样的东西:
您可以使用该
mouseover
事件,该事件会冒泡: