问题描述
我正在用 js 编写滚动同步模块,并且遇到了一些非常意外的行为,即滚动事件的触发方式。从代码开始:
const attach = (element) => {
// synchronizedElements is in scope because of closures
const onScrollListener = () => {
// first detach all other listeners
synchronizedElements.current.forEach((listener,el) =>
if(el !== element) el.removeEventListener('scroll',listener)
);
// synchronize all of the other elements with the leader
syncFollowers({leader: element,synchronizedElements});
// then re-attach all of the other listeners
synchronizedElements.current.forEach((listener,el) =>
if(el !== element) el.addEventListener('scroll',listener)
);
}
synchronizedElements.current.add(element,onScrollListener);
element.addEventListener('scroll',synchronizedElements);
}
有了这个,我希望滚动第一个元素来调用它的侦听器。它将承担领导者的角色并移除所有其他元素; (followers) 所以它可以移动他们的滚动位置而无需他们触发 onScrollListeners 响应。然后重新吸引听众,让他们再次成为领导者。这很关键,因此关注者不会尝试做完全相同的事情。
相反,在领导者完成后,追随者的听众也会立即被调用。我想要做的就是在领导者滚动时绕过非领导者的滚动事件。
我想知道 removeEventListener
+ addEventListener
是否已排队并且直到整个函数完成后才真正执行,因此当领导者移动它们时,追随者仍然附有他们的听众。有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)