我有连续的动画,用gsap library制作。我正在使用mouseover
/mouseout
事件来暂停/恢复此动画。在窗口调整大小事件中,我正在重新初始化。我的问题是:我需要调用removeEventListener
第二次初始化吗?
这是代码/场景:
const scroll = {
create: function (el) {
this.scrollAnimation = gsap.timeline({
repeat: -1
});
// another piece of awesome code here...
this.create__addMouseEvents(el);
},
create__addMouseEvents: function (el) {
// here, on window resize event( when i call update(), during re-initialization ), do i need to call "removeEventListener"?
el.addEventListener('mouseover', () => this.scrollAnimation.pause());
el.addEventListener('mouseout', () => this.scrollAnimation.resume());
},
update: function () {
//
// destroy old "scrollAnimation" if it's already exists
if (this.scrollAnimation) {
this.scrollAnimation.kill();
}
//
// reinit
this.init();
},
init: function () {
// some awesome code here...
this.create(el);
}
}
document.addEventListener('DOMContentLoaded', function () {
scroll.init();
});
let windowResizeTimer;
window.addEventListener('resize', function () {
clearTimeout(windowResizeTimer);
windowResizeTimer = setTimeout(function () {
scroll.update();
}, 150);
});
四季花海
相关分类