简体   繁体   English

在特定媒体查询中防止jQuery悬停事件

[英]Prevent jQuery hover event at specific media query

I've the following working example showing a basic menu/nav system that uses at hover event for screen widths greater than 480px and and click/expand event for widths less than 480px: 我有以下工作示例,展示了一个基本菜单/导航系统,该菜单/导航系统在发生悬停事件时使用的屏幕宽度大于480px,在单击/展开事件时使用的宽度小于480px:

my website 我的网站

If you reduce the screen size down from greater than 480px you'll see I'm not getting the desired effect, both the hover and click events are being triggered. 如果将屏幕尺寸从大于480像素减小到小于480像素,您会看到我没有获得预期的效果,同时触发了悬停和click事件。

I'm new to jQuery so any help on how to prevent this would be great!!!! 我是jQuery新手,所以关于如何防止这种情况的任何帮助都将很棒!!!!

My code up to now: 到目前为止,我的代码:

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();
});

This seems to of fixed the problem, but is no doubt more of a hack: 这似乎解决了问题,但毫无疑问更多是黑客:

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM