如何创建适合移动设备的响应菜单?

我是HTML,CSS的新手,我正在尝试制作简单的响应式菜单。resize后,浏览器菜单图标将显示,然后点击打开菜单。请检查下面的代码。你能帮帮我吗? 注意:我不想使用bootstrap。

body{ margin: 0; padding: 0; height: 100%; } #menu-bar { font-size: 20px; text-align: right; cursor: pointer; display: none; } .menu-logo { float: left; } nav{ width: 100%;text-align: center; } nav ul { background-color: #A4D54C; float: right; font-size: 20px; line-height: 50px; } nav li{ display: inline; list-style-type: none; } nav a{ text-decoration: none; padding: 10px; color: #000; text-transform: uppercase; } @media only screen and (max-width: 768px) { .menu-logo { margin-top: -50px; } nav { } nav ul{ float: none; margin-top: 50px; background-color: #A4D54C; } nav li{ display: inline; } nav a{ color: #fff; } } @media only screen and (max-width: 400px) { #menu-bar { display: block; } .menu-logo { width: 100%;text-align: center; } nav { } nav ul{ float: none; margin-top: 50px; background-color: #A4D54C; } nav li{ } nav a{ display: block; color: #fff; border-bottom:1px solid #000; margin: 0; padding: 03px; } } 
    
info@info.com

对于响应式菜单,我们从768Px启动窗口屏幕,因此我不会更改您的CSS上的任何内容只需用768px替换媒体屏幕400px并删除您为768px创建的媒体部分。 我还在所有3节HTML,CSS和jQuery中进行了注释,我在其中更改或添加简短描述。 任何问题都在评论中问我。 谢谢。 LiveFiddle

 $(document).ready(function() { $('https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#menu-bar').click(function() { $("nav ul").slideToggle("slow"); /* this function show/hide the menu when you click the bar icon */ }) $('nav ul li a').click(function() { $('nav ul ').slideToggle("slow"); /* this function hide the menu when you click on a link*/ }) }); 
 body { margin: 0; padding: 0; height: 100%; } https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#menu-bar { font-size: 20px; text-align: right; cursor: pointer; display: none; } .menu-logo { float: left; } nav { width: 100%; text-align: center; } nav ul { background-color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#A4D54C; float: right; font-size: 20px; line-height: 50px; } nav li { display: inline; list-style-type: none; } nav a { text-decoration: none; padding: 10px; color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#000; text-transform: uppercase; } @media only screen and (max-width: 768px) { https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#menu-bar { display: block; margin-right: 10px; /* set this margin because its close to the window screen*/ } .menu-logo { width: 100%; text-align: center; } nav {} nav ul { float: none; display: none; /* set display none for use the click fucntion to show the menu*/ background-color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#A4D54C; /* Set margin and padding 0 for full width border bottom*/ margin:0; padding: 0; } nav li {} nav a { display: block; color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#fff; border-bottom: 1px solid https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#000; margin: 0; padding: 3px; } } 
    
info@info.com

您可以为它编写媒体查询。 如果你不想使用bootstrap。

例如。

你想让一个网页对768px做出响应,那你就可以写了。

 @media only screen and (min-width: 768px) { body { // Your CSS Code Here } } 

供参考您可以在这里看到

试试这个:)这项工作对我来说。 没有引导程序

 $('https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#menu-bar').click(function(){ $('nav ul').toggle('blind'); }) 
 body{ margin: 0; padding: 0; height: 100%; } https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#menu-bar { font-size: 20px; text-align: right; background-color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#A4D54C; padding:10px; margin-bottom:-50px; color:https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#fff; cursor: pointer; display: none; } .menu-logo { float: left; } nav{ width: 100%;text-align: center; } nav ul { background-color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#A4D54C; float: right; font-size: 20px; line-height: 50px; } nav li{ display: inline; list-style-type: none; } nav a{ text-decoration: none; padding: 10px; color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#000; text-transform: uppercase; } @media only screen and (max-width: 768px) { .menu-logo { margin-top: -50px; } https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#menu-bar { display:block; } nav { } nav ul{ float: none; margin-top: 50px; background-color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#A4D54C; display:none; } nav li{ display: block; position:relative; border-bottom:1px solid https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#fff; } nav a{ color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#fff; } } @media only screen and (max-width: 400px) { https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#menu-bar { display: block; } .menu-logo { width: 100%;text-align: center; } nav { } nav ul{ float: none; margin-top: 50px; background-color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#A4D54C; } nav li{ } nav a{ display: block; color: https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#fff; border-bottom:1px solid https://stackoverflow.com/questions/39740988/how-to-create-mobile-friendly-responsive-menu/#000; margin: 0; padding: 03px; } } 
     
info@info.com