繁体   English   中英

网站响应时,HTML / CSS导航下拉列表和子下拉列表不显示

[英]HTML/CSS Navigation dropdown and sub dropdown not showing up when the site goes responsive

网站响应时,导航栏出现问题。 单击下拉菜单时,div似乎消失了。 不知道这是否与CSS或jQuery有关。

演示在这里: https : //jsfiddle.net/0nxfys9s/


HTMl(所有li类均由WordPress生成,对不起)

<nav class="clearfix">

            <div class="menu-main-menu-container"><ul id="menu-main-menu-1" class="menu"><li class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-86"><a href="#">Home</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-148"><a href="#">other tab</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-149"><a href="#">other tab</a>
<ul class="sub-menu">
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-320"><a href="#">other sub tab</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-321"><a href="#">other sub tab</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-322"><a href="#">other sub tab</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-323"><a href="#">other sub tab</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-326"><a href="#">other sub tab</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-324"><a href="#">other sub tab</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-327"><a href="#">other sub tab</a></li>
    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-328"><a href="#">other sub tab</a></li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-136"><a href="#">other tab</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-147"><a href="#">other tab</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-7"><a href="#">other tab</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-58"><a href="#">other tab</a></li>
</ul></div>


 <a href="#" id="pull">Menu</a>
            </nav>

的CSS

nav {
  height: 40px;
  width: 100%;
  color: #fff;
  background: #86c024;
  font-size: 11pt;
  position: relative;
}
nav a {
  color: #fff;
  display: inline-block;
  width: auto;
  text-align: center;
  text-decoration: none;
  line-height: 40px;

}
nav ul  {
  padding: 0;
  margin: 0 auto;
  max-width:1000px;
  width: 100%;
}
nav li  {
  display: inline;
  float: left;
  height: 40px; /* this should be the same as your #main-nav height */
  position: relative; /* this is needed in order to position sub menus */
}
nav li a  {
  padding: 0 15px;
  border-right: 1px solid #fff;
  box-sizing:border-box;
  -moz-box-sizing:border-box;
  -webkit-box-sizing:border-box;
}
nav  a:hover {
  background: #2098d1;
  color: white;
  width: 100%;
}
nav ul ul { /* this targets all sub menus */
  display: none; /* hide all sub menus from view */
  position: absolute;
  top: 40px; /* this should be the same height as the top level menu -- height + padding + borders */
}
nav li li a {
  border:none;
  font-size: 10pt;
}
nav ul ul li { /* this targets all submenu items */
  float: none; /* overwriting our float up above */
  width: 100px; /* set to the width you want your sub menus to be. This needs to match the value we set below */
}
nav ul ul li a { /* target all sub menu item links */
  padding: 5px 10px; /* give our sub menu links a nice button feel */
}
nav ul li:hover > ul {
  display: inline-block; /* show sub menus when hovering over a parent */
  background: gray;
  text-align: center;
  border: 0;
  z-index: 100;

}
nav li a:link, a:visited {
    color: white;
}
nav li:last-child a {
    border-right: 0;
}
nav ul li.current-menu-item a:link,
nav ul li.current-menu-item a:visited,
nav ul li.current-page-ancestor a:link,
nav ul li.current-page-ancestor a:visited {
  font-weight: bold;
  background: #2098d1;
  color: white;
}

nav #pull {
    display: none;
}  
@media screen and (max-width: 600px) {
    nav { 
    height: auto;
    }
    nav ul {
    width: 100%;
    display: block;
    height: auto;
    }
    nav li {
    width: 100%;
    float: left;
    position: relative;
    }

    nav li a {
    border-bottom: 1px solid #fff;
    border-right: 1px solid #fff;
    }
    nav a {
    text-align: left;
    width: 100%;
    text-indent: 25px;
    }
}
@media only screen and (max-width : 600px) {

    nav {
    border-bottom: 0;
    }
    nav ul {
    display: none;
    height: auto;
    }
    nav #pull {
    display: block;
    background: #86c024;
    width: 100%;
    position: relative;
    }
    nav #pull:after {
    content:"";
    background: red;
    width: 30px;
    height: 30px;
    display: inline-block;
    position: absolute;
    right: 15px;
    top: 10px;
    }
}
@media only screen and (max-width : 320px) {

    nav li {
    display: block;
    float: none;
    width: 100%;
    }
    nav li a {
    border-bottom: 1px solid #576979;
    }

}

jQuery的

    $(function() {
        var pull        = $('#pull');
            menu        = $('nav');
            menuHeight  = menu.height();

        $(pull).on('click', function(e) {
            e.preventDefault();
            menu.slideToggle();
        });

        $(window).resize(function(){
            var w = $(window).width();
            if(w > 320 && menu.is(':hidden')) {
                menu.removeAttr('style');
            }
        });
    });

正如@Moob所说, menu.slideToggle(); 单击#pull元素时会隐藏整个nav元素。

要获得理想的效果,请执行此操作

menu = $('nav .menu');

小提琴

更新

当屏幕尺寸调整为小于或等于320像素时,将显示菜单项,因为您的代码中有此CSS规则

@media only screen and (max-width : 320px) {

    nav li {
        display: block;
        float: none;
        width: 100%;
    }
    nav li a {
        border-bottom: 1px solid #576979;
    }

}

之间320px600px的菜单项所涵盖nav #pull元素,你让它display: block ,因此它转向display:inline-block ,它会正常工作。

更新的小提琴

暂无
暂无

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

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