jQuery: How do I hide a dropdown menu ONLY at mobile screen resolution?

元气小坏坏 提交于 2019-12-13 07:21:37

问题


I'm creating a CSS responsive site.

How can I go about hiding the dropdown menu at mobile screen resolution? And having it show up again when at larger resolutions? (I'm new to jQuery)

This is my html:

<div id="menuContainer">

  <ul id="menu">

     <li id="about">

     <a href="about.html">About</a>

          <ul class="dropdown">

             <li>
               <a href="#">Link 1</a>
             </li>

             <li>
               <a href="#">Link 2</a>
             </li>

             <li>
               <a href="#">Link 3</a>
             </li>

          </ul>

      </li>

  </ul>

</div>

And my jQuery:

$('#menu li ul').css({
    display: "none",
  });
      $('#menu li').hover(function() {
        $(this)
          .find('ul')
          .stop(true, true)
          .slideDown('fast');
      }, function() {
        $(this)
          .find('ul')
          .stop(true,true)
          .fadeOut('fast');
      });

回答1:


Why not use Media Queries

@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* Styles */
/* Do whatever you want for this resolution */
}

As Of your comment above
You can try this

$(window).resize(function() {
  var windowWidth = $(window).width(); //retrieve current window width
  var windowHeight = $(window).height(); //retrieve current window height

  // show/hide dropdown based on height/width values
});



回答2:


Try using a media query in your CSS

@media only screen and (max-device-width: 480px) {
    .dropdown {  display: none; }

}

This means it only applies display: none to to that class when the screen size is less than 480px

Read this article to learn more - http://css-tricks.com/css-media-queries/ - these are at the heart of making a website responsive.




回答3:


This may not be what you are looking for, but what I've used for dynamic page sizes is Twitter's Bootstrap library.

It's a free to use API.

Otherwise I don't have the technical know how to help you with the JQuery implementation.



来源:https://stackoverflow.com/questions/15558692/jquery-how-do-i-hide-a-dropdown-menu-only-at-mobile-screen-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!