简体   繁体   English

jQuery窗口调整大小不起作用

[英]jquery window resize not working

I'm trying to make a menu where sub menus are opened on hover but when on mobile (width < 800) sub menus should be triggered on click. 我正在尝试制作一个菜单,其中悬停时打开子菜单,但是在移动(宽度<800)上单击时应触发子菜单。

I made this jquery which is not working, it works on hover but when you re-size window its still triggered on hover not on click. 我做了这个无法正常工作的jQuery,它可以在悬停时工作,但是当您调整窗口大小时,它仍然会在悬停时触发而不是单击。

jquery: jQuery的:

$(document).ready(function () {

    $(".menu ul li").hover(function () {
        $('li > ul').not($(this).children("ul").slideToggle(200)).hide();
    });

});
$(window).resize(function () {
    if ($(window).width() <= 800) {
        $(".menu ul li").click(function () {
            $('li > ul').not($(this).children("ul").slideToggle(200)).hide();
        });
    }
}); 

CSS: CSS:

.menu {
  width: 100%;
  background: #333;
}
.menu:before,
.menu:after {
  content: "";
  display: table;
}
.menu:after {
  clear: both;
}
.menu > ul {
  width: 100%;
  list-style: none;
  padding: 0;
  margin: 0;
}
.menu > ul > li {
  float: left;
  background: #e9e9e9;
  padding: 0;
  margin: 0;
}
.menu > ul > li > a {
  text-decoration: none;
  padding: 1.5em 3em;
  display: block;
  outline: 0 none;
}
.menu > ul > li > ul {
  display: none;
  background: #333;
  padding: 20px 30px;
  position: absolute;
  width: 100%;
  z-index: 99;
  left: 0;
  color: #fff;
  margin: 0;
}
@media only screen and (max-width: 767px) {
  .menu ul > li {
    float: none;
    width: 100%;
  }
  .menu ul > li > ul {
    position: relative;
  }
}

HTML: HTML:

<div class="menu">
        <ul>
            <li><a href="#">Home</a>
                <ul>
                    This is also mega menu
                </ul>
            </li>
            <li><a href="#">Who are we?</a>
                <ul>
                    This is mega menu
                </ul>
            </li>
            <li><a href="#">Services</li></a>
                <li><a href="#">Contact</li></a>
        </ul>
    </div>

How can i do with jquery to change from on hover to on click when the window is resized or its below 800px? 当窗口调整大小或低于800px时,如何使用jquery从悬停更改为单击?

use this script 使用这个脚本

$(document).ready(function () {

       $(".menu ul li").hover(function () {
          if ($(window).width() > 800) {

                $('li > ul').not($(this).children("ul").slideToggle(200)).hide();
          }

        });
         $(".menu ul li").click(function () {
          if ($(window).width() <= 800) {

                $('li > ul').not($(this).children("ul").slideToggle(200)).hide();
          }
        });

});

You can make use of .off() method to unbind the events attached and make sure to bind the other events with .on() : 您可以使用.off()方法解除绑定的事件的绑定,并确保将其他事件与.on()绑定:

$(document).ready(function() {
  $(".menu ul li").on('mouseenter', function() {
    $('> ul', this).stop().slideToggle(200); // this would do, as per context applied with "this"
  });

  $(window).resize(function() {
    if ($(window).width() <= 800) {
      $(".menu ul li").off().on('click', function() {
        $('> ul', this).stop().slideToggle(200); // this would do, context applied with "this"
      });
    }
  }).resize(); // trigger the resize() event on dom ready.
}); // <-----------wrap everything in the doc ready block

Plnkr demo Plnkr演示


Even a better one: 更好的一个:

// make a global function which toggles the submenus.
function submenutoggler() {
  $('> ul', this).stop().slideToggle(200); // .stop() will tackle the multiple clicks.
}

$(document).ready(function() {    
  $(window).resize(function() {
    if ($(window).width() <= 800) {
      $(".menu ul li").off().on('click', submenutoggler); // if mobile bind click
    }else{
      $(".menu ul li").on('mouseenter', submenutoggler); // if desktop bind mouseenter
    }
  }).resize(); // trigger the resize to execute at dom ready.

}); // <----wrap it within dom ready block.

Updated plnkr. 更新了plnkr。

use like this 这样使用

$(document).ready(function () {

    $(".menu ul li").on('hover',function () {
        $('li > ul').not($(this).children("ul").slideToggle(200)).hide();
    });

});
$(window).on('resize'function () {
    if ($(window).width() <= 800) {
        $(".menu ul li").on('click',function () {
            $('li > ul').not($(this).children("ul").slideToggle(200)).hide();
        });
    }
}); 

You need to unbind the event before the binding new event. 您需要在绑定新事件之前取消绑定该事件。 I have modified your code see the fiddle here: http://jsfiddle.net/sachinkk/nu1dbp5a/ 我已经修改了您的代码,请参见此处的小提琴: http : //jsfiddle.net/sachinkk/nu1dbp5a/

Also, made the changes in HTML. 另外,对HTML进行了更改。 Hope this will helpful. 希望这会有所帮助。

Make sure while loading the page, the size of a page should be more than 800px. 确保在加载页面时,页面大小应大于800px。 (events are binding after page resizes) (页面大小调整后,事件具有约束力)

 $(document).ready(function () { $(".menu ul li").hover(function () { $('li > ul').not($(this).children("ul").slideDown(200)).hide(); },function(){ $('li > ul').not($(this).children("ul").slideUp(200)).hide(); }); }); $(window).resize(function () { if ($(window).width() <= 800) { $(".menu ul li").unbind('hover'); $(".menu ul li").unbind('click'); $(".menu ul li").click(function (e) { $('li > ul').not($(this).children("ul").slideToggle(200)).hide(); }); } else{ if ($(window).width() > 800){ $(".menu ul li").unbind('click'); $(".menu ul li").unbind('hover'); $(".menu ul li").hover(function () { $('li > ul').not($(this).children("ul").slideDown(200)); },function(){ $('li > ul').not($(this).children("ul").slideUp(200)); }); } } }); 
 .menu { width: 100%; background: #333; } .menu:before, .menu:after { content: ""; display: table; } .menu:after { clear: both; } .menu > ul { width: 100%; list-style: none; padding: 0; margin: 0; } .menu > ul > li { float: left; background: #e9e9e9; padding: 0; margin: 0; } .menu > ul > li > a { text-decoration: none; padding: 1.5em 3em; display: block; outline: 0 none; } .menu > ul > li > ul { display: none; background: #333; padding: 20px 30px; position: absolute; width: 100%; z-index: 99; left: 0; color: #fff; margin: 0; } @media only screen and (max-width: 767px) { .menu ul > li { float: none; width: 100%; } .menu ul > li > ul { position: relative; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="menu"> <ul> <li><a href="#">Home</a> <ul> <li>This is also mega menu</li> </ul> </li> <li><a href="#">Who are we?</a> <ul> <li>This is mega menu</li> </ul> </li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </div> 

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

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