我的代码库中有这个函数:
let touch = true;
function init() {
let firstMousemoveHasOccured = false;
$(window).on('mousemove.touchdetection', () => {
if (firstMousemoveHasOccured) {
touch = false;
$(window).off('mousemove.touchdetection touchend.touchdetection');
} else {
firstMousemoveHasOccured = true;
}
});
$(window).on('touchend.touchdetection', () => {
touch = true;
$(window).off('mousemove.touchdetection touchend.touchdetection');
});
}
事件mousemove.touchdetection
不是标准事件,那么这是从哪里来的?
这些是 jQuery 命名空间事件
事件名称mousemove
的第一部分是触发时调用回调的事件。第二部分touchdetection
毫无意义,除了它允许您轻松关闭特定类或mousemove
事件组的机制。
$(document).off('mousemove'); //turns off all callbacks attached to the `mousemove` event.
$(document).off('mousemove.touchdetection'); //turns of all callbacks attached to the mousemove event that have been attached with the touchdetection namespace
正如您将从阅读 API 文档中看到的那样,这样做的目的是允许您轻松修改应用程序中的侦听器,而不会影响第三方代码附加的侦听器。