如何根据智能手机屏幕尺寸制作下拉菜单?

时间:2016-11-23 19:11:31

标签: css html5

我想让我的下拉菜单响应智能手机屏幕尺寸@media屏幕和(最大宽度:360px) 有人可以告诉我这是怎么可能的 我目前的菜单html5标记是:

<div id="menu" >
<nav id="primary_nav_wrap">
<ul>
<li class="currentmenuitem"><a href="#">Home</a></li>
<li><a href="aboutus.html">about us</a></li>
<li><a href="#">day course</a>
<ul>
  <li class="submenu"><a href="threedaycourse.html">3 Day Course
  </a></li>
  <li class="submenu"><a href="#">6 Day Course</a></li>
  </ul></li>
  <li><a href="#">short course</a>
  <ul>
  <li class="submenu"><a href="#">Pasta course</a></li>
  <li class="submenu"><a href="#">Pizza course</a></li>
  <li class="submenu"><a href="#">Fettucine course</a></li>
  </ul></li>
  <li><a href="#">Tours</a></li>
  <li><a href="#">recipes</a></li>
  <li><a href="#">reviews</a></li>
  <li><a href="contact.html">Contact Us</a></li>
  <li class="last"><a href="location.html">Location</a></li>
  </ul>
  </nav></div>

css样式如下:

 #menu{
    width:960px;
    margin-left:auto;
    margin-right:auto;
    text-align:center;
    position: relative;
    z-index:1;}
    #primary_nav_wrap{
    margin-left:auto;
    margin-right:auto;
    margin-top:5px;
    text-align:center;
    /*margin-left:50px; */}

    #primary_nav_wrap ul{
    list-style:none;
    position:relative;
    /*float:left;*/
    text-align: center;
    margin:0;
    padding:0;}


    #primary_nav_wrap ul a{
    /*display:block;*/
    color:#333;
    text-decoration:none;
    font-weight:500;
    font-size:14px;
    text-transform: uppercase;
    line-height:32px;
    padding-top:10px;
    font-family:'Raleway',sans-serif; 
    display: inline-block;}

    #primary_nav_wrap ul li{
    display:inline-block;
    position:relative;
    /*float:left;*/
    margin:0px;
    padding:0px;}

     #primary_nav_wrap ul li:after{
     margin-left: 10px;
     margin-right: 10px;
     content: " | ";}

     #primary_nav_wrap ul li.last:after{content: none !important;}


    #primary_nav_wrap ul li li.submenu:after{content: none !important;}

    #primary_nav_wrap ul li a:hover{
    background:#efefef;}

    #primary_nav_wrap ul ul{
    display:none;
    position:absolute;
    top:100%;
    left:0;
    background:#fff;
    opacity: .8;
    padding:0;}

    #primary_nav_wrap ul ul li{
    float:none;
    text-align: left;
    width:200px;}

    #primary_nav_wrap ul ul a{
    line-height:120%;
    padding:10px 15px;}

    #primary_nav_wrap ul ul ul{
    top:0;
    left:100%;}

    #primary_nav_wrap ul li:hover ul{
    display:inline-block;}

2 个答案:

答案 0 :(得分:1)

您可以检查屏幕的宽度,如果它小于一定数量(在这种情况下,500px),则更改CSS(您也可以使用高度执行此操作)

@media screen and (max-width: 500px) {
    // Change element CSS here
}

答案 1 :(得分:0)

使用在页面宽度<1时触发的媒体查询360px并更改查询中的外观。 给予bash -c 'PID=$$; echo $PID' 一个#menu会使其更具响应性

width:100%;

点击运行代码段和整页,然后调整大小以查看更改

&#13;
&#13;
@media screen and (max-width: 360px) {
   .  .  .
}
&#13;
 #menu{
    width:100%;
    margin-left:auto;
    margin-right:auto;
    text-align:center;
    position: relative;
    z-index:1;}
    #primary_nav_wrap{
    margin-left:auto;
    margin-right:auto;
    margin-top:5px;
    text-align:center;
    /*margin-left:50px; */}

    #primary_nav_wrap ul{
    list-style:none;
    position:relative;
    /*float:left;*/
    text-align: center;
    margin:0;
    padding:0;}


    #primary_nav_wrap ul a{
    /*display:block;*/
    color:#333;
    text-decoration:none;
    font-weight:500;
    font-size:14px;
    text-transform: uppercase;
    line-height:32px;
    padding-top:10px;
    font-family:'Raleway',sans-serif; 
    display: inline-block;}

    #primary_nav_wrap ul li{
    display:inline-block;
    position:relative;
    /*float:left;*/
    margin:0px;
    padding:0px;}

     #primary_nav_wrap ul li:after{
     margin-left: 10px;
     margin-right: 10px;
     content: " | ";}

     #primary_nav_wrap ul li.last:after{content: none !important;}


    #primary_nav_wrap ul li li.submenu:after{content: none !important;}

    #primary_nav_wrap ul li a:hover{
    background:#efefef;}

    #primary_nav_wrap ul ul{
    display:none;
    position:absolute;
    top:100%;
    left:0;
    background:#fff;
    opacity: .8;
    padding:0;}

    #primary_nav_wrap ul ul li{
    float:none;
    text-align: left;
    width:200px;}

    #primary_nav_wrap ul ul a{
    line-height:120%;
    padding:10px 15px;}

    #primary_nav_wrap ul ul ul{
    top:0;
    left:100%;}

    #primary_nav_wrap ul li:hover ul{
    display:inline-block;}

    #nav-toggle{
      position: fixed;
      margin: 0;
      padding: 10px;
      z-index: 99999;
      background: darkgrey;
      color: white;
      cursor: pointer;
      display: none;
    }

@media screen and (max-width: 360px) {
  
  nav{
  display: none;
  }
  
  input[type=checkbox]:checked ~ nav {
  display: block;
  }
  
  nav ul{
    width: 100%;
  }
  
  #nav-toggle{
   padding: 20px; 
   display: block;
  }
  
  nav{
    padding-top: 60px;
    width: 100%;
  }
  
  #primary_nav_wrap ul li:after{
     display: none;
  }
 
  #primary_nav_wrap ul li{
    width: 100%;
  }

}
&#13;
&#13;
&#13;