我的代码库中有这个功能:
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
不是标准事件,所以它来自何处?
答案 0 :(得分:2)
事件名称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文档中看到的那样,目的是允许您轻松修改应用程序中的侦听器,而不会影响第三方代码附加的侦听器。