在排網頁時響應式可彈出導航欄能夠增長你網頁的美觀和可讀性。在PC端時導航欄的內容能夠經過bars來顯示和隱藏,在移動端時也能夠達到一樣的效果。
效果如圖所示:
javascript
代碼以下(html):css
<!DOCTYPE html> <html> <head> <title>響應式菜單</title> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="demo1.css"/> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div class="bars active"> <span></span> <span></span> <span></span> </div> <div class="nav"> <ul> <li><a href="#">首頁</a></li> <li><a href="#">產品</a></li> <li><a href="#">關於</a></li> <li><a href="#">服務</a></li> <li><a href="#">聯繫</a></li> </ul> </div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.slim.js"></script> <script type="text/javascript"> $(".bars").click(function(){ $(".bars").toggleClass("active"); $(".nav").toggleClass("active"); }); </script>(控制鼠標點下時會顯示的內容) </body> </html>
body{ margin:0; background:black; } .bars{ width:60px; height:60px; background:white; position:fixed; top:0px; left:0px; cursor:pointer; z-index:1; border-right:1px solid rgba(0,0,0.2); } .bars span{ display:block; width:30px; height:2px; background:#262626; position:absolute; top:calc(50% - 1px); left:calc(50% - 15px); transition:.2s; } .bars span:nth-child(1){ transform:translateY(-10px); } .bars span:nth-child(3){ transform:translateY(10px); } .bars.active span:nth-child(1){ transform:translateY(0px) rotate(-45deg);(控制第一個span順時針旋轉45度) } .bars.active span:nth-child(3){ transform:translateY(0px) rotate(45deg);(控制第三個span逆時針旋轉45度) } .bars.active span:nth-child(2){ transform:translateY(-100%);(控制第二個span隱藏) opacity:0; }(這樣就能夠造成一個關閉圖標) .nav{ height:60px; background:#fff; padding:0px; margin:0px; transition:.5s; } .nav.active{ transform:translate(-100%); transition:.5s; } .nav ul{ float:right; display:flex;(可讓父元素內的子元素排成一行) list-style:none; padding:0px 20px 0px 0px; margin:0px; } .nav ul li{ border-right:1px solid rgba(0,0,0,.2); } .nav ul li:last-child{ border-right:none; } .nav ul li a{ line-height:60px; text-decoration:none; color:#262626; padding:0 20px; display:block; } .nav ul li a:hover{ background:#262626; color:#fff; } (這裏用媒體查詢來實現響應式的效果) @media screen and (max-width:640px){ .nav{ height:100vh;(指的是在手機上視口有多高,這個就有多高) } .nav ul{ display:block; width:100%; text-align:center; padding:0px; } .nav ul li{ border-right:none; border-bottom:1px solid rgba(0,0,0 .2); }