How can I use nested collapsible menu? - html

I'd like to use collapsible menu inside the sections. But it's not working, when i Add class=”collapsed” to SECTION B-B, it displays expanded without clicking. I want section 2.2 or section B-B like Drop down. What should i add or change to do that? Your help is very much appreciated.
Here's my work what i tried
JavaScript:
$(document).ready(function () {
setTimeout(function () {
// Slide
$('#menu1 > li > a.expanded + ul').slideToggle('medium');
$('#menu1 > li > a').click(function () {
$(this).toggleClass('expanded').toggleClass('collapsed').parent().find('> ul').slideToggle('medium');
});
$('#example1 .expand_all').click(function () {
$('#menu1 > li > a.collapsed').addClass('expanded').removeClass('collapsed').parent().find('> ul').slideDown('medium');
});
$('#example1 .collapse_all').click(function () {
$('#menu1 > li > a.expanded').addClass('collapsed').removeClass('expanded').parent().find('> ul').slideUp('medium');
});
}, 250);
});
HTML
<div id="example1">
<ul id="menu1" class="example_menu">
<li><a class="collapsed" href="#">SECTION A</a>
<ul>
<li>SECTION A-A</li>
<li>SECTION A-B</li>
<li>SECTION A-C</li>
</ul>
</li>
<li><a class="collapsed" href="#">SECTION B</a>
<ul>
<li>SECTION B-A</li>
<ul>
<li><a class="collapsed" href="#">SECTION B-B</a></li>
<li>SECTION B-B-1</li>
<li>SECTION B-B-2</li>
<li>SECTION B-B-3</li>
<li>SECTION B-B-4</li>
</ul>
</ul>
</li>
<li><a class="collapsed" href="#">SECTION C</a>
<ul>
<li>SECTION C-A</li>
<li>SECTION C-B</li>
<li>SECTION C-C</li>
</ul>
</li>
</ul>
</div>
CSS
body {
font: 10pt Arial, Helvetica, Sans-serif;
background-image: url();
margin: 0px;
}
a {
text-decoration: none;
}
#example1,
#example2,
#example3,
#example4,
#example5 {
float: left;
}
.expand_all,
.collapse_all {
cursor: pointer;
}
.example_menu {
font-size: 95%;
list-style: none;
margin: 0;
padding: 0;
vertical-align: top;
width: 230px;
}
.example_menu ul {
display: none;
list-style: none;
margin: 0;
padding: 0;
}
#menu1,
#menu2,
#menu3,
#menu4,
#menu5 {
margin: 0;
color: #96C;
}
#menu1 li,
#menu2 li,
#menu3 li,
#menu4 li,
#menu5 li,
.example_menu li {
background-image: none;
margin: 0;
padding: 0;
}
.example_menu ul ul {
display: block;
}
.example_menu ul ul li a {
padding-left: 20px;
width: 202px;
}
.example_menu li.header3 a {
padding-left: 34px;
width: 188px;
}
.example_menu a {
color: #000;
cursor: pointer;
display: block;
font-weight: bold;
margin-left: 0;
width: 211px;
padding-top: 18px;
padding-right: 0px;
padding-bottom: 12px;
padding-left: 19px;
}
.example_menu a.expanded {
background: #bbb url('images/collapse.gif') no-repeat 3px 61%;
}
.example_menu a.collapsed {
border-top: 1px solid #E3E3E3;
background-color: #bbb;
background-image: url(images/expand.gif);
background-repeat: no-repeat;
background-position: 3px 61%;
}
.example_menu a.normal {
background: none repeat scroll 0 0 #BBBBBB;
}
.example_menu a:hover {
text-decoration: none;
}
.example_menu ul a {
background: #e8e8e8;
border-top: 2px solid #fff;
color: #000;
display: block;
font-weight: normal;
padding: 3px 8px 2px 17px;
width: 205px;
}
.example_menu ul a:link {
font-weight: bolder;
}
.example_menu ul a:hover {
background : #f5f5f5;
text-decoration: underline;
}
.example_menu li.active a {
background: #fff;
}
.example_menu li.active li a {
background: #e8e8e8;
}
#menu1 li.footer,
#menu2 li.footer,
#menu3 li.footer,
#menu4 li.footer,
#menu5 li.footer,
.example_menu .footer {
background: transparent url('images/footer.jpg') no-repeat 0 0;
border-top: 2px solid #fff;
height: 9px;
line-height: 15px;
margin: 0 0 10px 0;
width: 131px;
}
.example_menu .footer span {
display: none;
}

Fixing your html itself seems to solve half of the issue.
You need to add the following in css
a.collapsed + ul{
display:none;
}
note: i took the click event handler outside the setTimeout which seem to be unnecessary (no need of slideToggle('medium') and timeOut to compensate it since <ul> s are hidden using css)
check this Fiddle

I didn't dive fully into your code. But this would be a start:
Change HTML structure of the second level to match the first level drop down:
<li><a class="collapsed" href="#">SECTION B</a>
<ul>
<li>SECTION B-A
</li>
<li><a class="collapsed" href="#">SECTION B-B</a>
<ul>
<li>SECTION B-B-1
</li>
<li>SECTION B-B-2
</li>
<li>SECTION B-B-3
</li>
<li>SECTION B-B-4
</li>
</ul>
</li>
</ul>
</li>
And "generalise" your click handler:
[...]
$('#menu1 li > a.expanded + ul').slideToggle('medium');
$('#menu1 li > a').click(function () {
$(this).toggleClass('expanded').toggleClass('collapsed').parent().find('> ul').slideToggle('medium');
});
[...]
Not sure about your "all" handlers. Without source you have to figure it out yourself :)

Related

How to classify li tags when we want it 2 times

I wanted to use li tags 2 times first time I used class with li tag
<ul *class="2"*>
<li *class="!"*><.......jpg" width="280" height="280" \></li>
<li class="!"><img src="......jpg" width="280" height="280" \></li>
And 2nd time...
I DID THIS
<ul>
<li>HOME</li>
<li>SONGS</li>
<li>GAMES</li>
<li><a class="active" href=".........html">GALLERY</a></li>
</ul>
MY CSS
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #0066ff;
}
I don't know why they are overlapping??Please tell me if anything wrong with the code above.
As LGSon said, it's because you have
position: fixed
Defined on your ul tag css.
if you define one of them to use a class name instead (e.g.)
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
ul.fixedHeader {
position: fixed;
bottom: 0;
width: 100%;
}
And your html would look something like this
<ul *class="2"*>
<li *class="!"*><.......jpg" width="280" height="280" \></li>
<li class="!"><img src="......jpg" width="280" height="280" \></li>
and
<ul class="fixedHeader">
<li>HOME</li>
<li>SONGS</li>
<li>GAMES</li>
<li><a class="active" href=".........html">GALLERY</a></li>
</ul>
You should be good to go. Further, I'm not sure what the asterisks are doing there... seems like invalid html to me.
Please comment if any part of this didn't make sense.
it works fine with me
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #0066ff;
}
<ul *class="2"*>
<li *class="!"*><img src="https://pixabay.com/static/uploads/photo/2015/06/03/14/24/ladybug-796481_960_720.jpg"" width="280" height="280" \></li>
<li class="!"><img src="http://www.planwallpaper.com/static/images/thailand.jpg" width="280" height="280" \></li>
</ul>
<ul>
<li>HOME</li>
<li>SONGS</li>
<li>GAMES</li>
<li><a class="active" href=".........html">GALLERY</a></li>
</ul>
Just use some class name on ul and style it's li as necessary
.ul1 {
list-style-type: none;
margin: 0;
}
.ul2 {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%;
}
.ul2 li {
float: left;
}
.ul2 li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.ul2 li a:hover:not(.active) {
background-color: #111;
}
.ul2 li a.active {
background-color: #0066ff;
}
<ul class="ul1">
<li>
<img src="1.jpg " width="280 " height="280 " />
</li>
<li>
<img src="2.jpg " width="280 " height="280 " />
</li>
</ul>
<ul class="ul2">
<li>HOME
</li>
<li>SONGS
</li>
<li>GAMES
</li>
<li><a class="active " href="#">GALLERY</a>
</li>
</ul>

How to activate the link when i hover over the full block not only just text?

When I hovering over the menu the color changes properly but the link will activate only when i hovering over the text i want it to be activate at the time when i just touch the block or when it changes its color.Please help me any help would be appreciated
a {
text-decoration: none;
color: #fff;
}
#navbar {
background: #204d86;
border-radius: 5px;
}
#navbar>ul {
list-style: none;
margin: 0;
padding: 0;
height: 40;
margin-left: 300px;
}
#navbar>ul>li {
float: left;
padding: 10px 35px;
border-radius: 5px;
}
#navbar>ul>li:hover {
background: #5cadff;
border-radius: 5px;
}
#navbar>ul>li>ul {
display: none;
padding: 0;
position: absolute;
background: #204d86;
border-radius: 5px;
}
#navbar>ul>li>ul>li {
padding: 0 20px;
line-height: 40px;
display: block;
background: #5cadff;
}
#navbar>ul>li:hover>ul {
display: block;
}
#navbar>ul>li>ul>li>ul {
display: none;
padding: 0 30px;
position: absolute;
}
#navbar li:hover > a {
color: #000;
}
#navbar>ul>li>ul>li:hover {
background: #204d86;
}
#navbar:after {
content: "";
clear: both;
display: table;
}
<div id="navbar">
<ul>
<li><a class="active" href="../index/myindex.html">HOME</a>
</li>
<li>STAYING HEALTHY
<ul>
<li>Diet & Weight loss
</li>
<li>Exercises
</li>
<li>Physical Activity
</li>
<li>Healthy Eating
</li>
</ul>
</li>
<li>DISEASES
<ul>
<li>Stock
</li>
<li>Osteoporosis
</li>
<li>Diabetes
</li>
<li>Heart Disease
</li>
</ul>
</li>
<li>MIND&MOOD
<ul>
<li>Depression
</li>
<li>Anxiety
</li>
<li>Addiction
</li>
<li>Stress
</li>
</ul>
</li>
</ul>
</div>
a {
text-decoration:none;
color:#fff;
display: block;
}
THEN, move any padding you had on the li to the links themselves. Adjust as required.
body {
background: #000;
}
a {
text-decoration: none;
color: #fff;
display: block;
}
#navbar {
background: #204d86;
border-radius: 5px;
}
#navbar>ul {
list-style: none;
margin: 0;
padding: 0;
height: 40px;
}
#navbar>ul>li {
float: left;
border-radius: 5px;
}
#navbar>ul>li>a {
padding: 10px 35px;
}
#navbar>ul>li:hover {
background: #5cadff;
border-radius: 5px;
}
#navbar>ul>li>ul {
display: none;
padding: 0;
position: absolute;
background: #204d86;
border-radius: 5px;
}
#navbar>ul>li>ul>li {
line-height: 40px;
display: block;
background: #5cadff;
}
#navbar>ul>li>ul>li>a {
padding: 0 20px;
}
#navbar>ul>li:hover>ul {
display: block;
}
#navbar>ul>li>ul>li>ul {
display: none;
padding: 0 30px;
position: absolute;
}
#navbar li:hover > a {
color: #000;
}
#navbar>ul>li>ul>li:hover {
background: #204d86;
}
<div id="navbar">
<ul>
<li><a class="active" href="../index/myindex.html">HOME</a>
</li>
<li>STAYING HEALTHY
<ul>
<li>Diet & Weight loss
</li>
<li>Exercises
</li>
<li>Physical Activity
</li>
<li>Healthy Eating
</li>
</ul>
</li>
<li>DISEASES
<ul>
<li>Stock
</li>
<li>Osteoporosis
</li>
<li>Diabetes
</li>
<li>Heart Disease
</li>
</ul>
</li>
</ul>
</div>
You should also validate the CSS as I spotted a couple of errors.
You have to set this in your a-tag style:
display: block
width: 100%
height: 100%
and the padding of li should be 0 px,
have a look here.

Menu dropdown isn't hidden beforehand

I've added submenus to some code I found online. I want the submenu, which is a drop-down list, only to show when I hover over the appropriate <a>. But the submenu is appearing right from the start without me hovering over it. What am I doing wrong?
div.menu3 {
/*width:500px;margin:0 auto;*/
/*Uncomment this line to make the menu center-aligned.*/
text-align: center;
font-size: 0;
height: 25px;
*position: relative;
*top: 1px;
/*Hacks for IE6 & IE7 */
}
div.menu3 a {
display: inline-block;
padding: 0 20px;
margin-right: 1px;
/* It specifies the distance between each tab */
background: #F7F7F7;
color: Black;
text-decoration: none;
font: normal 12px Trebuchet MS;
line-height: 24px;
border: 1px solid #CAD0DB;
border-bottom: 0;
color: #666;
vertical-align: top;
/*ChangeSet#2*/
text-decoration: none;
}
div.menu3 a:hover,
div.menu3 a.current {
background: #E9ECF0;
line-height: 25px;
color: #000;
}
div.menu3sub {
height: 6px;
border: 1px solid #CAD0DB;
background: #E9ECF0;
}
div.menu3sub a:hover,
div.menu3 a.current {
background: #E9ECF0;
line-height: 25px;
color: #000;
}
<div class="menu3">
Home
Employees
Department
<ul>
<li>Add Department</li>
<li>Delete Department</li>
</ul>
Asset
</div>
<div class="menu3sub"></div>
Your html is off. So your dropdown won't display properly. Use un-ordered lists instead of what you have. This should point you in the right direction.
ul.menu {
list-style-type: none;
}
ul.menu li {
display: inline-block;
}
ul.menu li.submenu ul {
display: none;
position: absolute;
}
ul.menu li.submenu:hover ul {
display: block;
}
ul.menu li.submenu ul li {
display: block;
}
<ul class="menu">
<li>Home
</li>
<li>Employees
</li>
<li class="submenu">Department
<ul>
<li>Add Department
</li>
<li>Delete Department
</li>
</ul>
</li>
<li>Asset
</li>
</ul>

CSS - Dropdown menu child item drags the whole style one level down

I have on main Menu that works totally fine without child menu items.
But now i have to add additional items and wanted to do a drop down Menu.
The Menu item "Services" is parent of "test" there should be a clean line, but when opening "Services" the border of the whole menu expands with "test" there should only be a small border the same length as "Services" surrounding "test".
For this to work i have to edit "current active deeper parent" or "nav-child unstyled small" if i'm correctly.
If possible you should even only need to hover over "Services" to trigger the dropdown menu.
.flowmenu {
background: none repeat scroll 0 0 #333333;
padding-left: 0px;
position: relative;
}
.flowmenu li {
display: inline-block;
list-style: none outside none;
padding: 0;
}
.flowmenu ul li {
position: relative;
}
.flowmenu li > a {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
border-color: #000000;
border-style: solid;
border-width: 0 1px 0 0;
box-shadow: 1px 0 0 0 #555555;
margin-bottom: 0;
padding: 5px 15px;
text-transform: uppercase;
color: #FFFFFF;
cursor: pointer;
display: block;
font-family: 'Corbel', sans-serif;
font-size: 12px;
font-weight: normal;
line-height: 30px;
text-decoration: none;
}
.flowmenu li.active {
background: #222222;
}
<div class="navfix">
<ul class="nav menu flowmenu li">
<li class="item-435">Home
</li>
<li class="item-485">Communication
</li>
<li class="item-486 current active deeper parent">Services
<ul class="nav-child unstyled small">
<li class="item-579">Test
</li>
</ul>
</li>
<li class="item-487">Languages
</li>
<li class="item-488">Network
</li>
</ul>
</div>
The parent list item of the <ul> has to be position relative if you want a dropdown menu
the <ul> it self can be position absolute so it appears nicely under the parent. I made a jsFIDDLE
Most important changes:
.flowmenu > li {
position:relative;
display: inline-block;
list-style: none outside none;
padding: 0;
}
.flowmenu ul {
position: absolute;
display:block; /* or none */
width:100%;
background: #222222;
list-style:none;
margin:0;
padding:0;
}
Hope this helps! I didn't completly styled the dropdown maybe it's better to do that yourself
.flowmenu {
background: none repeat scroll 0 0 #333333;
padding-left: 0px;
position: relative;
}
.flowmenu > li {
position: relative;
display: inline-block;
list-style: none outside none;
padding: 0;
}
.flowmenu ul {
position: absolute;
display: none;
width: 100%;
background: #222222;
list-style: none;
margin: 0;
padding: 0;
}
.flowmenu ul > li {
display: block;
}
.flowmenu li > a {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
border-color: #000000;
border-style: solid;
border-width: 0 1px 0 0;
box-shadow: 1px 0 0 0 #555555;
margin-bottom: 0;
padding: 5px 15px;
text-transform: uppercase;
color: #FFFFFF;
cursor: pointer;
display: block;
font-family: 'Corbel', sans-serif;
font-size: 12px;
font-weight: normal;
line-height: 30px;
text-decoration: none;
}
.flowmenu > li.active {
background: #222222;
}
.flowmenu > li:hover ul {
display: block;
}
<div class="navfix">
<ul class="nav menu flowmenu li">
<li class="item-435">Home
</li>
<li class="item-485">Communication
</li>
<li class="item-486 current active deeper parent">Services
<ul class="nav-child unstyled small">
<li class="item-579">Test
</li>
</ul>
</li>
<li class="item-487">Languages
</li>
<li class="item-488">Network
</li>
</ul>
</div>

Absolute <ul> for dropdown shows at wrong place on IE7

If you try that in FireFox:
http://jsfiddle.net/rJUKT/2/
it works fine. The dropdown menu shows at the bottom of its parent like that:
But if you try it with IE7, the dropdown menu shows at the right, like that:
Any idea why it does that?
HTML
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<ul id="menu">
<li>
<span>Menu 1</span>
<ul>
<li>Link 1.1</li>
<li>Link 1.2</li>
<li>Link 1.3</li>
<li>Link 1.4</li>
</ul>
</li>
<li>
<span>Menu 2</span>
<ul>
<li>Link 2.1</li>
<li>Link 2.2</li>
<li>Link 2.3</li>
<li>Link 2.4</li>
</ul>
</li>
</ul>
CSS
#menu { width: 100%; float: right; list-style: none; margin-top: 5px; color: #2A4153; }
#menu li {
float: left;
font-size: 17px;
font-weight: bold;
background-color: #e1f1ff;
border: 1px solid #93b5d4;
margin: 0 1px 0 1px;
padding: 4px 0 0 0;
border-bottom: none;
height: 20px;
}
#menu li a, #menu li span { padding: 4px 7px 2px 7px; cursor: pointer; }
#menu li ul {
clear: both;
position:absolute;
display:none;
padding:0;
list-style:none;
width: 150px;
margin: -1px 0 0 -2px;
}
#menu li ul li {
float: left;
width: 150px;
border: 1px solid #93b5d4;
border-top: none;
font-size: 15px;
font-weight: normal;
background-image: url('16x16/eye.png');
background-position: 4px 4px;
background-repeat: no-repeat;
-moz-border-radius: 0;
}
#menu li ul li a, #menu li ul li span { display:block; padding-left: 25px; }
#menu li ul li:hover { background-color: #e5f6e8; border: 1px solid #93d4a2; border-top: none; }
#menu li:hover { background-color: #e5f6e8; border: 1px solid #93d4a2; border-bottom: none; }
JS
$(function() {
$('#menu li').hover(
function () {
//show its submenu
$('ul', this).slideDown(100);
},
function () {
//hide its submenu
$('ul', this).slideUp(100);
}
);
});
Thanks!
You need to set "top" and "left" properties for dropdown UL
You can check it out this. Some css properties updated in this
http://jsfiddle.net/vkVHC/
If that's the only reason you're using jQuery (for the dropdown effect on hover), you may as well do it using CSS instead (and thus save many kBs being loaded by the site). Also, Alexei's answer is correct.