I wrote a list item hover effect today, but the result behavior is quite strange:
When hover from Item6 to Item2, everything's ok,
but when hover from Item2 to Item6 & hover the blank area (inside<li>, outside of <p>),
it seems something is blocking the hovering.
Feels like something's wrong with the pseudo element.
Can anyone help me with it? Thanks!
Here is my code:
var li = document.querySelectorAll('li')
li.forEach((e) => {
e.addEventListener('click', (event) => {
li.forEach((l) => {
l.classList.remove('active')
})
e.classList.add('active')
})
})
:root {
--el-color-primary: #409eff;
--el-color-info: #909399;
--el-color-primary-rgb: 64, 158, 255;
}
.categories {
width: 200px;
}
li {
list-style-type: none;
box-sizing: border-box;
height: 2.2rem;
cursor: pointer;
color: var(--el-color-info);
z-index: 0;
border-radius: .5rem;
}
li {
margin: .3rem;
}
li p {
font-size: 1rem;
line-height: 1rem;
padding: .6rem 0 .6rem .8rem;
margin: 0;
color: var(--el-color-info);
}
li.active p {
font-weight: 600;
color: var(--el-color-primary);
}
li.active {
background-color: rgba(var(--el-color-primary-rgb), .2);
}
li:not(.active)::after {
content: "";
display: inline-block;
width: 0;
height: 2.2rem;
position: relative;
top: -2.2rem;
left: 0;
border-radius: .5rem;
background-color: rgba(var(--el-color-primary-rgb), .1);
z-index: -99;
transition: width .4s ease-out;
}
li:not(.active):hover p {
color: var(--el-color-primary);
}
li:not(.active):hover::after {
width: 100%;
}
<ul class="categories">
<li class="active">
<p>Item 1</p>
</li>
<li>
<p>Item 2</p>
</li>
<li>
<p>Item 3</p>
</li>
<li>
<p>Item 4</p>
</li>
<li>
<p>Item 5</p>
</li>
<li>
<p>Item 6</p>
</li>
</ul>
The problem is solved by modifying putting a display: block at li:not(.active)::after instead of an inline-block
var li = document.querySelectorAll('li')
li.forEach((e) => {
e.addEventListener('click', (event) => {
li.forEach((l) => {
l.classList.remove('active')
})
e.classList.add('active')
})
})
:root {
--el-color-primary: #409eff;
--el-color-info: #909399;
--el-color-primary-rgb: 64, 158, 255;
}
.categories {
width: 200px;
}
li {
list-style-type: none;
box-sizing: border-box;
height: 2.2rem;
cursor: pointer;
color: var(--el-color-info);
z-index: 0;
border-radius: .5rem;
}
li {
margin: .3rem;
}
li p {
font-size: 1rem;
line-height: 1rem;
padding: .6rem 0 .6rem .8rem;
margin: 0;
color: var(--el-color-info);
}
li.active p {
font-weight: 600;
color: var(--el-color-primary);
}
li.active {
background-color: rgba(var(--el-color-primary-rgb), .2);
}
li:not(.active)::after {
content: "";
display: block;
width: 0;
height: 2.2rem;
position: relative;
top: -2.2rem;
left: 0;
border-radius: .5rem;
background-color: rgba(var(--el-color-primary-rgb), .1);
z-index: -99;
transition: width .4s ease-out;
}
li:not(.active):hover p {
color: var(--el-color-primary);
}
li:not(.active):hover::after {
width: 100%;
}
<ul class="categories">
<li class="active">
<p>Item 1</p>
</li>
<li>
<p>Item 2</p>
</li>
<li>
<p>Item 3</p>
</li>
<li>
<p>Item 4</p>
</li>
<li>
<p>Item 5</p>
</li>
<li>
<p>Item 6</p>
</li>
</ul>
The problem is in top: -2.2rem because it is relative and relative position doesn't remove the element from the normal document flow (mdn link). see how it looks like when we just use top: 0:
The whole selected area shows "item 2" li element and the light-blue box is actually a part of that element and we bring it up by negative top value. That's why when we enter mouse from item 2 to 3, we think we would be on item 3 but in fact we are still hovering item 2.
I guess the better way to do this is using absolute:
var li = document.querySelectorAll('li')
li.forEach((e) => {
e.addEventListener('click', (event) => {
li.forEach((l) => {
l.classList.remove('active')
})
e.classList.add('active')
})
})
:root {
--el-color-primary: #409eff;
--el-color-info: #909399;
--el-color-primary-rgb: 64, 158, 255;
}
.categories {
width: 200px;
}
li {
list-style-type: none;
box-sizing: border-box;
height: 2.2rem;
cursor: pointer;
color: var(--el-color-info);
z-index: 0;
border-radius: .5rem;
position: relative; /* <----- new */
}
li {
margin: .3rem;
}
li p {
font-size: 1rem;
line-height: 1rem;
padding: .6rem 0 .6rem .8rem;
margin: 0;
color: var(--el-color-info);
}
li.active p {
font-weight: 600;
color: var(--el-color-primary);
}
li.active {
background-color: rgba(var(--el-color-primary-rgb), .2);
}
li:not(.active)::after {
content: "";
display: block;
width: 0;
height: 2.2rem;
position: absolute; /* <----- new */
top: 0; /* <----- new */
left: 0;
border-radius: .5rem;
background-color: rgba(var(--el-color-primary-rgb), .1);
z-index: -99;
transition: width .4s ease-out;
}
li:not(.active):hover p {
color: var(--el-color-primary);
}
li:not(.active):hover::after {
width: 100%;
}
<ul class="categories">
<li class="active">
<p>Item 1</p>
</li>
<li>
<p>Item 2</p>
</li>
<li>
<p>Item 3</p>
</li>
<li>
<p>Item 4</p>
</li>
<li>
<p>Item 5</p>
</li>
<li>
<p>Item 6</p>
</li>
</ul>
Related
I have hardcoded padding for each of the padding hierarchy level.
I need the solution for any of level without hardcoding padding for each level.
.jdm-nav li {
font-size: 16px;
position: relative;
}
.jdm-nav li a {
padding-left: 15px;
}
.jdm-nav li li a {
padding-left: 30px;
}
.jdm-nav li li li a {
padding-left: 45px;
}
.jdm-nav li li li li a {
padding-left: 60px;
}
li li li li ... a
Every next step could be increased by 15px.
Full code example.
There is the mobile menu with ability to dropdown each item by click dropdown icon in the right item corner. If there are the items to expand.
.jdm-nav {
background-color: white;
width: 100%;
padding: 0;
}
.jdm-nav ul {
padding: 0;
}
.jdm-nav {
list-style: none;
}
.jdm-nav li {
list-style: none;
}
.jdm-nav ul {
margin: 0;
}
.jdm-nav a {
text-align: left;
display: flex;
color: rgb(76, 92, 104);
font-weight: 500;
padding: .5em 0;
font-size: 16px;
border-bottom: 1px solid rgba(76, 92, 104, 0.1);
}
.jdm-nav li {
font-size: 16px;
position: relative !important;
}
.jdm-nav li a {
padding-left: 15px;
}
.jdm-nav li li a {
padding-left: 30px;
}
.jdm-nav li li li a {
padding-left: 45px;
}
.jdm-nav li li li li a {
padding-left: 60px;
}
.jdm-nav li {
position: relative;
}
.jdm-nav li>[data-jdm-sub-menu-switcher] {
font-size: 13px;
display: flex;
width: 100%;
height: 100%;
border-bottom: 1px solid rgba(76, 92, 104, 0.1);
position: relative;
}
.jdm-nav li {
position: relative !important;
}
.jdm-nav li.menu-item-has-children>ul {
max-height: 0;
overflow: hidden;
transition: 150ms max-height ease-out;
}
.jdm-nav li.menu-item-has-children>ul[data-jdm-state="expanded"] {
max-height: initial;
}
.jdm-nav li.menu-item-has-children> :first-child {
grid-column: 1;
}
.jdm-nav li.menu-item-has-children>[data-jdm-sub-menu-switcher] {
grid-column: 2;
}
.jdm-nav li.menu-item-has-children>ul {
grid-row: 2;
grid-column-start: 1;
grid-column-end: 3;
}
.jdm-nav li>[data-jdm-sub-menu-switcher]::after {
position: absolute;
}
.jdm-nav li>[data-jdm-sub-menu-switcher]::after {
position: absolute;
width: 1em;
height: 1em;
content: '';
clip-path: url(#prefix-mobile-dropdown-clip-mask);
font-size: 13px;
background-color: rgb(76, 92, 104);
display: inline-flex;
top: 50%;
right: 50%;
transform: translateY(-50%) translateX(50%);
}
.jdm-nav li.menu-item-has-children {
display: grid;
grid-template-columns: 100fr 15fr;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<div id="prefix-mobile-menu" class="mobile-menu-area prefix-mobile-menu" data-prefix-status="collapsed">
<nav id="mobile-menu" class="jdm-nav">
<ul id="primary-menu" class="menu-overflow">
<li id="menu-item-2420" class="menu-item-has-children">
1
<button data-jdm-sub-menu-switcher=""></button>
<ul class="sub-menu" style="" data-jdm-state="expanded">
<li id="menu-item-2418" class="menu-item-has-children">1.1
<button data-jdm-sub-menu-switcher=""></button>
<ul class="sub-menu" style="" data-jdm-state="expanded">
<li id="menu-item-2434" class="menu-item-has-children">1.1.1
<button data-jdm-sub-menu-switcher=""></button>
<ul class="sub-menu">
<li id="menu-item-2435">
1.1.1.1
</li>
<li id="menu-item-2436">1.1.1.2
</li>
<li id="menu-item-2437">1.1.1.3
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li id="menu-item-1832">2</li>
</ul>
</nav>
<svg width="0" height="0" viewBox="0 0 448 512">
<defs>
<clipPath
viewbox="0 0 448 512"
transform="scale(0.00223, 0.00195)"
clipPathUnits="objectBoundingBox"
id="prefix-mobile-dropdown-clip-mask">
<path fill="#000000"
d="M441.9 167.3l-19.8-19.8c-4.7-4.7-12.3-4.7-17 0L224 328.2 42.9 147.5c-4.7-4.7-12.3-4.7-17 0L6.1 167.3c-4.7 4.7-4.7 12.3 0 17l209.4 209.4c4.7 4.7 12.3 4.7 17 0l209.4-209.4c4.7-4.7 4.7-12.3 0-17z"
class=""></path>
</clipPath>
</defs>
</svg>
</div>
Put the padding on the li and stretch the left of the a out by a large fixed amount.
Illustrated:
body > ul {
border: 1px solid #eee;
border-radius: 2px;
margin: 1em;
padding: 1em;
overflow: hidden;
}
ul {
list-style: none;
padding: 0;
}
li {
padding-left: 15px;
}
a {
display: block;
margin-left: -150px;
margin-right: -1em;
padding-left: 150px;
}
a:hover {
background-color: #cff;
}
<ul>
<li>
nested
<ul>
<li>
navigation
<ul>
<li>
forever
<ul>
<li>
and
<ul>
<li>
ever
</ul>
</ul>
</ul>
<li>
foo
<li>
bar
</ul>
<li>
baz
</ul>
(Notice how the clickable area of each link extends all the way to the left.)
you can use this code for solve your problem
.jdm-nav li {
font-size: 16px;
position: relative;
}
.jdm-nav li{
font-size:1.2em;
padding-left: 3em;
}
.jdm-nav li a {
background-color:red;
font-size:16px;
}
<ul class="jdm-nav">
<li>
<a>Item1</a>
<ul>
<li>
<a>Item2</a>
<ul>
<li>
<a>Item3</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
use pseudo function of css to increase the padding of every children by 15px.
I'm having an issue with my anchors not covering full width of the sidebar (for instance, 'Search'). I realise that this is because of padding, but I'm not sure how to overcome the issue whilst keeping the padded effect. I have tried to add a class (like, '.no-submenu) on the <li> and did .no-submenu a { margin: 0 -1.5rem; padding: 0 3rem; }, but it only partially solved the problem - my icons were off, they go all the way to the left:
I couldn't find the place online to upload SVG files (my icons are SVG), so I created a minimal example without icons. Icons are handled as background-image on the anchors.
<div class="sidebar">
<nav>
<ul class="menu">
<li id="search">
<a class="sidebar-icon" href="/search/notes/">Search</a>
</li>
<li id="dashboard">
<a class="sidebar-icon">Dashboard</a>
</li>
<li id="notebooks" class="has-submenu">
<div class="menu-item">
<span class="sidebar-icon">Notebooks</span>
<span class="arrow"></span>
</div>
<ul class="submenu">
<li id="new-notebook"><a class="sidebar-icon" href="/notebooks/create/">New Notebook</a></li>
<li id="view-notebooks"><a class="sidebar-icon" href="/notebooks/">View Notebooks</a></li>
</ul>
</li>
<li id="tags" class="has-submenu">
<div class="menu-item">
<span class="sidebar-icon">Tags</span>
<span class="arrow"></span>
</div>
<ul class="submenu">
<li id="view-tags"><a class="sidebar-icon" href="/tags/">View Tags</a></li>
</ul>
</li>
<li id="account" class="has-submenu">
<div class="menu-item">
<span class="sidebar-icon">Account</span>
<span class="arrow"></span>
</div>
<ul class="submenu">
<li id="settings"><a class="sidebar-icon" href="/accounts/settings/">Settings</a></li>
<li id="logout"><a class="sidebar-icon" href="/accounts/logout/">Logout</a></li>
</ul>
</li>
</ul>
</nav>
</div>
CSS:
#import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,600);
#font-face { font-family: "ionicons"; src: url("https://code.ionicframework.com/ionicons/2.0.1/fonts/ionicons.eot?v=2.0.1"); src: url("https://code.ionicframework.com/ionicons/2.0.1/fonts/ionicons.eot?v=2.0.1#iefix") format("embedded-opentype"), url("https://code.ionicframework.com/ionicons/2.0.1/fonts/ionicons.ttf?v=2.0.1") format("truetype"), url("https://code.ionicframework.com/ionicons/2.0.1/fonts/ionicons.woff?v=2.0.1") format("woff"), url("https://code.ionicframework.com/ionicons/2.0.1/fonts/ionicons.svg?v=2.0.1#Ionicons") format("svg"); font-weight: normal; font-style: normal; }
.sidebar {
width: 200px;
height: 100vh;
padding: 1rem 1.5rem;
background-color: #348ceb;
font-family: 'Source Sans Pro';
font-size: 14px;
position: fixed;
box-sizing: border-box;
}
ul {
list-style: none;
}
ul, li {
padding: 0;
margin: 0;
}
li {
padding: 0.5rem 0;
}
.submenu {
display: none;
overflow-y: hidden;
margin: 0 -1.5rem;
background-color: #3287e3;
}
.submenu li:first-child {
padding: 0.1rem 0 0 1.5rem;
}
.submenu li:last-child {
padding: 0 0 0.1rem 1.5rem;
}
.submenu li {
padding: 0.5rem 1.5rem;
}
.menu > li:hover {
background-color: #3287e3;
}
.menu > li {
position: relative;
color: #F9FAFC;
font-weight: bold;
margin: 0 -1.5rem;
padding: 0.5rem 1.5rem;
}
.menu > li > a {
display: block;
color: #F9FAFC;
text-decoration: none;
font-weight: bold;
}
.menu > li > a:hover {
color: white;
}
.submenu > li > a {
display: block;
color: #F9FAFC;
text-decoration: none;
}
.submenu > li > a:hover {
color: white;
}
.submenu > li {
font-weight: normal !important;
}
.arrow {
display: inline-block;
width: 10px;
transition: all 0.5s ease-in-out;
transform-origin: left center;
font-size: 0.5em;
position: absolute;
right: 0.75em;
}
.arrow:after {
content: '\f125';
font-family: 'ionicons';
color: white;
}
.rotated {
transform: rotate(90deg);
transition: transform 0.5s ease;
}
.menu-item {
display: flex;
align-items: center;
}
.menu-item > span:first-child {
flex: 1;
}
.sidebar-icon {
background-repeat: no-repeat;
padding-left: 25px;
}
JS (jQuery):
$(document).ready(function() {
$('.has-submenu').click(function() {
$(this).toggleClass('active');
$(this).children('.menu-item').children('.arrow').toggleClass('rotated');
if ($(this).hasClass('active')) {
$(this).children('.submenu').slideDown("slow");
}
else {
$(this).children('.submenu').slideUp("slow");
}
});
});
How can I overcome the issue?
Here is the demo.
#EDIT:
so, I want this:
but the menu item has to be fully clickable, i.e. you can click on any part of it and it brings you to, say, Search. To see the area it currently covers, right-click on the anchor or inspect the element.
I guess this is what you want:
<li style="padding: 0.5rem 0" id="search">
<a style="width: 100% !important; padding: none" class="sidebar-icon" href="/search/notes/">
<i style="padding-right: 10px">#</i>Search</a>
</li>
This question already has answers here:
Transitions on the CSS display property
(37 answers)
Closed 5 years ago.
How can I get my transition to work? From other examples I have seen and tried it look like it should be working. I'm wanting a slight delay on the display of the sub-menu so that they are instantly shown when the mouse rolls over them.
.TopMenuBar {
border: none;
background-color: purple;
width: 100%;
margin: 0;
padding: 0;
}
.TopMenuBar>ul {
display: flex;
justify-content: space-between;
margin: 0;
padding: 0;
}
.TopMenuBar>ul,
.dropdown-menu>ul,
.sub-dropdown-menu>ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.TopMenuBar>li {
display: inline;
}
.TopMenuBar a,
.dropdown-menu a,
.sub-dropdown-menu a {
color: white;
text-decoration: none;
padding: 20px;
display: block
}
/* Applys to all links under class TopMenuBar on hover */
.dropdown-menu,
.sub-dropdown-menu {
display: none;
background-color: purple;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
position: absolute;
z-index: 1;
}
/* Applys to all links under class TopMenuBar */
.dropdown-menu a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown:hover .dropdown-menu {
display: block;
transition: all 1s linear 1s;
}
.sub-dropdown {
position: relative;
}
.sub-dropdown-menu {
left: 100%;
top: 0;
white-space: nowrap;
}
<div class="TopMenuBar">
<ul>
<li>Home</li>
<li class="dropdown">Programs
<div class="dropdown-menu">
<ul>
<li>Preschool Mandarin
<li>Grade School Mandarin</li>
<li>High School Mandarin</li>
<li>Weekend Class</li>
<li>Summer Camp</li>
<li class="sub-dropdown">Examination Training
<div class="sub-dropdown-menu">
<ul>
<li>AP Test</li>
<li>YCT Test</li>
<li>HSK Test</li>
</ul>
</div>
</li>
<li class="sub-dropdown">Adult Mandarin
<div class="sub-dropdown-menu">
<ul>
<li>Conversational Mandarin Class</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li class="dropdown">Resources
<div class="dropdown-menu">
<ul>
<li>Chinese Club</li>
<li>Literature</li>
</ul>
</div>
</li>
<li class="dropdown">Contact us
<div class="dropdown-menu">
<ul>
<li>Customer Service</li>
<li>Careers</li>
</ul>
</div>
</li>
<li class="dropdown">Registration
</ul>
</div>
Cause transitions do not work if element is or has display: none; one way around it is to use visibility:hidden; and opacity: 0;
Also it is better practice to define
transition: all 1s linear 1s;
on the element itself not just on the :hover state
Like so:
.TopMenuBar {
border: none;
background-color: purple;
width: 100%;
margin: 0;
padding: 0;
}
.TopMenuBar>ul {
display: flex;
justify-content: space-between;
margin: 0;
padding: 0;
}
.TopMenuBar>ul,
.dropdown-menu>ul,
.sub-dropdown-menu>ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.TopMenuBar>li {
display: inline;
}
.TopMenuBar a,
.dropdown-menu a,
.sub-dropdown-menu a {
color: white;
text-decoration: none;
padding: 20px;
display: block
}
/* Applys to all links under class TopMenuBar on hover */
.dropdown-menu,
.sub-dropdown-menu {
visibility: hidden;
opacity: 0;
background-color: purple;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
position: absolute;
z-index: 1;
transition: all 1s linear 1s;
}
/* Applys to all links under class TopMenuBar */
.dropdown-menu a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown:hover .dropdown-menu {
visibility: visible;
opacity: 1;
}
.sub-dropdown {
position: relative;
}
.sub-dropdown-menu {
left: 100%;
top: 0;
white-space: nowrap;
}
<div class="TopMenuBar">
<ul>
<li>Home</li>
<li class="dropdown">Programs
<div class="dropdown-menu">
<ul>
<li>Preschool Mandarin
<li>Grade School Mandarin</li>
<li>High School Mandarin</li>
<li>Weekend Class</li>
<li>Summer Camp</li>
<li class="sub-dropdown">Examination Training
<div class="sub-dropdown-menu">
<ul>
<li>AP Test</li>
<li>YCT Test</li>
<li>HSK Test</li>
</ul>
</div>
</li>
<li class="sub-dropdown">Adult Mandarin
<div class="sub-dropdown-menu">
<ul>
<li>Conversational Mandarin Class</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li class="dropdown">Resources
<div class="dropdown-menu">
<ul>
<li>Chinese Club</li>
<li>Literature</li>
</ul>
</div>
</li>
<li class="dropdown">Contact us
<div class="dropdown-menu">
<ul>
<li>Customer Service</li>
<li>Careers</li>
</ul>
</div>
</li>
<li class="dropdown">Registration
</ul>
</div>
I have a navbar made up of a ul and some li and a elements, as shown below:
/*Fonts*/
#import url(https://fonts.googleapis.com/css?family=Libre+Baskerville);
/*Header*/
.header-wrapper {
background-color: #696969;
height: 53px;
width: 100%;
top: 0;
margin: 0;
padding: 0;}
.header-nav {
background-color: darkblue;
top: 0;
height: 48px;
margin: 0;
padding: 0;
list-style-type: none;}
.header-nav-element {
float: left;}
.header-nav-element-logo {
height: 48px;}
.header-nav-element-link {
display: block;
text-decoration: none;
color: white;
padding: 14px 16px;
font-family: "Libre Baskerville", serif;
transition-duration: 0.3s}
.active {
background-color: #696969;}
.header-nav-element-link:hover:not(.active) {
background-color: #808080;}
<div class="header-wrapper">
<ul class="header-nav">
<li class="header-nav-element noselect"><img src="../img/indexlogo.JPG" alt="" class="header-nav-element-logo"></li>
<li class="header-nav-element noselect active">Home</li>
<li class="header-nav-element noselect">Bio</li>
<li class="header-nav-element noselect">Stances</li>
<li class="header-nav-element noselect">Solutions</li>
</ul>
</div>
As you can see, the active tab is lighting up on hover, even though I have specified not to. Why is this happening, and how can I fix it?
It's because your :not(.active) is being applied to the a but the active class is on its parent, the li
You could fix it by doing this -
.header-nav-element:not(.active):hover .header-nav-element-link
Or by modifying the HTML by moving the active class to the element with the class header-nav-element-link.
As per the MDN documentation of :not():
This selector only applies to one element; you cannot use it to exclude all ancestors.
.header-nav-element is ancestor of .header-nav-element-link.
Change:
.header-nav-element-link:hover:not(.active) {
background-color: #808080;}
to:
.header-nav-element:hover:not(.active) {
background-color: #808080;}
Corrected snippet:
.header-nav {
background-color: darkblue;
top: 0;
height: 48px;
margin: 0;
padding: 0;
list-style-type: none;
}
.header-nav-element {
float: left;
}
.header-nav-element-logo {
height: 48px;
}
.header-nav-element-link {
display: block;
text-decoration: none;
color: white;
padding: 14px 16px;
font-family: "Libre Baskerville", serif;
transition-duration: 0.3s
}
.active {
background-color: #696969;
}
.header-nav-element:hover:not(.active) {
background-color: #808080;
}
<ul class="header-nav">
<li class="header-nav-element noselect"><img src="../img/indexlogo.JPG" alt="" class="header-nav-element-logo"></li>
<li class="header-nav-element noselect active">Home</li>
<li class="header-nav-element noselect">Bio</li>
<li class="header-nav-element noselect">Stances</li>
<li class="header-nav-element noselect">Solutions</li>
</ul>
I have my custom menu in HTML theme and I want to create same menu but in wordpress.
On my page u can see how it works...
In this menu I use jQuery and Bootstrap dropdown. How to create this menu using wp_nav_menu ?
This is my code (HTML, JS, CSS).
<script type="text/javascript">
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
(function($) {
$.fn.menumaker = function(options) {
var cssmenu = $(this), settings = $.extend({
title: "Menu",
format: "dropdown",
sticky: false
}, options);
return this.each(function() {
cssmenu.prepend('<div id="menu-button">' + settings.title + '</div>');
$(this).find("#menu-button").on('click', function(){
$(this).toggleClass('menu-opened');
var mainmenu = $(this).next('ul');
if (mainmenu.hasClass('open')) {
mainmenu.hide().removeClass('open');
}
else {
mainmenu.show().addClass('open');
if (settings.format === "dropdown") {
mainmenu.find('ul').show();
}
}
});
cssmenu.find('li ul').parent().addClass('has-sub');
multiTg = function() {
cssmenu.find(".has-sub").prepend('<span class="submenu-button"></span>');
cssmenu.find('.submenu-button').on('click', function() {
$(this).toggleClass('submenu-opened');
if ($(this).siblings('ul').hasClass('open')) {
$(this).siblings('ul').removeClass('open').hide();
}
else {
$(this).siblings('ul').addClass('open').show();
}
});
};
if (settings.format === 'multitoggle') multiTg();
else cssmenu.addClass('dropdown');
if (settings.sticky === true) cssmenu.css('position', 'fixed');
resizeFix = function() {
if ($( window ).width() > 768) {
cssmenu.find('ul').show();
}
if ($(window).width() <= 768) {
cssmenu.find('ul').hide().removeClass('open');
}
};
resizeFix();
return $(window).on('resize', resizeFix);
});
};
})(jQuery);
(function($){
$(document).ready(function(){
$("#cssmenu").menumaker({
title: "Menu",
format: "multitoggle"
});
});
})(jQuery);
</script>
/*!
* Start Bootstrap - Simple Sidebar HTML Template (http://startbootstrap.com)
* Code licensed under the Apache License v2.0.
* For details, see http://www.apache.org/licenses/LICENSE-2.0.
*/
/* Toggle Styles */
/* FB, wersja jezykowa */
.fb-language {
position: fixed;
bottom: 20px;
left: 65px;
list-style: none;
}
/* Ikonka jezyka */
.lang-item {
display: inline;
}
/* ikonka fb */
.fb-language img {
display: inline-block;
width: 20px;
height: auto;
}
.fb-language p {
display: inline-block;
color: #999999;
font-size: 12px;
margin-left: 5px;
}
.fb-language a {
text-decoration: none;
color: #999999;
}
.fb-language a:hover {
color: #656565;
text-decoration: underline;
}
/* Kolor linku w menu jezeli jestes na podstronie */
.current_page_item a {
color: black !important;
}
.page_item {
list-style: none !important;
}
.dropdown-toggle .icon-bar {
margin-bottom:4px;
margin-top: 4px;
display: block;
width: 22px;
height: 2px;
background-color: #ffffff;
border-radius: 1px;
}
/* kolor button'a dla mobilnego menu */
button {
background-color: rgb(230, 225, 225) !important;
text-align: left !important;
left: 8px !important;
position: absolute;
}
.navbar-header {
position: fixed !important;
width: 100% !important;
height: 45px !important;
background-color: rgb(255, 255, 255) !important;
border-radius: 0 !important;
margin: 0 auto !important;
text-align: center !important;
}
.navbar-header img {
width: 13% !important;
height: auto !important;
margin: 0 auto !important;
text-align: center !important;
}
#wrapper {
padding-top: 50px;
padding-left: 0;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#wrapper.toggled {
padding-left: 250px;
}
#sidebar-wrapper {
z-index: 1000;
position: fixed;
left: 250px;
width: 0;
height: 100%;
margin-left: -250px;
overflow-y: auto;
background-color: rgb(255, 255, 255);
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
}
#wrapper.toggled #sidebar-wrapper {
width: 250px;
}
#page-content-wrapper {
width: 100%;
background-color: rgb(214, 214, 214);
position: absolute;
padding: 15px;
}
#wrapper.toggled #page-content-wrapper {
position: absolute;
margin-right: -250px;
}
/* Sidebar Styles */
.sidebar-nav {
position: block;
top: 0;
width: 250px;
margin: 0;
padding-top: 9px;
padding-left: 50px;
list-style: none;
text-align: left;
}
.sidebar-nav li {
list-style: none;
text-indent: 20px;
line-height: 40px;
}
.sidebar-nav li a.bez:before {
content: " " !important;
}
.sidebar-nav li a {
display: block;
line-height: 18px;
text-decoration: none;
color: #999999;
}
.sidebar-nav li a:hover {
text-decoration: none;
color: #000;
}
.sidebar-nav li a:active,
.sidebar-nav li a:focus {
text-decoration: none;
}
.sidebar-nav > .sidebar-brand {
font-size: 14px;
line-height: 30px;
}
.sidebar-nav > .sidebar-brand a {
color: #999999;
}
.sidebar-nav > .sidebar-brand a:hover {
color: #000;
background: none;
}
.sidebar-nav > .sidebar-links {
padding-left: 43px;
}
.sidebar-nav2 {
position: block;
top: 0;
width: 250px;
margin: 0;
padding-top: 60px;
padding-left: 80px;
list-style: none;
text-align: left;
list-style: none;
text-transform: lowercase;
}
.sidebar-nav2 li {
text-indent: 20px;
line-height: 24px;
}
.sidebar-nav2 li a {
display: block;
text-decoration: none;
color: #999999;
}
.sidebar-nav2 li a:hover {
text-decoration: none;
color: #000;
}
.sidebar-nav2 li a:active,
.sidebar-nav2 li a:focus {
text-decoration: none;
}
#media(max-width: 769px) {
#wrapper {
padding-top: 70px !important;
}
.sidebar-nav {
padding-top: 5px !important;
margin-top: 0;
}
.fb-language {
position: absolute;
bottom: 20px;
left: 65px;
z-index: 10;
}
}
#media(min-width:769px) {
#wrapper {
padding-left: 250px;
}
#wrapper.toggled {
padding-left: 0;
}
#sidebar-wrapper {
width: 250px;
}
#wrapper.toggled #sidebar-wrapper {
width: 0;
}
#page-content-wrapper {
padding: 20px;
position: relative;
background-color: rgb(214, 214, 214);
}
#wrapper.toggled #page-content-wrapper {
position: relative;
margin-right: 0;
}
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<!-- Sidebar -->
<div class="navbar-header navbar-toggle">
<button href="#menu-toggle" class="btn btn-xs btn-default dropdown-toggle" id="menu-toggle">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</a>
</div>
<div id="sidebar-wrapper">
<div class="sidebar-nav">
<li class="sidebar-brand"><span class="brand"></span> ARCHITEKTURA</li>
<ul class="collapse" id="arch">
<li>jednorodzinne</li>
<ul class="collapse" id="jedno">
<li>dom opolski</li>
<li>dom 2</li>
<li>dom 3</li>
</ul>
<li>wielorodzinne</li>
<ul class="collapse" id="wielo">
<li>dom 1</li>
<li>dom 2</li>
<li>dom 3</li>
</ul>
<li>usługowe</li>
<ul class="collapse" id="uslu">
<li>przedszkole w Chróścicach</li>
<li>obiekt 2</li>
<li>obiekt 3</li>
</ul>
</ul>
<li class="sidebar-brand">PORT DESIGN</li>
<ul class="collapse" id="design">
<li>meble</li>
<ul class="collapse" id="meble">
<li>stół 1</li>
<li>stół 2</li>
<li>krzesło 1</li>
</ul>
<li>ceramika</li>
<ul class="collapse" id="ceramika">
<li>ceramika 1</li>
<li>ceramika 2</li>
<li>ceramika 3</li>
</ul>
<li>grafika</li>
<ul class="collapse" id="grafika">
<li>grafika 1</li>
<li>grafika 2</li>
<li>grafika 3</li>
</ul>
<li>inne</li>
<ul class="collapse" id="inne">
<li>inne 1</li>
<li>inne 2</li>
<li>inne 3</li>
</ul>
</ul>
<li class="sidebar-brand">PORT MIEJSCE</li>
<ul class="collapse" id="miejsce">
<li>kawiarnia</li>
<ul class="collapse" id="kawiarnia">
<li>kawa</li>
<li>śniadanie</li>
<li>wydarzenia</li>
</ul>
<li>coworking</li>
<ul class="collapse" id="coworking">
<li>coworking 1</li>
<li>coworking 2</li>
<li>coworking 3</li>
</ul>
<li>warsztat</li>
<ul class="collapse" id="warsztat">
<li>warsztat 1</li>
<li>warsztat 2</li>
<li>warsztat 3</li>
</ul>
<li>cennik</li>
</ul>
</div>
</div>
</div>
I'm using the wordpress bootstrap navwalker for using the bootstrap menu. You can easily edit the walker.
Try https://github.com/twittem/wp-bootstrap-navwalker
~ Neme
In WordPress, the menus are usually done by using the admin dashboard UI. Using appearance -> Menus.
The drop-down ability is done by indenting sub-menu items to the parent menu item. Then it's either you add you own JavaScript to manipulate how the current menu behave, or your current theme could be doing it exactly how you want it.
I'd look into using the right theme as well.