我有切换导航和功能,当您单击外部菜单(在带有模糊过滤器的div
上(导航正在关闭。我在IOS手机上遇到了问题,所以我添加了触摸事件。现在没问题了,但是还有一个问题 - 当我单击外部菜单以触发关闭功能时,下面的正文内容也在单击。如何预防这种情况?
JS代码:
var sideNav = document.getElementById('Sidenav');
var sliderFilter = document.getElementById('sliderFilter');
function touchHandler () {
if (event.target.closest("#Sidenav")) {
// This is a click inside. Do nothing, just return.
return;
}
// This is a click outside.
closeNav();
}
function openNav() {
sideNav.classList.add('open');
sliderFilter.classList.add('open');
document.addEventListener('click', touchHandler, true);
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
body.style.overflow = "hidden";
}
function closeNav() {
sideNav.classList.remove('open');
sliderFilter.classList.remove('open');
document.removeEventListener('click', touchHandler, true);
document.removeEventListener("touchstart", touchHandler, true);
document.removeEventListener("touchend", touchHandler, true);
body.style.overflow = "auto";
}
我已经删除了touchstart
事件并只留下了touchend
,它现在可以正常工作。