i have a little problem in my hands, i've got one Megamenu, that works on mobile and desktop, i want to have the content of the menu center aligned, but i can only align left or right. i can align center with
> ul {
display: flex; //it was none
justify-content: center;
align-self: center;
...
}
but then the hover is always enable and the mobile version have all the menus opened.. it i close each one of them , then the menu work as expected.. the menu i started with was this one fiddle:
Fiddle of actual code with
display: flex;
justify-content: space-between;
working as expected but the menus are all opened..
FIDDLE: https://codepen.io/anon/pen/JpBrRp
my code:
<div class="menu-container">
<div class="menu">
<nav class="navbar-toggleable-md">
<div id="toggle" class="navbar-toggler"><a></a>
</div>
</nav>
<ul id="my_styles" class="rowcenter" >
<li>
<ul>
<li>
<a href="#">menu</a>
<ul>
<li><a href=...</a>x</li>
<li><a href=..</a>z</li>
</ul>
</li>
</ul>
</li>
The .css:
.menu-container {
width: 100%;
margin: 0 auto;
}
.menu {
> ul {
margin: 0 auto;
width: 100%;
list-style: none;
padding: 0;
position: relative;
//position: relative;
/* IF .menu position=relative -> ul = container width, ELSE ul = 100% width */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
&:before,
&:after {
content: "";
display: table;
}
&:after {
clear: both;
}
> li {
float: left;
padding: 0px;
margin: 0px;
a {
text-decoration: none;
padding: 1.5em 2.1em;
display: block;
}
&:hover {
}
> ul {
display: none;
justify-content: center;
align-self: center;
width: 100%;
background: #3a3f48;
padding: 20px;
position: absolute;
z-index: 1001;
left: 0;
margin: 0;
list-style: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
&:before,
&:after {
content: "";
display: table;
}
&:after {
clear: both;
}
> li {
margin: 0;
padding-bottom: 0;
list-style: none;
width: 25%;
background: none;
float: left;
a {
color: #ffffff;
padding: .2em 0;
width: 95%;
display: block;
border-bottom: 1px solid #ccc;
}
> ul {
display: block;
padding: 0;
margin: 10px 0 0;
list-style: none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
&:before,
&:after {
content: "";
display: table;
}
&:after {
clear: both;
}
> li {
float: left;
width: 100%;
padding: 10px 0;
margin: 0;
font-size: .8em;
a {
border: 0;
}
}
}
}
&.normal-sub {
width: 300px;
left: auto;
padding: 10px 20px;
> li {
width: 100%;
a {
border: 0;
padding: 1em 0;
}
}
}
}
}
}
}
/* ––––––––––––––––––––––––––––––––––––––––––––––––––
Mobile style's
–––––––––––––––––––––––––––––––––––––––––––––––––– */
@media only screen and (max-width: 959px) {
.menu-container {
width: 100%;
}
.menu-mobile {
display: block;
}
.menu-dropdown-icon {
&:before {
//display: block;
}
}
.menu {
> ul {
display: none;
> li {
width: 100%;
float: none;
display: block;
a {
padding: 1.5em;
display: block;
}
> ul {
position: relative;
&.normal-sub {
width: 100%;
}
> li {
float: none;
width: 100%;
margin-top: 20px;
&:first-child {
margin: 0;
}
> ul {
position: relative;
> li {
float: none;
}
}
}
}
}
}
.show-on-mobile {
display: block;
}
}
}
the JS:
/* global $ - Mega menu ***********/
$(document).ready(function() {
"use strict";
$('.menu > ul > li:has(ul)').addClass('menu-dropdown-icon');
//Checks if li has sub (ul) and adds class for toggle icon - just an UI
$('.menu > ul > li > ul:not(:has(ul))').addClass('normal-sub');
//Checks if drodown menu's li elements have anothere level (ul), if not the dropdown is shown as regular dropdown, not a mega menu (thanks Luka Kladaric)
$(".menu > nav > div > a").before("<a href=\"#\" class=\"menu-mobile\"><img width='34px' height='34px' src=\"/assets/images/Menu_icons/hmb.png\"></a>");
//Adds menu-mobile class (for mobile toggle menu) before the normal menu
//Mobile menu is hidden if width is more then 959px, but normal menu is displayed
//Normal menu is hidden if width is below 959px, and jquery adds mobile menu
//Done this way so it can be used with wordpress without any trouble
$(".menu > ul > li").hover(function(e) {
if ($(window).width() > 943) {
$(this).children("ul").stop(true, false).fadeToggle(150);
e.preventDefault();
}
});
//If width is more than 943px dropdowns are displayed on hover
$(".menu > ul > li").click(function() {
if ($(window).width() <= 943) {
$(this).children("ul").fadeToggle(150);
}
});
//If width is less or equal to 943px dropdowns are displayed on click (thanks Aman Jain from stackoverflow)
$(".menu-mobile").click(function(e) {
$(".menu > ul").toggleClass('show-on-mobile');
e.preventDefault();
});
//when clicked on mobile-menu, normal menu is shown as a list, classic rwd menu story (thanks mwl from stackoverflow)
});
I got the answer, i think that's not the correct one, but it works..
on .css i've created a class called center:
.center{
display: flex !important;
}
then on .JS i created a function that open the submenu ('ul') and toogleclass to override with class center:
$(".menu > ul > li").click(function(e) {
if ($(window).width() > 943) {
$(this).children('ul').fadeToggle(15);
$(this).children('ul').toggleClass('center');
e.preventDefault();
}
});
However i have a little problem: when opened a submenu (clicking on one item of main menu) i want that submenu to vanish when i click anywhere on the document body, if i click on that submenu ('ul'), or one particular item on submenu it works like i want, but if i click on other menu item the previous submenu keeps opened, creating layers of submenus that i have to click on to make them vanish (or click on the main menus item that make them appear) im not sure i am clear..
here's a Fiddle https://codepen.io/anon/pen/JpBrRp