Code to use jquery to make parent menu item to be clickable - php

On the page: Make parent menu clickable, the jquery:
jQuery(document).ready(function() {
jQuery('ul li > a.dropdown-toggle').addClass('disabled');
});
...was given as an alternate solution. What I'd like to know is, can one then just add "....addClass('enabled');" instead, to make the parent menu item clickable?
I don't want to put a custom link in the menu, I don't want to modify bootstrap files, I don't want to modify any other files except the external jquery (".js") file in my own WordPress Theme, as well as my theme files (header.php, page.php, etc.) of course.
I have tried to add the dropdown-toggle class to the wp_nav_menu like this:
menu_class' => 'list_no_decoration dropdown-toggle',
and then simply modified the above code to:
jQuery('ul li > a.dropdown-toggle').addClass('enabled');
But sadly, the parent menu item is still not clickable. I really like this solution as it is very simple and can easily be repeated in other themes i end up designing.
My current code looks as follows:
header.php
<div class="container-fluid" id="header_nav">
<div class="row" id="main_nav_row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="header_nav_col">
<span class="glyphicon glyphicon-chevron-up"></span><span class="glyphicon glyphicon-chevron-down"></span>
<?php
wp_nav_menu(
array(
'sort_column' => 'menu_order',
'menu' => 'Categories',
'container_class' => 'main-menu',
'container_id' => 'header',
'theme_location' => 'header',
'menu_class' => 'list_no_decoration',
'menu_id' => 'top_nav',
)
);
?>
</div>
</div>
</div>
style.css
ul.list_no_decoration { /* "list_no_decoration" as per menu_class in wp_nav_menu */
list-style: none;
border:0; /* Reset */
margin:0; /* Reset */
padding:0; /* Reset */
outline:0; /* Reset */
}
ul.list_no_decoration#top_nav {
vertical-align: top;
top:0;
width:99.9%;
padding-left: 0.05%;
margin: 0 auto;
display: inline-block;
font-size: 0;
text-align: center;
}
ul.list_no_decoration#top_nav li {
display: inline-block;
vertical-align: top;
border: 1px solid white;
padding: 0 10px;
line-height: 30px;
height: 30px;
width: 120px;
}
ul.list_no_decoration#top_nav li a {
font-size: 10pt;
font-weight: bold;
color: #fff;
text-decoration: none;
}
ul.list_no_decoration#top_nav li > a:link {
}
ul.list_no_decoration#top_nav a:visited {
font-size: 14pt;
text-decoration: none;
color: #fff;
}
ul.list_no_decoration#top_nav a:active {
font-size: 14pt;
text-decoration: none;
color: #fff;
}
ul.list_no_decoration#top_nav > li > a:hover {
}
/* sub-level menu item */
ul.sub-menu {
position: absolute;
width: 120px;
border:0; /* Reset */
margin:0; /* Reset */
padding:0; /* Reset */
outline:0; /* Reset */
}
ul.sub-menu li {
position: relative;
display: block;
border:0; /* Reset */
margin:0; /* Reset */
padding:0; /* Reset */
outline:0; /* Reset */
top: 30px;
left: -11px;
background-color:black;
}
ul.list_no_decoration#top_nav > li ul {
vertical-align: top;
top: 0;
border:0; /* Reset */
margin:0; /* Reset */
padding:0; /* Reset */
outline:0; /* Reset */
display: none; /* display: none; to make it invisible until hover */
}
ul.list_no_decoration#top_nav > li:hover ul {
display: block;
}
Here's what the menu currently looks like:
enter image description here

just make your HTML like this dont put <a href=" "> in your dropdown only the parent menu
<nav>
<ul id="menu">
<li>MENU
<ul>
<li>display 1</li>
<li>display 2</li>
<li>display 3</li>
<li>display 4</li>
</ul>
</li>
</ul>
</nav>

Related

How to make a responsive bar out of a dynamic values coming from the database

Good day! I'm having a problem doing a responsive navigation bar coming from HTML, PHP, and CSS only. (No DIVS/Navbar) As you can see below in the OUTPUT image. WHen I used a mobile phone or resize my browser into smaller one. It will output my values like in the OUTPUT image. What I want is to when I use mobile or just make my browser smaller. There will be a button containing all the values and hwen its clicked. it will breakdown all my values like this. This is an example of my desired output. Please see my desired output
Here's my CSS
ul {
list-style: none;
padding: 0;
margin: 0;
background: #f8f8f8;
}
ul li {
display: block;
position: relative;
float: left;
background: #f8f8f8;
}
/* This hides the dropdowns */
li ul { display: none; }
ul li a {
display: block;
padding: 1em;
text-decoration: none;
white-space: nowrap;
color: #000000;
}
ul li a:hover { background: #e4e3e3; }
/* Display the dropdown */
li:hover > ul {
display: block;
position: absolute;
}
li:hover li { float: none; }
li:hover a { background: #f8f8f8; }
li:hover li a:hover { background: #e4e3e3; }
.main-navigation li ul li {
color: black;
position: relative !important;
z-index: 9999 !important;
border-top: 0; }
/* Displays second level dropdowns to the right of the first level dropdown */
ul ul ul {
left: 100%;
top: 0;
}
/* Simple clearfix */
ul:before,
ul:after {
content: " "; /* 1 */
display: table; /* 2 */
}
ul:after { clear: both; }
Here's my HTML, PHP Code:
<?php
function get_menu_tree($parentID)
{
global $con;
$menu = "";
$sqlquery = " SELECT * FROM category where parentID='" .$parentID . "' ";
$res=mysqli_query($con,$sqlquery);
while($row=mysqli_fetch_array($res,MYSQLI_ASSOC))
{
$menu .="<li><a href='base?page=post&categoryID=".$row['categoryID']."'>".$row['name']."</a>";
$menu .= "<ul>".get_menu_tree($row['categoryID'])."</ul>"; //call recursively
$menu .= "</li>";
}
return $menu;
}
?>
Here's how I call the function from my HTML code:
<div class="container">
<ul class="main-navigation">
<?php echo get_menu_tree(0); ?>
</ul>
</div>
</div>
This my DATABASE structure
This is my OUTPUT out of the following codes
I think the issue is that on desktop you are relying on the :hover to reveal the dropdown. For mobile there is no hover state and so you would need to either use JavaScript (or styled form checkboxes to simulate it with html/css) that would make it a tap to reveal sub items.
This would also mean that you would not be able to click through to the top level item on dropdowns as that would expand the dropdown rather than be a link. The work around could be to make a dropdown chevron on the right that would be it's own button to reveal the sub menu on mobile and the text still link through. This is the solution I used on https://www.firetechcamp.com/
Unfortunately, without adding at least some javascript, this is the closest i could get to a pure css / html solution:
try resizing the browser to see the responsive solution
ul {
list-style: none;
padding: 0;
margin: 0;
background: #f8f8f8;
font-family: Tahoma, Arial;
}
ul li {
display: block;
position: relative;
float: left;
background: #f8f8f8;
}
/* This hides the dropdowns */
li ul { display: none; }
ul li a {
display: block;
padding: 1em;
text-decoration: none;
white-space: nowrap;
color: #000000;
}
ul li a:hover { background: #e4e3e3; }
/* Display the dropdown */
li:hover > ul {
display: block;
position: absolute;
}
#media( max-width: 600px ){
ul li{
float:none;
transition: all 0.3s ease forwards;
}
ul li a{
color: white;
}
ul>li:first-child{
background: #4caf50;
padding-right: 20px;
position: relative;
}
ul:hover li{
opacity: 1 !important;
position: relative !important;
z-index: 1 !important
}
ul>li:first-child:after{
content:"☰";
position: absolute;
top:0;
height: 100%;
right: 0;
margin: 0;
color: white;
background: #555555;
width: 50px;
font-size: 30px;
text-align: center;
line-height: 50px;
cursor: red;
}
ul>li:not(:first-child){
position: fixed;
z-index: -1;
background: #333333;
opacity: 0;
}
}
<ul>
<li>Home</li>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
</ul>

Drop down menu on navigation bar. -Drop down button not aligned with rest of navigation buttons. HTML

As you can see, I commented out the code for the dropdown menu, the menu works. But it isn't aligned with the rest of the navigation bar. Any help is appreciated.
<div id="top_nav">
<div id="top_nav_container">
<ul>
<li>Home</li>
<li>Redacted</li>
<li>How It Works</li>
<li>FAQs</li>
<li>Special Offers</li>
<li>Customer Service</li>
<!-- <span class="dropdown">
<span class="dropbtn">Special Offers</span>
<span class="dropdown-content">
Link 1
Link 2
Link 3
</span> -->
</ul>
</div>
Here's the CSS - the original is towards the bottom (after "cont.", the top was added later from an external source)
/* TOP NAV */
/* dd
/* Style The Dropdown Button */
.dropbtn {
background-color: #5d72b3;
color: white;
margin:auto;
padding: 0px;
text-align:center;
font-size: 14px;
border: none;
cursor: pointer;
text-decoration:none;
border-right:1px solid #FFF;
padding:9px 12px 0 12px;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
/*________________________cont. */
#top_nav_container {
position:relative;
width:1000px;
margin:auto;
height:33px;
}
#top_nav {
width:1000px;
height:33px;
background-color:#5d72b3;
border-bottom:1px solid #FFF;
border-top:1px solid #FFF;
margin:auto;
}
#top_nav ul {
width:849px;
margin:0;
padding:0;
list-style:none;
}
#top_nav ul li {float: left;}
#top_nav ul li a {
display:block;
width:auto;
height:24px;
text-align:center;
font-size:14px;
font-family:Arial, Helvetica, sans-serif;
color:#FFF;
text-decoration:none;
border-right:1px solid #FFF;
padding:9px 12px 0 12px;
}
#top_nav ul li a:hover {
background-color:#86a3ff;
}
#sub_nav_bar {
width:1000px;
height:40px;
margin:0 auto 6px auto;
text-align:center;
}
Redacted company info djdjsjshsjshhshshshshshshshhshshshshshshshshshhshshs
Sjshshsjs
Hshshshshshshsh jsjsjsjsjhshss
.dropbtn { display: block;
height: 24px; }

Link not clickable in Wordpress Menu but can be opened in a new tab

Disclaimer: I am sorry the post looks too big but I just thought I should provide all the details. I am sure the issue is something very stupid but I am not able to figure out :(
Background: I am creating a wordpress site using an awesome theme called Parallax from Access Press. I wanted to play out my business website before buying the pro version so decided to make a few changes myself.
Problem: You can either use the theme's menu (which allows animation and same page scroll; This Golum wantses) or use native wordpress menus (which does not allow scrolling; Golum hateses).
Workaround Tried: I figured I can go into the header.php and add one menu item manually. So following is the code per my understanding:
<nav id="site-navigation" class="main-navigation">
<div class="menu-toggle"><?php _e( 'Menu', 'accesspress_parallax' ); ?></div>
<?php
$sections = of_get_option('parallax_section');
if(of_get_option('enable_parallax') == 1 && of_get_option('enable_parallax_nav') == 1):
?>
<ul class="nav single-page-nav">
<?php
$home_text = of_get_option('home_text');
if(of_get_option('show_slider')== "yes" && !empty($home_text)) : ?>
<li class="current"><?php echo esc_attr($home_text); ?></li>
<?php endif;
if(!empty($sections)):
foreach ($sections as $single_sections):
if($single_sections['layout'] != "action_template" && $single_sections['layout'] != "blank_template" && $single_sections['layout'] != "googlemap_template" && !empty($single_sections['page'])) :
$title = get_the_title($single_sections['page']); ?>
<li><?php echo $title; ?></li>
<?php
endif;
endforeach;
endif; ?>
<li>Pre-order Here</li>
</ul>
<?php
else:
<some more code that picks custom menu if you choose to skip parallax menu>
</nav>
The first LI item just puts the text 'Home' in the page. Second one picks all the values from parallax menu. The Last LI item is the one I have included.
<li>Pre-order Here</li>
Behavior: This perfectly adds the item to the menu and also appears to be clickable. The problem is - when it is clicked, it does nothing. If I right click it and open in a new tab, it works perfectly fine. Including related CSS below for your reference
Edit: If I include a link to another section of the page. it works. Example: http://domain.com/#section-20
.main-navigation {
float: right;
max-width: 80%;
margin-top: 15px;
}
.logo-top .main-navigation{
float: none;
width: auto;
text-align: center;
max-width: none;
}
.main-navigation ul {
list-style: none;
margin: 0;
padding-left: 0;
display: inline-block;
}
.main-navigation ul li{
position: relative;
}
.main-navigation > ul > li {
float: left;
position: relative;
font-family: 'Oxygen', sans-serif;
font-weight: 400;
}
.main-navigation > ul > li > a{
text-transform: uppercase;
font-size:14px;
padding: 5px 15px;
}
.main-navigation > ul > li.current a{
color: #E66432;
}
.main-navigation a {
display: block;
text-decoration: none;
color:#333;
}
.main-navigation ul ul {
background: #FFF;
position: absolute;
top: 100%;
left:0;
z-index: 99999;
padding:0 10px;
font-size: 15px;
box-shadow: 1px 1px 2px rgba(0,0,0,0.3);
display: none;
}
.main-navigation ul ul ul {
left: 100%;
top: 0;
}
.main-navigation ul ul a {
min-width: 200px;
}
.main-navigation ul ul li {
border-bottom: 1px solid #EEE;
padding: 8px 0;
}
.main-navigation ul ul li:last-child{
border:none;
}
.main-navigation li:hover > a {
color: #E66432;
}
.main-navigation ul li:hover > ul {
display: block;
}
.main-navigation .current_page_item > a,
.main-navigation .current-menu-item > a {
color: #E66432;
Thanks #pschueller and #dingo_d!!
You pushed me into the correct direction. The github to the JS plugin for scroll has the secret sauce.
I had to add a class='external' to my tag with the href and update the jquery.nav.js with the following:
Replaced
filter: '',
with
filter: ':not(.external)',

Menu bar flicker when goes to different page

Can anyone suggest what might be the problem on my flickering menu bar?
Please suggest anything that will make the flickering of menu bar stop.
Thanks much!
#mainmenu{
margin-bottom: 2.5em;
}
.menubar{
position: fixed;
top:0;
left:0;
max-height:10em;
width:100%;
list-style: none;
background-color:#333333;
text-align: left;
margin:0;
padding: 0;
z-index: 1;
border-bottom: 1px solid #ccc;
}
.menubar .first {
margin-left: 1em;
}
.menubar li{
position: relative;
display: inline-block;
width:15em;
text-align: center;
font-family: 'Oswald', sans-serif;
font-size: 1em;
border-bottom: none;
cursor: pointer;
}
.menubar li:hover{
background-color:#6666ff;
}
.menubar a{
display: block;
padding: 0.5em 0.5em 0.5em 0.5em;
text-decoration: none;
color:#ffffff;
}
/* for submenus */
.menubar .first .submenubar {
padding: 0;
position: absolute;
top:2em;
left:0;
width:auto;
display: none;
-webkit-box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
-moz-box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
}
.menubar li .submenubar li {
text-align: left;
list-style-type: none;
background-color:brown;
display: block;
color:#fff;
}
.menubar > li > .submenubar > li:hover {
background-color:black;
}
.menubar li:hover .submenubar{
display: block;
}
See this JsFiddle for my complete code.
I suspect that one of 2 things are happening.
Is the whole header "flickering" when you go to a new page? If so,
that's because you are building an html page from your php on the
server and then rendering the page again. Sometimes this will flash.
Sucks.
The sub-menu appears to "flicker" because it's broken and when you
try and hove over it, it disappears.
If it's 1, you can use caching to try and lessen the chances of this happening, or you can learn how to use ajax, or a js framework to build single page apps, but that's a lot of work.
If it's 2, then this code I'll include below, and this fiddle - will set you up with some more solid code to work with.
My real advice, is to just never use drop-down menus. They are a terrible interface pattern.
HTML
<nav class='container navigation'>
<div class='inner-w'>
<ul class='menu'>
<li>
<a href='#'>Top-level menu item 1</a>
<ul class='sub-menu'>
<li><a href='#'>Sub-menu item 1</a></li>
<li><a href='#'>Sub-menu item 2</a></li>
<li><a href='#'>Sub-menu item 3</a></li>
<li><a href='#'>Sub-menu item 4</a></li>
</ul>
</li>
<li><a href='#'>Top-level menu item 2</a></li>
<li>
<a href='#'>Top-level menu item 3</a>
<ul class='sub-menu'>
<li><a href='#'>Sub-menu item 1</a></li>
<li><a href='#'>Sub-menu item 2</a></li>
</ul>
</li>
<li><a href='#'>Top-level menu item 4</a></li>
</ul>
</div>
</nav>
CSS
/* global-structure */
.container {
width: 100%;
float: left;
}
.container .inner-w {
margin-right: auto; margin-left: auto;
max-width: 900px; /* arbitrary */
/* this should have a clear-fix */
}
/* menu styles */
.menu, .menu ul {
list-style: none;
margin: 0;
padding: 0;
}
.menu li {
float: left;
}
.menu > li { /* just the top-level li */
position: relative;
/* so the sub-menu can be positioned to this */
border-right: 1px solid rgba(0,0,0,.3)
}
.menu > li:last-child {
border-right: 0;
}
.menu a {
display: block;
padding: .8rem .5rem;
background: #eee;
min-width: 160px;
color: inherit;
text-decoration: none;
}
.sub-menu {
position: absolute;
top: 100%;
left: 0;
height: 0;
width: 0; /* just hide it visually */
overflow: hidden;
z-index: 5; /* arbitrary, keep them small though... */
}
.sub-menu li {
clear: left;
border-bottom: 1px solid rgba(0,0,0,.3)
}
.sub-menu li:last-child {
border-bottom: 0;
}
.sub-menu a {
background: #ccc;
}
.sub-menu a:hover {
background: #aaa;
}
.menu > li:hover .sub-menu {
height: auto;
width: auto;
}
If I was absolutely forced to write a drop-down menu, It would have to be like this: http://codepen.io/sheriffderek/pen/qdBryb/?editors=010

CSS menu options

I am creating a menu bar with rollovers, when i roll over the option the background and text colour change but when i move my mouse down to the sub menu that appeared with hovering on the menu the text colour changes back but the background stays.
This is my HTML for the menu bar:
<div id="menuBar">
<div id="nav">
<div id="nav_wrapper">
<ul>
<li class="bar">Home
</li>
<li class="bar"> Services
</li>
<li class="bar"> Sales →
<ul>
<li class="bar">dropdown #1 item #1
</li>
<li class="bar">dropdown #1 item #2
</li>
<li class="bar">dropdown #1 item #3
</li>
</ul>
</li>
<li class="bar"> Repairs →
<ul>
<li class="bar">Bar
</li>
<li class="bar">Kitchen
</li>
<li class="bar">dropdown #2 item #3
</li>
</ul>
</li>
<li class="bar"> Installations
</li>
<li class="bar"> Contact Us
</li>
</ul>
</div>
<!-- Nav wrapper end -->
And the CSS that im using:
#menuBar {
background:#FF0;
box-shadow:5px 5px 2px #888888;
position:absolute;
height:55px;
width:1000px;
left:0;
right:0;
margin:auto;
top:253px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
-khtml-border-radius: 5px;
border-radius: 5px;
behavior: url(/css/border-radius.htc);
z-index:5;
}
#nav {
padding: 0;
margin: 0;
font-family: Arial;
font-size: 21px;
left:0;
right:0;
margin:auto;
}
#nav_wrapper {
width: 960px;
margin: 0 auto;
text-align: center;
}
#nav a {
color:#0000FF;
}
#nav a:hover {
color:#FF0;
}
#nav a:active {
color:#FF0;
}
#nav a::focus {
color:#FF0;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
min-width: 200px;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #00F;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-khtml-border-radius: 10px;
color:#FF0;
border-radius: 10px;
behavior: url(/CSS/border-radius.htc);
}
#nav ul li a, visited {
display: block;
padding: 15px;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
-khtml-border-radius: 15px;
color:#FF0;
border-radius: 15px;
behavior: url(/CSS/border-radius.htc);
}
#nav ul ul {
display: none;
position: absolute;
background-color: #FFFF00;
border: 3px solid #00F;
border-top: 1;
margin-left: -5px;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:hover {
color:#FF0;
}
.bar {
color:#FF0;
}
Im sure that im missing something simple but i cant find it at all.
Help!
Building on what Niet the Dark Absol said:
you would need to add styles (notice the > - this selects ONLY direct children, works with IE8+ and elsewhere)
#nav ul li:hover > a {
color: #ff0;
}
and you can probably remove this because it would be redundant:
#nav ul ul li a:hover {
color:#FF0;
}
You are applying the color change to a:hover, but the background-color change to li:hover.
Both must be on the li:hover to work. You may need to add color:inherit to the a elements within the menu to ensure natural link colours don't override the setting.

Categories