我的以下工作示例显示了一个基本的菜单/导航系统,它在悬停事件中使用大于480px的屏幕宽度,并且单击/展开事件的宽度小于480px:
如果您将屏幕尺寸从大于480px缩小,您将看到我没有获得所需的效果,因此触发了悬停和点击事件。
我是jQuery的新手,所以任何有关如何防止这种情况的帮助都会很棒!!!!
我的代码到目前为止:
var next_move = "show";
$(document).ready(function () {
$("#show-investment-type-nav").click(function() {
if (Modernizr.mq('screen and (max-width: 480px)')) {
$('#sub-menu').slideToggle(100);
if (next_move === "show") {
$("body").addClass("nav-active");
$("#site-nav #icon").empty().html("");
$("#site-nav #nav-margin-down").animate({"margin-top": "163"}, 100);
next_move = "hide";
} else {
$("body").removeClass("nav-active");
$("#site-nav #icon").empty().html("");
$("#site-nav #nav-margin-down").animate({"margin-top": "0"}, 100);
next_move = "show";
}
}
});
function doneResizing() {
if (Modernizr.mq('screen and (min-width: 481px)')) {
// Hide submenu
$("#sub-menu").hide();
// Reset margin for li tags if screen expanded whilst nav open
$("#site-nav #nav-margin-down").css("margin-top","0");
$("#show-investment-type-nav").hover(function() {
$(this).find("#sub-menu").stop(true, true).slideDown("fast");
}, function () {
$(this).find("#sub-menu").stop(true, true).fadeOut("fast");
});
} else if (Modernizr.mq('screen and (max-width: 480px)')) {
$("#sub-menu").hide();
next_move = "show";
}
}
var id;
$(window).resize(function () {
clearTimeout(id);
id = setTimeout(doneResizing, 0);
});
doneResizing();
});
答案 0 :(得分:0)
这似乎解决了这个问题,但无疑更像是一个黑客攻击:
var next_move = "show";
$(document).ready(function () {
// Touch device fix to prevent submenu being shown during initial load
$('#sub-menu').css("display","block");
$("#show-investment-type-nav").click(function() {
if (Modernizr.mq('screen and (max-width: 480px)')) {
$('#sub-menu').slideToggle(100);
if (next_move === "show") {
$("body").addClass("nav-active");
$("#site-nav #icon").empty().html("");
$("#site-nav #nav-margin-down").animate({"margin-top": "163"}, 100);
next_move = "hide";
} else {
$("body").removeClass("nav-active");
$("#site-nav #icon").empty().html("");
$("#site-nav #nav-margin-down").animate({"margin-top": "0"}, 100);
next_move = "show";
}
}
});
function doneResizing() {
if (Modernizr.mq('screen and (min-width: 481px)')) {
// Hide submenu
$("#sub-menu").hide();
// Reset margin for li tags if screen expanded whilst nav open
$("#site-nav #nav-margin-down").css("margin-top","0");
$("#show-investment-type-nav").hover(function() {
$(this).find("#sub-menu").stop(true, true).slideDown("fast");
}, function () {
$(this).find("#sub-menu").stop(true, true).fadeOut("fast");
});
} else if (Modernizr.mq('screen and (max-width: 480px)')) {
// Fixes issue on touch device when you touch away from expanded submenu...this took forever!!!
$('#sub-menu').slideUp(100);
$("#site-nav #nav-margin-down").animate({"margin-top": "0"}, 100);
// To make sure if nav expanded, so next_move ="hide", hover event doesn't hide() submenu
if (!Modernizr.touch) {
$("#show-investment-type-nav").hover(function() {
$("#sub-menu").hide();
if (next_move === "hide") {
$("#sub-menu").show();
}
});
}
next_move = "show";
}
}
var id;
$(window).resize(function () {
clearTimeout(id);
id = setTimeout(doneResizing, 0);
});
doneResizing();
});