I need to add 2 more levels to my navigation menu first and second levels working fine but couldnt handle third level, I tried several variations but couldnt handle problem.
I asked on stackoverflow too none answered, Thanks for any help.
Here is css :
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
background: #e3e3e3;
position: relative;
}
nav ul li {
display: inline-block;
}
nav ul li a {
color: #292929;
text-decoration: none;
padding: 15px;
display: block;
}
nav ul li:hover {
background: lightgrey;
}
nav ul ul {
position: absolute;
min-width: 200px;
background: lightgrey;
display: none;
}
nav ul ul li {
display: block;
background: #e3e3e3;
}
nav ul li:hover ul {
display: block;
}
nav ul li i {
color: #292929;
float: right;
padding-left: 20px;
}
nav div {
background: lightgrey;
color: #292929;
font-size: 24px;
padding: 0.6em;
cursor: pointer;
display: none;
}
@media(max-width: 768px) {
nav div {
display: block;
}
nav ul {
display: none;
position: static;
background: #e3e3e3;
}
nav ul li {
display: block;
}
nav ul ul {
position: static;
background: #e3e3e3;
}
}
JS isn’t my area but in the CSS you would need to move the deeper nested levels to the right of the menu so that they will show at the side rather than underneath.
I would also use classes to identify the dropdown triggers and also avoid globally applying styles to elements without limiting their context with a class. Otherwise you can never use another list element in your site without the js and css being applied to it.
As I said my JS (and Jquery) is very basic but here is a start for you.
I have removed the hover menu on desktop because that would confuse the mobile versions as they would apply a first touch as a hover and then toggle it back off with the click. Its much easier (and more accessible) to use a click menu for all.
Your example has class in dropdown ul which is I dont want to use, Why ?
Because my menu is in action on site now, if I use that menu I need to change whole php function is well.
I am trying to get jquery code from your example if I can.
Not sure what you mean as they have nothing in common as such. The stopPropogation is to stop the click event bubbling up through all the parent list items and triggering multiple toggles.you only want to catch the click on the actual item you clicked.
The proper way to do it would be to apply the click handler to the main parent of the ul and then work out which element triggered the click and then you wouldn’t need the stopPropogation but that is a question for the JS experts here
My example looks fine on mobile on my iphone 5 and works very well.
Or did you mean something else?
Never mind ! I got yours, wasnt sure about my own css in mine
.sibling() Function is closing first level dropdown when you click on second one, no need to click back.
.stopPropagation() is keeping dropdown open until you click back, it makes menu structure longer for visiters, if you have a long structered data then visiters need to go down all that way long.
Without the stopPropogation you would have all levels of the menu opening and closing as soon as you clicked on one of them so there would be no point in having anything but one level
I made my example so that as you click each level only one level opens at a time but if you click the top level (or a higher menu level) all menus below will collapse. This would seem to be the best and most accessible option. Otherwise if you open all levels and close all levels then that will confuse the user and also becomes unwieldy as you get the great big one menu on your first click.
The menu needs to be logical where possible but of course there are probably loads of variations you could build into the behaviour but usually simple is best. I tested my example on mobile and it works very nicely and as I would expect but feel free to change to suit your purposes.