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()函數運行一次。我不確定你的解決方案是什麼。 –
傳奇,完美答案。謝謝 –