简体   繁体   English

jQuery window.width'if''else if''else'陈述不起作用

[英]jQuery window.width 'if' 'else if' 'else' statment not working

function changeDrop() {
 var windowSize = $(window).width();

    if (windowSize > 450) {

              $('.menu-361').hover(
      function() {
            $('.menu-361 ul').show();
      },

      function() {
            $('.menu-361 ul').hide();
      });

        console.log("screen width is greater than 450px");

    }
    else if (windowSize <= 450) {

        $('.menu-361').on('hover', function () {
           //mousehover
         } , function(){
           //mouseout
         } );


        $('#dropbutton').click(function() {
        $('.menu-361 ul').toggle();
        $('#dropbutton p').toggle();
        });

        console.log("screen width is less than 450px");
    }

    else {}

 }

  changeDrop();
  $(window).resize(changeDrop);

After the if statment is true the bit of code is loaded in the memory i think. 在if陈述为真之后,我认为代码段已加载到内存中。 And when the window.width is <= 450 the code from the if statment or > 450 still runs. 当window.width <= 450时,如果if语句或> 450的代码仍在运行。 Any idea how to make this work? 任何想法如何使这项工作?

http://tidalbania.com/tidnew/ http://tidalbania.com/tidnew/

The link in witch the demo is on the real site! 该演示中的女巫链接位于实际站点上!

If you need a fiddle i can update the question. 如果您需要小提琴,我可以更新问题。

Well for starters, your code is essentially if A, else if not A, else which is redundant. 对于初学者来说,您的代码本质上是if A, else if not A, else为多余。 Just if A, else is fine. if A, else都没问题。

That aside, you are attaching a new event handler for every single time your code runs. 除此之外,您的代码每次运行都会附加一个新的事件处理程序。 That's a lot of event handlers! 有很多事件处理程序! You are also never detaching your handlers, instead trying to re-add blank ones. 您也永远不会分离处理程序,而是尝试重新添加空白处理程序。

Take a moment to learn how event handlers work ^_^ 花一点时间来了解事件处理程序的工作方式^ _ ^

As others have mentioned, you should not bind event handlers on every change. 正如其他人提到的那样,您不应在每次更改时都绑定事件处理程序。 This should suffice: 这样就足够了:

$('.menu-361').hover(hoverInOut);

$('#dropbutton').click(function() {
    $('.menu-361 ul').toggle();
    $('#dropbutton p').toggle();
});

function hoverInOut(event) {
    var windowSize = $(window).width();
    if (windowSize > 450) {
        if(event.type == "mouseenter")
            $('.menu-361 ul').show();
        else
            $('.menu-361 ul').hide();
    }
}
function changeDrop() {
 var windowSize = $(window).width();

    if (windowSize > 450) {
              $('.menu-361').hover(
      function() {
            $('.menu-361 ul').show();
      },

      function() {
            $('.menu-361 ul').hide();
      });

        console.log("screen width is greater than 450px");
    }

    else {          
        $('.menu-361').unbind('mouseenter mouseleave');

        $('#dropbutton').click(function() {
        $('.menu-361 ul').toggle();
        $('#dropbutton p').toggle();
        });

        console.log("screen width is less than 450px"); 
    } 
 }

 changeDrop();
 $(window).resize(changeDrop);

I think i fixed the issue adding the unbind ('mouseenter mouseleave') when width is less than 450px by removing the binded hover function. 我认为我通过删除绑定的悬停功能解决了宽度小于450px时添加取消绑定(“ mouseenter mouseleave”)的问题。

Is this the way it should be done? 这是应该做的方式吗?

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

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