I want that my mobile hamburger menu and submenu items are centered, and that when I click on one of the menu items that contain a subemenu item, it won't disappear. I've tried different things in CSS but I can't seem to solve this by myself.
我希望我的移动汉堡菜单和子菜单项居中,当我点击其中一个包含子菜单项的菜单项时,它不会消失。我在CSS中尝试了不同的东西,但我似乎无法自己解决这个问题。
Here's my site(use the mobile view): http://sstromberg.saldev.nl/index.html
这是我的网站(使用移动视图):http://sstromberg.saldev.nl/index.html
Could anyone help me?
谁能帮助我?
HTML markup:
HTML标记:
<body>
<nav>
<div id="hamburger-menu">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
<ul id="menu">
<li><a href="index.html">Home</a></li>
<li>
<a>CV</a>
<ul class="submenu">
<li><a href="pgegevens.html">Persoonlijke Gegevens</a></li>
<li><a href="werkervaring.html">Werkervaring</a></li>
<li><a href="opleiding.html">Opleiding</a></li>
</ul>
</li>
<li>
<a>Portfolio</a>
<ul class="submenu">
<li><a href="dezewebsite.html">Deze Website</a></li>
<li><a href="klassenlijst.html">Klassenlijst</a></li>
<li><a href="todolist.html">TODO-list</a></li>
</ul>
</li>
<li>
<a>Overig</a>
<ul class="submenu">
<li><a href="socialmedia.html">Social Media</a></li>
<li><a href="gmaps.html">Google Maps</a></li>
</ul>
</li>
</ul>
</div>
</nav>
CSS code:
CSS代码:
@media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
}
ul#menu > li {
width: 100%;
display: block;
}
ul#menu > li > ul.submenu {
display: none;
}
ul#menu > li:hover > ul.submenu {
display: block;
list-style: none;
margin: 0;
padding: 0;
background-color: #f3f3f3;
}
ul#menu a:hover {
transform: scale(1.1);
}
ul#menu {
list-style: none;
margin: 0;
padding: 0;
text-align: center;
font-family: sans-serif;
background-color: #f3f3f3;
z-index: 50;
}
ul#menu a {
display: block;
padding: 1em; /* met 1em word de ruimte tussen de list items 2x zo groot */
font-family: sans-serif;
color: #FF4136;
text-decoration-line: none;
font-weight: bold;
}
.submenu {
z-index: 100;
}
}
jQuery code:
jQuery代码:
$(document).ready(function() {
$('#hamburger-menu').on('click', function () {
$("#menu").slideToggle();
});
});
1 个解决方案
#1
1
First of all wrap your menu bar into a div and then apply margin:auto
with width. It will center the icon.
首先将菜单栏包装成div,然后应用margin:auto with width。它将使图标居中。
And also apply click event on icon
not in the whole div
并且还在整个div中的图标上应用click事件
$(document).ready(function() {
$('#hamburger-menu .icon').on('click', function() {
$("#menu").slideToggle();
});
});
@media only screen and (min-width: 320px) and (max-width: 700px) {
.bar1,
.bar2,
.bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
}
.icon {
width: 35px;
margin: auto;
}
ul#menu>li {
width: 100%;
display: block;
}
ul#menu>li>ul.submenu {
display: none;
}
ul#menu>li:hover>ul.submenu {
display: block;
list-style: none;
margin: 0;
padding: 0;
background-color: #f3f3f3;
}
ul#menu a:hover {
transform: scale(1.1);
}
ul#menu {
list-style: none;
margin: 0;
padding: 0;
text-align: center;
font-family: sans-serif;
background-color: #f3f3f3;
z-index: 50;
}
ul#menu a {
display: block;
padding: 1em;
/* met 1em word de ruimte tussen de list items 2x zo groot */
font-family: sans-serif;
color: #FF4136;
text-decoration-line: none;
font-weight: bold;
}
.submenu {
z-index: 100;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div id="hamburger-menu">
<div class="icon">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<ul id="menu">
<li><a href="index.html">Home</a></li>
<li>
<a>CV</a>
<ul class="submenu">
<li><a href="pgegevens.html">Persoonlijke Gegevens</a></li>
<li><a href="werkervaring.html">Werkervaring</a></li>
<li><a href="opleiding.html">Opleiding</a></li>
</ul>
</li>
<li>
<a>Portfolio</a>
<ul class="submenu">
<li><a href="dezewebsite.html">Deze Website</a></li>
<li><a href="klassenlijst.html">Klassenlijst</a></li>
<li><a href="todolist.html">TODO-list</a></li>
</ul>
</li>
<li>
<a>Overig</a>
<ul class="submenu">
<li><a href="socialmedia.html">Social Media</a></li>
<li><a href="gmaps.html">Google Maps</a></li>
</ul>
</li>
</ul>
</div>
</nav>
#1
1
First of all wrap your menu bar into a div and then apply margin:auto
with width. It will center the icon.
首先将菜单栏包装成div,然后应用margin:auto with width。它将使图标居中。
And also apply click event on icon
not in the whole div
并且还在整个div中的图标上应用click事件
$(document).ready(function() {
$('#hamburger-menu .icon').on('click', function() {
$("#menu").slideToggle();
});
});
@media only screen and (min-width: 320px) and (max-width: 700px) {
.bar1,
.bar2,
.bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
}
.icon {
width: 35px;
margin: auto;
}
ul#menu>li {
width: 100%;
display: block;
}
ul#menu>li>ul.submenu {
display: none;
}
ul#menu>li:hover>ul.submenu {
display: block;
list-style: none;
margin: 0;
padding: 0;
background-color: #f3f3f3;
}
ul#menu a:hover {
transform: scale(1.1);
}
ul#menu {
list-style: none;
margin: 0;
padding: 0;
text-align: center;
font-family: sans-serif;
background-color: #f3f3f3;
z-index: 50;
}
ul#menu a {
display: block;
padding: 1em;
/* met 1em word de ruimte tussen de list items 2x zo groot */
font-family: sans-serif;
color: #FF4136;
text-decoration-line: none;
font-weight: bold;
}
.submenu {
z-index: 100;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div id="hamburger-menu">
<div class="icon">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<ul id="menu">
<li><a href="index.html">Home</a></li>
<li>
<a>CV</a>
<ul class="submenu">
<li><a href="pgegevens.html">Persoonlijke Gegevens</a></li>
<li><a href="werkervaring.html">Werkervaring</a></li>
<li><a href="opleiding.html">Opleiding</a></li>
</ul>
</li>
<li>
<a>Portfolio</a>
<ul class="submenu">
<li><a href="dezewebsite.html">Deze Website</a></li>
<li><a href="klassenlijst.html">Klassenlijst</a></li>
<li><a href="todolist.html">TODO-list</a></li>
</ul>
</li>
<li>
<a>Overig</a>
<ul class="submenu">
<li><a href="socialmedia.html">Social Media</a></li>
<li><a href="gmaps.html">Google Maps</a></li>
</ul>
</li>
</ul>
</div>
</nav>