如何防止在GSAP动画期间触发事件?
我有一个类似的GSAP动画:
window.addEventListener('keydown', ()=>{
GSAP.to(this.box, {
x: this.box.position.x + 4,
duration: 1,
}
});
我想通过4的倍数将框的x
位置递增,而我当前的代码上面有效,但是只有用户等待直到1秒钟才通过,然后按下另一个键。然后它进行0-> 4-> 8
等。
如果他们不等待一秒钟,则发生的是0-> 3.76-> 6.45
。这些数字会根据用户等待多长时间按下另一个键的时间而改变。这是有道理的,因为我有效地取消了动画并使用当前位置(因为1秒尚未通过,它尚未到4),然后将动画重新使用当前位置(这不是) 4)。
从本质上讲,我想防止触发“键盘”事件侦听器,直到完全完成1秒动画为止。
我该怎么做?
I have a GSAP animation like this:
window.addEventListener('keydown', ()=>{
GSAP.to(this.box, {
x: this.box.position.x + 4,
duration: 1,
}
});
I want to increment the box's x
position by multiples of 4, and my current code above works, but only if the user waits until 1 second has passed before pressing another key. Then it goes 0 -> 4 -> 8
etc.
If they don't wait a second, what happens is something like 0 -> 3.76 -> 6.45
. These numbers change depending on how long a user waits before they press another key. This makes sense because I'm effectively cancelling the animation and using the current position (since 1 second hasn't passed yet, it wouldn't get to 4 yet) and then just rerunning the animation with the current position (that wasn't 4).
Essentially, I want to prevent the 'keydown' event listener from triggered until the 1 second animation has been fully completed.
How would I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
GSAP可能不是最干净的解决方案,是一个很好的工具,并且肯定有更好的方法来处理这种情况,但这是我想到的第一种残酷方法。
编辑:
使用
ISACTIVE()
方法已经好一点了。This should work:
Probably not the cleanest solution, gsap is a great tool and surely has some better way to handle this scenario, but this is the first brutal approach that came to my mind.
EDIT:
Using
isActive()
method is already a tiny bit better.