0

我是使用 jQuery 的新手,并尝试编写一个基本菜单系统,在屏幕宽度大于 480 像素时,通过将鼠标悬停在 li 标签上来展开子菜单,在宽度小于 480 像素时,通过单击 li 标签来展开子菜单。

我尝试使用 Modernizr 检测媒体查询的能力,但是正如您从我的尝试中看到的那样,它失败了:

工作示例。.....将鼠标悬停在名为“属性”的 li 标签上

这个问题显然与代码没有调整屏幕大小有关,因为如果您以正确的宽度刷新浏览器,它在 > 480px 和 < 480px 下工作,但是当您调整浏览器大小时,这些功能仍然会尝试触发!

我的代码:

// Main nav dropdown animation

$(document).ready(function() {
  function doneResizing() {
    if(Modernizr.mq('screen and (min-width: 481px)')) {

        $("#sub-menu").hide();

        $("#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();

      var next_move = "show";

        $("#show-investment-type-nav").click(function() {

            $('#sub-menu').slideToggle(100);

            if (next_move === "show") {
                $("body").addClass("nav-active");
                $("#site-nav #icon").empty().html("&#59236;");
                $("#site-nav #nav-margin-down").animate({"margin-top": "163"}, 100);
                next_move = "hide";
            } else {
                $("body").removeClass("nav-active");
                $("#site-nav #icon").empty().html("&#59238;");
                $("#site-nav #nav-margin-down").animate({"margin-top": "0"}, 100);
                next_move = "show";
            }
        });
    }
  }

  var id;
  $(window).resize(function() {
    clearTimeout(id);
    id = setTimeout(doneResizing, 0);
  });

  doneResizing();
});

关于为什么会发生这种情况的任何帮助都会很棒,它会帮助我学习!

干杯伙计们!

4

2 回答 2

0

试试这个,看看它是否有效。我将点击处理程序移出调整大小处理程序。在事件中绑定事件很棘手,通常没有必要。我还必须将next_move其置于全球范围内。使用将值添加到菜单元素data()可能是一个更好的选择:

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("&#59236;");
                $("#site-nav #nav-margin-down").animate({
                    "margin-top": "163"
                }, 100);
                next_move = "hide";
            } else {
                $("body").removeClass("nav-active");
                $("#site-nav #icon").empty().html("&#59238;");
                $("#site-nav #nav-margin-down").animate({
                    "margin-top": "0"
                }, 100);
                next_move = "show";
            }
        }
    });

    function doneResizing() {
        if (Modernizr.mq('screen and (min-width: 481px)')) {
            $("#sub-menu").hide();
            $("#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();
});
于 2013-09-23T14:54:49.773 回答
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("&#59236;");
          $("#site-nav #nav-margin-down").animate({"margin-top": "163"}, 100);
          next_move = "hide";
        } else {
          $("body").removeClass("nav-active");
          $("#site-nav #icon").empty().html("&#59238;");
          $("#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();
});
于 2013-09-23T16:57:29.603 回答