使用汉堡包标签设置响应式下拉导航

时间:2015-12-16 16:09:26

标签: jquery html css responsive-design nav

我是一名设置响应式导航的业余爱好者,在网上查找教程似乎并不能指导我在尝试时想要的最终结果。见截图:

screen shot

这是一个粗略的想法,我希望导航在某个断点处表现,但我不知道如何使这个想法发挥作用。有人可以帮助我为此设置一个基础或指导我一个有用的教程,我需要的一切(js和css媒体查询)?谢谢。

2 个答案:

答案 0 :(得分:0)

作为一个概念,可以像这样使用smt:

    * {
  box-sizing: border-box
}
input {
  display: none;
}
label {
  display: inline-block;
  cursor: pointer;
  padding: 10px;
}

.hamb {
  display: none;
}

label:after {
  display: table;
  content: '';
  
}

ul {
  padding: 0;
  margin: 0;
  list-style: none;  
}
.navi {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-flex-flow: row wrap;
      -ms-flex-flow: row wrap;
          flex-flow: row wrap;
  -webkit-box-pack: end;
  -webkit-justify-content: flex-end;
      -ms-flex-pack: end;
          justify-content: flex-end;
  border-bottom: 2px solid;
  padding-bottom: 20px;
  
}
.navi > li {
  padding: 10px ;
  border-right: 2px solid #fff;
  position: relative;
  background: #f2f2f2;
  margin: 2px;
  border: none;
  }
.navi > li > ul {
  position: absolute;
  left: 0;
  top: 100%;
  visibility: hidden;
  opacity: 0;
  background: #fff;
}

.navi li:hover ul {
  visibility: visible;
  opacity: 1;
 
}
@media screen and (max-width: 800px) {
 .hamb {
  display: block;
  } 

  input + section {
 display: none
}
  input:checked + section {
 display: block
}
  .navi {
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -webkit-flex-direction: column;
        -ms-flex-direction: column;
            flex-direction: column;
     -webkit-box-align: end;
     -webkit-align-items: flex-end;
         -ms-flex-align: end;
             align-items: flex-end;
  }
  .navi > li {
    
  }
  .navi > li > ul {
      left: calc(-100% + 4px);
    top: 0px;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<div class="hamb" style="text-align: right"><label for="menu"><i class="fa fa-bars fa-2x"></i></label></div>

<input type="checkbox" id="menu">
<section>
<ul class="navi">
<li>Link 1
  <ul>
    <li>Link 1.1
      <li>Link 1.2
        <li>Link 1.3
          
  </ul>
  </li>
  <li>Link 2 
    <ul>
    <li>Link 2.1
      <li>Link 2.2
        <li>Link 2.3
          
  </ul>
    <li>Link 3
     <li>Link 4
      <li>Link 5
  
</ul>
<section>

<强> UPD 玩风格,使其正常工作。我稍后会做出最好的答案。

答案 1 :(得分:0)

这是一个很棒的教程,可以根据你想要的疯狂程度来开发几种类型的响应式菜单。 http://responsivenavigation.net/index.html我希望这会帮助你。

这是另一个例子

&#13;
&#13;
jQuery( document ).ready( function( $ ) {

	var $menu = $('#menu'),
	  $menulink = $('.menu-link'),
	  $menuTrigger = $('.has-submenu > a');

	$menulink.click(function(e) {
		e.preventDefault();
		$menulink.toggleClass('active');
		$menu.toggleClass('active');
	});

	$menuTrigger.click(function(e) {
		e.preventDefault();
		var $this = $(this);
		$this.toggleClass('active').next('ul').toggleClass('active');
	});

});
&#13;
.example-header {
    width: 100%;
    background-color: #fff;
    z-index: 99;
    position: relative;
}

.container {
    max-width: 1000px;
    width: 96%;
    margin: 0px auto;
    position: relative;
}

.menu-link { display: none;}
.menu { 
  float: left;
  -webkit-transition: all 0.3s ease;  
  -moz-transition: all 0.3s ease;
  -ms-transition: all 0.3s ease;
  transition: all 0.3s ease; 
}
.menu ul { 
  padding: 0px;
  margin: 0px;
  list-style: none;
  position: relative;
  display: inline-table;
}
.menu > li > ul.sub-menu {
	min-width: 10em;
	padding: 4px 0;
	background-color: #f4f4f4;
	border: 1px solid #CCC;
}
.menu ul li { padding: 0px; }
.menu > ul > li { display: inline-block; }
.menu ul li a { display: block; text-decoration: none; color: #000; font-size: .9em; }
.menu ul li > a { height:58px; padding: 19px 12px; }
.menu ul ul { 
  display: none; 
  position: absolute; 
  top:100%;
  min-width: 160px;
  background-color: #f4f4f4;
  border: 1px solid #CCC;
}
.menu ul li:hover > ul { display: block; }
.menu ul ul > li { position: relative; }
.menu ul ul > li a { padding: 5px 15px 5px 10px; height: auto; background-color: #f4f4f4; }
.menu ul ul > li a:hover { background-color: #42BBA3; color: #fff; }
.menu ul ul ul { position: absolute; left: 100%; top:0; }

@media all and (max-width: 768px) {
  .example-header .container { width: 100%; }
  
  a.menu-link { display: block; color: #fff; background-color: #333; float: right; text-decoration: none; padding: 19px 10px;}
  .menu { clear: both; min-width: inherit; float: none; }
  .menu, .menu > ul ul { overflow: hidden; max-height: 0; background-color: #f4f4f4; }
  .menu > li > ul.sub-menu { padding: 0px; border: none; }
  .menu.active, .menu > ul ul.active { max-height: 55em; }
  .menu ul { display: inline; }
  .menu > ul { border-top: 1px solid #808080; }
  .menu li, .menu > ul > li { display: block; }
  .menu li a { color: #000; display: block; padding: 0.8em; border-bottom: 1px solid #808080; position: relative; }
  .menu li.has-submenu > a:after {
    content: '+';
    position: absolute;
    top: 0;
    right: 0;
    display: block;
    font-size: 1.5em;
    padding: 0.55em 0.5em;
  }
  .menu li.has-submenu > a.active:after {
    content: "-";
  }
  .menu ul ul > li a { background-color: #e4e4e4; height:58px; padding: 19px 18px 19px 30px; }
  .menu ul ul, .menu ul ul ul { display: inherit; position: relative; left: auto; top:auto; border:none; }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="example-header cf">

	<div class="container">
		
		<a class="menu-link" href="#menu">Menu</a>
		
		<nav id="menu" class="menu">
			<ul>
	        	<li class="has-submenu"><a href="#">For Facilities</a>
	        		<ul class="sub-menu">
	        			 <li><a href="#">Artificial Turf</a></li>
	        			 <li class="has-submenu">
	        				<a href="#">Batting Cages</a>
	        				<ul>
	        					<li><a href="#">Indoor</a></li>
	        					<li><a href="#">Outdoor</a></li>
	        				</ul>
	        			 </li>
	        			 <li><a href="#">Benches &amp; Bleachers</a></li>
	        			 <li><a href="#">Communication Devices</a></li>
	        			 <li><a href="#">Dugouts</a></li>
	        			 <li><a href="#">Fencing &amp; Windscreen</a></li>
	        			 <li><a href="#">Floor Protectors</a></li>
	        			 <li><a href="#">Foul Poles</a></li>
	        			 <li><a href="#">Shade Structures</a></li>
	        		</ul>
	        	</li>
	        	<li class="has-submenu"><a href="#">Field Maintenance</a>
	        		<ul class="sub-menu">
	        			<li><a href="#">All-in-One Team Cart</a></li>
	        			<li><a href="#">Air &amp; Electrical Reels</a></li>
	        			 <li><a href="#">Field Drags</a></li>
	        			 <li class="has-submenu">
	        				<a href="#">Field Marking Equipment</a>
	        				<ul>
	        					<li><a href="#">Batter's Box Templates</a></li>
	        					<li><a href="#">Dryline Markers</a></li>
	        					<li><a href="#">Field Paint</a></li>
	        					<li><a href="#">Field Sprayers</a></li>
	        					<li><a href="#">Stencils</a></li>
	        				</ul>
	        			 </li>
	        			 <li class="has-submenu">
	        				<a href="#">Field Tarps</a>
	        				<ul>
	        					<li><a href="#">Area Tarps</a></li>
	        					<li><a href="#">Growth Covers / Protectors</a></li>
	        					<li><a href="#">Infield Tarps</a></li>
	        					<li><a href="#">Tarp Accessories</a></li>
	        				</ul>
	        			 </li>
	        			 <li><a href="#">Hand Tools</a></li>
	        			 <li class="has-submenu">
	        				<a href="#">Irrigation, Hoses, Nozzles</a>
	        				<ul>
	        					<li><a href="#">Hoses &amp; Hose Reels</a></li>
	        					<li><a href="#">Irrigation</a></li>
	        					<li><a href="#">Nozzles</a></li>
	        				</ul>
	        			 </li>
	        			 <li><a href="#">Layout &amp; Measurement Tools</a></li>
	        			 <li><a href="#">Moisture Removal</a></li>
	        			 <li><a href="#">Mound &amp; Home Plate Fortification</a></li>
	        			 <li><a href="#">Mowers &amp; Stripers</a></li>
	        		</ul>
	        	</li>
	        	<li class="has-submenu"><a href="#">Game-Practice Equipment</a>
	        		<ul class="sub-menu">
	        			 <li class="has-submenu">
	        				<a href="#">Baseball - Softball</a>
	        				<ul>
	        					<li><a href="#">Base Accessories</a></li>
	        					<li><a href="#">Bases &amp; Home Plates</a></li>
	        					<li><a href="#">Game Accessories</a></li>
	        					<li><a href="#">Pitching Rubbers</a></li>
	        				</ul>
	        			 </li>
	        			 <li class="has-submenu">
	        				<a href="#">Batting Practice Equipment</a>
	        				<ul>
	        					<li><a href="#">Backstops</a></li>
	        					<li><a href="#">Infield Screens</a></li>
	        					<li><a href="#">Jugs Pitching Machines</a></li>
	        					<li><a href="#">Turf Mats</a></li>
	        					<li><a href="#">Turf Protectors</a></li>
	        					<li><a href="#">Replacement Accessories</a></li>
	        				</ul>
	        			 </li>
	        			 <li class="has-submenu">
	        				<a href="#">Batting Cages</a>
	        				<ul>
	        					<li><a href="#">Indoor</a></li>
	        					<li><a href="#">Outdoor</a></li>
	        				</ul>
	        			 </li>
	        			 <li class="has-submenu">
	        				<a href="#">Portable Mounds</a>
	        				<ul>
	        					<li><a href="#">Batting Practice Mounds</a></li>
	        					<li><a href="#">Game Mounds</a></li>
	        					<li><a href="#">Practice Mounds</a></li>
	        				</ul>
	        			 </li>
	        			 <li class="has-submenu">
	        				<a href="#">Football</a>
	        				<ul>
	        					<li><a href="#">First Down Markers</a></li>
	        					<li><a href="#">Football Accessories</a></li>
	        					<li><a href="#">Football Goalposts</a></li>
	        				</ul>
	        			 </li>
	        			 <li class="has-submenu">
	        				<a href="#">Soccer</a>
	        				<ul>
	        					<li><a href="#">Soccer Goals</a></li>
	        					<li><a href="#">Soccer Accessories</a></li>
	        				</ul>
	        			 </li>
	        			 
	        		</ul>
	        	</li>
	        	<li class="has-submenu"><a href="#">Training &amp; Conditioning</a>
	        		<ul class="sub-menu">
	        			 <li><a href="#">Ladders &amp; Sticks</a></li>
	        			 <li><a href="#">Hurdles</a></li>
	        			 <li><a href="#">Training Accessories</a></li>
	        			 <li><a href="#">Smart-Cart Training System</a></li>
	        		</ul>
	        	</li>
	        	<li class="has-submenu"><a href="#">Books-Videos</a>
	        		<ul class="sub-menu">
	        			 <li><a href="#">Field Design &amp; Maintenance</a></li>
	        			 <li><a href="#">Turf Management</a></li>
	        			 <li><a href="#">Training</a></li>
	        		</ul>
	        	</li>
	        </ul>
		</nav>
	
	</div>

</header>
&#13;
&#13;
&#13;