내 웹 사이트가 반응적이고 좁은보기의 경우 네비게이션이 클릭하여 탐색을 표시하는 아이콘으로 전환합니다. 탐색 패널을 닫으려면 아이콘을 다시 클릭하거나 탐색 모달 바깥 쪽을 클릭합니다. 당신이이 탐색 모달를 닫 아이콘을 탭,하지만 때 탐색 모달 아무것도 외부를 눌러 때 모바일 기기에외부 터치 이벤트에서 nav 모드 닫기를 닫습니다.
$(document).mousedown(function(e) {
var clicked = $(e.target);
if (clicked.is("#navigation") || clicked.parents().is("#navigation") || clicked.is("#hamburger-nav-link")) {
return;
} else {
$("#hamburger-nav-link").removeClass("hamburger-nav-active");
$("#navigation").removeClass("mobile-nav");
}
});
(물론, 내 아이폰) 발생 :이이 탐색 모달 외부 클릭을 관리 할 수있는 JS입니다 .
나는 이벤트를 클릭하여 터치 이벤트를 매핑이 SO 질문에서 코드를 실행하려고 : JavaScript mapping touch events to mouse events
그러나이 나를 위해 작동하지 않았다.
코드 아래에 $(document).mousedown()
코드를 붙여 넣었습니다.이 코드는 모두 일반 jQuery 함수 내에 있습니다. 모든 것을 볼 수 있도록 아래 코드를 붙여 넣었습니다. 이 파일은 닫는 태그 앞에있는 모든 페이지의 끝에서 호출됩니다.
도움을 주시면 감사하겠습니다.
$ (함수() {
// Mobile nav
$("#hamburger-nav-link").click(function() {
if ($("#navigation").hasClass("mobile-nav")) {
$(this).removeClass("hamburger-nav-active");
$("#navigation").removeClass("mobile-nav");
}
else {
$(this).addClass("hamburger-nav-active");
$("#navigation").addClass("mobile-nav");
}
return false;
});
// Close modal if click event is outside of it
$(document).mousedown(function(e) {
var clicked = $(e.target);
if (clicked.is("#navigation") || clicked.parents().is("#navigation") || clicked.is("#hamburger-nav-link")) {
return;
} else {
$("#hamburger-nav-link").removeClass("hamburger-nav-active");
$("#navigation").removeClass("mobile-nav");
}
});
function touchHandler(event)
{
var touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type)
{
case "touchstart": type = "mousedown"; break;
case "touchmove": type="mousemove"; break;
case "touchend": type="mouseup"; break;
default: return;
}
//initMouseEvent(type, canBubble, cancelable, view, clickCount,
// screenX, screenY, clientX, clientY, ctrlKey,
// altKey, shiftKey, metaKey, button, relatedTarget);
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
function init()
{
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
});
감사하지만 내가 터치 이벤트를 결합하려고했다, 그래서, 여기에 대한 대답이다. 위의 내 대답을 볼 수 있습니다. –