2
scope.offCanvasShow = function ($event) {
$('.app-off-canvas-menu').toggleClass('show');
// Prevents default functionality of a element
// In this instance, this prevents the anchor from reloading the page on click.
$event.preventDefault();
$event.stopPropagation();
$(document).one('touchstart click', offCanvasHandler);
/**
* Checks to see if the event target isn't .off-canvas-menu or has off-canvas-menu as a parent then removes the
* show class.
* @param event - is the event of the function
*/
function offCanvasHandler(event) {
console.log('hello');
if (!$(event.target).closest('.app-off-canvas-menu').length) {
$('.app-off-canvas-menu').removeClass('show');
$(document).off('touchstart click', offCanvasHandler);
} else {
$(document).one('touchstart click', offCanvasHandler);
}
}
};
这是一个简单的关闭帆布下拉菜单。我有这个冒泡的奇怪问题。我认为我修复了.one()
函数。
如果该类应用关闭帆布菜单上单击几次,然后离开菜单打开,单击菜单关闭这就是我想要的菜单之外。
但是,一旦我点击菜单外部,看起来控制台日志会运行多次,具体取决于我点击应用程序画布菜单汉堡包的次数。
任何人都可以看到什么我的代码区分明显?
它值得指出的是,我使用的角度,从而有可能我也去了解这个以不同的方式。
也许我有错误理解,但我相信,这是我想要的。我只想要.one()函数运行一次。我不确定你的解决方案是什么。 –
传奇,完美答案。谢谢 –