jQuery .toggle() child issue - javascript

I am having an issue where my jQuery will toggle when clicking within .mini-nav li rather than just .nav ul li. What would be the best way to fix this issue?
HTML:
<div class="nav">
<ul>
<li>
<img src="./assets/icon-down_arrow.png" />LoLNode
<ul class="mini-nav">
<li>Home</li><br />
<li>Popular</li><br />
<li>Create</li><br />
</ul>
</li>
</ul>
</div>
CSS:
.nav {
padding:5px 0;
background: -webkit-linear-gradient(#333, #222);
background: -o-linear-gradient(#333, #222);
background: -moz-linear-gradient(#333, #222);
background: linear-gradient(#333, #222);
}
.nav > ul {
width:1000px;
margin:auto auto;
list-style-type:none;
}
.nav ul > li {
display:inline-block;
padding:10px 5px;
font-size:16px;
color:#FFF;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
}
.nav ul li > .mini-nav {
min-width:92px;
position:absolute;
top:51px;
list-style-type:none;
background:#222;
border-bottom-left-radius:5px;
border-bottom-right-radius:5px;
display:none;
}
.nav ul li .mini-nav > li {
display:inline-block;
padding:2px 5px;
background:#222;
border-bottom-left-radius:5px;
border-bottom-right-radius:5px;
font-size:12px !important;
color:#FFF;
}
jQuery:
$('.nav ul > li:first-child').on('click', function() {
$('ul.mini-nav').toggle();
});

You need to stop event propagation when you click inner li, so that event doesn't bubble up to the outer list where it's handled, toggling inner one:
$('.nav > ul > li:first-child').on('click', function () {
$('ul.mini-nav').toggle();
});
$('ul.mini-nav').on('click', function (e) {
e.stopPropagation();
});
Demo: http://jsfiddle.net/8op8qt8w/
Also note that I fixed selector a little
.nav > ul > li:first-child
^-- here
One more thing: remove <br> after li. You want to make li block instead of inline-block if you want them to be rendered on the next line.

Related

How to hide currently selected li item from being an option when user clicks dropdown again?

So let's say user opens menu and chooses #4. When he opens the menu again while #4 is selected, the menu would ideally hide that #4 option (because it seems redundant to have the option still there). How can this be achieved in the code?
http://jsfiddle.net/j8oawqL4/
HTML
<ul class="navbar cf">
<li>
Category
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
</li>
</ul>
</div>
CSS
ul.navbar {
border-style:solid;
border-width:1px;
border-color:#739FE0;
width: 100px; /*Widthchanger1*/
border-radius: 4px;
margin-left:0px;
margin-right:0px;
font-size:14px;
height:33px;
}
ul.navbar li a.ActiveListItem {
background:url(../images/downarrowblue.png) !important;
background-repeat: no-repeat !important;
background-size: 10px 10px !important;
background-position: 83px 13px !important;
color:white; !important;
background-color:#222 !important;
padding:7.5px 0px !important;
font-weight:normal !important;
margin-left:0px; /*Widthchanger2, got the activeitem centered with list text this way*/
margin-right:0px;
border-radius: 4px;
height:18px;
width:100px; /*kinda messes with width of text*/
margin-bottom:1px;
}
ul.navbar li {
position: relative;
width:100px; /*Changes width of actual list*/
}
ul.navbar li a {
display: block;
color: white;
padding:10px 5px;
text-decoration:none;
transition: all .1s ease-in;
}
ul.navbar li a:hover,
ul.navbar li:hover > a {
/*background:black; */
background:#739FE0;
color:#FFFFFF;
/*font-weight:600;*/
/*border-bottom-color:#FFFFFF;
border-bottom-style:solid;*/
/*border-color:#FFFFFF;
border-style:solid;
border-width:1px; */
}
ul.navbar li ul {
margin-top: 0px; /*Controls space from listdropdown to listchooser*/
position: absolute;
background: #222;
font-size: 14px;
/* min-width: 200px; */
display: none;
z-index: 99;
box-shadow: inset 0 0px 3px rgba(0,0,0,.6),
0 5px 10px rgba(0,0,0,.6);
}
ol, ul { list-style: outside none none; }
.hidden { display: none; }
JS
$(function() {
$('.navbar ul li a').click(function(){
$('.navbar > li:first-child > a').text($(this).text());
$('.navbar > li > ul').addClass('hidden');
$('.navbar li ul').slideToggle(100);
});
$('.navbar > li').mouseenter(function(){
$(this).find('ul').removeClass('hidden');
});
$('.ActiveListItem').click(function(){
$('.navbar li ul').slideToggle(300);
});
});
add class with style to hide it on click, and when it click some other remove it
JS
$('.navbar ul li a').click(function(event){
$('.navbar > li:first-child > a').text($(this).text());
$('.navbar > li > ul').addClass('hidden');
$('.navbar li ul').slideToggle(100);
$('.navbar ul li.gone').removeClass("gone");
$(event.taget).closest("li").addClass("gone")
});
CSS
.navbar ul li.gone { display: none; }
Another approach would be, if an option has been clicked show first all options in case there were hidden elements set before
// show all hidden options
$('.navbar ul li a').show();
Then you can now hide the selected option
// hide selected option
$(this).hide();
Code :
$(function() {
$('.navbar ul li a').click(function(){
$('.navbar > li:first-child > a').text($(this).text());
$('.navbar > li > ul').addClass('hidden');
// show all hidden options
$('.navbar ul li a').show();
// hide selected option
$(this).hide();
$('.navbar li ul').slideToggle(100);
});
$('.navbar > li').mouseenter(function(){
$(this).find('ul').removeClass('hidden');
});
$('.ActiveListItem').click(function(){
$('.navbar li ul').slideToggle(300);
});
});
Fiddle

How to expand and collapse a responsive mobile menu

I have created a responsive menu, all that is left is to make it toggle. I am unsure as how to show the menu when the word menu is pressed. By default the menu is collapsed, then when the menu button is pressed it expands and when the button is pressed again it collapases.
Could someone please help me out as I am new and unsure about this. I have uploaded my project onto jsfiddle. link
http://jsfiddle.net/bLbdavqu/2/
HTML:
<div id="nav"><!--nav-->
<div id="pull">
Menu
</div>
<ul>
<li>Home</li>
<li class="active">About Gnosis
<ul>
<li>What is Gnosis</li>
<li>Origins of Gnosis</li>
<li>Foundations</li>
</ul>
</li>
<li>Articles</li>
<li>FAQ</li>
<li>Courses</li>
<li>Centres</li>
<li>International</li>
</ul>
</div><!--/nav-->
CSS:
body {
background:brown;
}
#pull {
display:none;
float:left;
height:auto;
width:100%;
}
#pull img {
float:right;
}
#nav{
float:left;
height:155px;
width:63%;
background:url(../images/top-banner.png) no-repeat;
background-position:bottom;
background-size:100%;
position:relative;
}
#nav ul {
height:auto;
width:100%;
list-style-type:none;
position:absolute;
bottom:0;
*zoom:1;
}
#nav ul li {
float:left;
padding-left:20px;
position:relative;
}
#nav ul li a {
font-family:Trajan-Pro;
font-size:0.9em;
color:#fff;
text-decoration:none;
}
#nav ul li a:hover {
color:#d2aa76;
border-bottom:2px solid #d2aa76;
padding-bottom:5px;
}
#nav ul li:hover > a {
color:#d2aa76;
border-bottom:2px solid #d2aa76;
padding-bottom:5px;
}
/*1st level sub-menu */
#nav ul ul {
display:none;
}
#nav ul li:hover > ul {
height:auto;
width:180px;
display:block;
}
#nav ul ul li:hover > a {
color:#d2aa76;
border-bottom:none;
}
#nav ul ul {
padding:0;
position:absolute;
top:100%;
padding-top:15px;
}
#nav ul ul li {
float:none;
position:relative;
padding:7px 0px;
border-top:1px solid #000;
background: #423d33;
background: linear-gradient(top, #423d33 0%, #4a4843 40%);
background: -moz-linear-gradient(top, #423d33 0%, #4a4843 40%);
background: -webkit-linear-gradient(top, #423d33 0%,#4a4843 40%);
-webkit-box-shadow:0px 5px 14px rgba(50, 50, 50, 0.75);
-moz-box-shadow:0px 5px 14px rgba(50, 50, 50, 0.75);
box-shadow:0px 5px 14px rgba(50, 50, 50, 0.75);
transition: all 300ms cubic-bezier(0.175,0.885,0.32,1.275) 0;
}
#nav ul ul li a {
font-size:0.8em;
padding-left:15px;
}
#nav ul ul li a:hover {
border-bottom:none;
}
#media screen and (max-width: 1900px) {
#logo {
height:95px;
width:100%;
background:none;
}
#logo h1 {
top:0;
margin:0;
padding-top:10px;
}
#header-container {
height:auto;
}
#pull {
display:block;
}
#nav {
height:100%;
width:100%;
background:none;
margin:0;
padding:0;
clear:both;
}
#nav ul {
height:100%;
position:static;
margin:0;
padding:0;
display:block;
}
#nav ul > li {
float: none;
}
#nav ul li {
padding-left:0px;
}
#nav ul li a {
display:block;
padding: 9px 9px;
position: relative;
z-index:100;
}
#nav ul ul {
position:relative;
top:0;
bottom:0;
margin:0;
padding:0;
}
#nav ul li:hover > ul {
height:auto;
width:100%;
}
#nav ul ul li {
border-top:none;
background: #615f5b;
-webkit-box-shadow:none;
-moz-box-shadow:none;
box-shadow:none;
}
#nav ul ul li a {
display: block;
padding-left:30px;
position: relative;
z-index: 100;
}
#nav ul > li.hover > ul , .nav li li.hover ul {
position: static;
}
}
You can use javascript to change the attribute:
I made a jsfiddle: http://jsfiddle.net/bLbdavqu/3/
function show(){
var show = document.getElementById('the_menu');
show.setAttribute('style','display:inline');
}
You can wrap it inside a div to make it easier and then change the display from none to inline. Question: do you want to give users the change to hide the menu when they click again on menu?
Or, if you are ready to use jquery, just use toggle:
$(document).ready(function(){
$('button').click(function () {
$("#toggle").toggle();
});});
http://jsfiddle.net/bLbdavqu/5/
<div id="nav"><!--nav-->
<div id="pull">
Menu
</div><br/><br/>
<div id="the_menu">
<ul>
<li>Home</li>
<li class="active">About Gnosis
<ul>
<li>What is Gnosis</li>
<li>Origins of Gnosis</li>
<li>Foundations</li>
</ul>
</li>
<li>Articles</li>
<li>FAQ</li>
<li>Courses</li>
<li>Centres</li>
<li>International</li>
</ul>
</div><!--/nav-->
</div>
Script
<script>
function show(ValueToggle){
if(ValueToggle==1){
$('#the_menu').show();
$('#pulls').attr('onClick','show(0)');
} else {
$('#the_menu').hide();
$('#pulls').attr('onClick','show(1)');
}
}
</script>
You can use JQuery to toggle Menu http://jsfiddle.net/bLbdavqu/8/
Just Change the CSS of the ul.
Hide List- $( "#mylist" ).css("display","none");
Show List- $( "#mylist" ).css("display","block");
I think you want to show the menu items("Home","About Gnosis",etc) on click of link "menu" and
again hide these items on click of link "menu" . I made a jsfiddle for you http://jsfiddle.net/Tushar490/kL1dgvmr/6/
<div id="nav"><!--nav-->
<div id="pull">
Menu
</div>
<div id="bs-example-navbar-collapse-2">
<ul>
<li>Home</li>
<li class="active">About Gnosis
<ul>
<li>What is Gnosis</li>
<li>Origins of Gnosis</li>
<li>Foundations</li>
</ul>
</li>
<li>Articles</li>
<li>FAQ</li>
<li>Courses</li>
<li>Centres</li>
<li>International</li>
</ul>
</div>
</div><!--/nav-->
Script :-
$(document).ready(function () {
$("#pull").on('click',function () {
$("#bs-example-navbar-collapse-2").toggle();
});
});
In CSS you just need to remove "float:left;" from "#pull" .
Hope this would help to you !!
here is the solution please see the code at
$(document).ready(function(){
$('#test').hide();$('#pull').click(function () {$("#test").toggle();});});
http://jsfiddle.net/bLbdavqu/12/
the updation of your requirements.

Highlight current page menu item. Also highlight main menu item when sub menu item is selected

I am working on a project in ASP.net using C#. I have a masterpage in which there is a menu with sub menus for some menu items. I want the current page menu item be highlighted when selected. Also I want the main menu item be highlighted when any of the sub menu items are selected or their link is opened in the same page.
Any idea or solution using CSS or Javascript is expected. I have searched for solution on this forum but my requirements are different so i have failed till now to find a feasible one.
<pre>
Javascript:
<script src="../Scripts/jquery-2.1.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#nav').find('li a').click(function () {
$('#nav').find('li a').removeClass('active');
$(this).addClass('active');
$($(this).closest('li.menu-item').children()[0]).addClass('active');
});
});
</script>
</pre>
<pre>
CSS:
#nav {
height: 50px;
width: auto;
position: relative;
background-color: #ad8f5d;
}
#nav ul li {
width:150px;
text-align:center;
}
#nav ul li a
{
color: Black;
font-size: 13px;
font-weight: bold;
line-height:50px;
text-decoration: none;
}
#nav li a.active
{
color:White;
}
#nav ul li a.has_submenu {
background: transparent url('../Images/submenu-item.jpg') no-repeat scroll right center;
padding-right: 0px;
}
#nav ul li a.has_submenu:hover, #nav ul li.sfHover a.has_submenu {
background-image: url('../Images/submenu-item-hover.jpg');
}
#nav ul ul li a.has_submenu {
background: transparent url('../Images/submenu-item-invert.jpg') no-repeat scroll right center;
padding-right: 0px;
}
#nav ul ul li a.has_submenu:hover, #nav ul ul li.sfHover a.has_submenu {
background-image: url('../Images/submenu-item-hover-invert.jpg');
}
#nav ul ul li a {
float: none;
width: 170px;
}
#nav ul > li:hover > a
{
background-color: #1f478d;
}
#nav ul ul > li:hover > a
{
background-color: #1f478d;
}
/*--------------------------Item Image Hover change---------------------CSS----*/
#nav ul > li:hover > a.has_submenu
{
background-image: url('../Images/submenu-item-hover.jpg');
}
#nav ul ul > li:hover > a.has_submenu
{
background-image: url('../Images/submenu-item-hover-invert.jpg');
}
/*--------------------------Item Image Hover change---------------------CSS ends----*/
#nav ul li a:hover, #nav ul li.sfHover a{
background-color: #1f478d;
}
#nav ul ul a
{
background-color: #ad8f5d;
}
#nav ul li li a:hover, #nav ul li.sfHover li a:hover {
background-color: #1f478d;
}
.sf-menu, .sf-menu *
{
height:50px;
margin: 0;
padding: 0;
list-style: none;
}
.sf-menu ul {
position: absolute;
top: -999em;
width: 200px;
margin-top: 0px;
padding-top: 0;
}
.sf-menu ul li {
width: 100%;
}
.sf-menu li:hover {
visibility: inherit;
}
.sf-menu li {
float: left;
position: relative;
}
.sf-menu a {
display: block;
position: relative;
}
.sf-menu li:hover ul,
.sf-menu li.sfHover ul {
left: 0;
top: 49px;
z-index: 99;
}
ul.sf-menu li:hover li ul,
ul.sf-menu li.sfHover li ul {
top: -999em;
}
ul.sf-menu li li:hover ul,
ul.sf-menu li li.sfHover ul {
left: 170px;
top: 0;
}
ul.sf-menu li li:hover li ul,
ul.sf-menu li li.sfHover li ul {
top: -999em;
}
ul.sf-menu li li li:hover ul,
ul.sf-menu li li li.sfHover ul {
left: 10em;
top: 0;
}
</pre>
<pre>
HTML:
<div id="nav">
<ul class="sf-menu">
<li class="menu-item">Home</li>
<li class="menu-item"><a class="has_submenu" href="#">Examples</a>
<ul>
<li>Static Text Page</li>
<li>Static Frontpage</li>
<li>Another link</li>
</ul>
</li>
<li class="menu-item"><a class="has_submenu" href="#">Products</a>
<ul>
<li>Product One</li>
<li>Product Two</li>
<li>Product Three</li>
</ul>
</li>
<li class="menu-item">Solutions</li>
<li class="menu-item">Contact</li>
</ul>
</div>
</pre>
Use this code, if you need more then ask me.
$(document).ready(function () {
$('.menu-item').click(function () {
alert("S");
$('.menu-item a').css("color","black");
$(this).find('a').css("color","red");
// $($(this).closest('li.menu-item').children()[0]).addClass('active');
});
});

Drop Down Menu Similar to xenForo?

How can I create a dropdown menu similar to xenforo, which will automatically close after 2 seconds?
I've tried searching but unfortunately couldn't find any proper solution.
But I don't know how to and what code to put to achieve like; 1. The drop down menu open after 2 seconds if the mouse cursor hovers over the main menu link for 2 seconds. 2. The drop down closes if the mouse cursor is away from the drop down for 2 seconds.
Thanks!
Here is Jsfiddle;
Try this:
$(document).ready(function(){
$("#sub-menu li").hide(); // Hide in order to fadeIn to work
$("#main-menu ul").hover(
function(){
$("#sub-menu li").fadeIn(slow);
},
function(){
$("#sub-menu li").fadeOut(slow);
});
});
You can use jquery to use this functionality. Instead of waiting for 2 seconds you can use fade out slow so that users can know something will happen.
$(document).ready(function(){
$("#sub-menu").hide(); // Hide in order to fadeIn to work
$("#main-menu").hover(
function(){
$("#sub-menu").fadeIn(slow);
},
function(){
$("#sub-menu").fadeOut(slow);
});
});
<html>
<head>
<style>
.dd_menu {
background: none;
padding: 0px;
margin:0;
list-style-type:none;
height:10px;
border: none;
font-size: 11px;
font-family: "Candarab";
}
.dd_menu li {
background: none;
float: left !important;
height:20px;
margin-left: 1px;
margin-top: 4px;
}
.dd_menu li a {
padding: 15px 5px;
display:inline;
color:#000;
text-decoration:none;
font:11px arial, verdana, sans-serif;
}
.dd_menu li:hover a {
text-decoration:none;
padding: 15px 5px;
}
.dd_menu ul {
position:absolute;
left:-9999px;
top:9px;
list-style-type:none;
text-decoration: none;
float: left !important;
}
.dd_menu li:hover {
position:relative;
background:#176093;
text-decoration: none;
z-index: 1000;
}
.dd_menu li:hover ul {
left:0px;
top:20px;
background:lavender;
padding: 3px 3px;
border:1px solid grey;
width:160px;
text-decoration: none;
}
.dd_menu li:hover ul li {
height:18px;
border:none;
}
.dd_menu li:hover ul li a {
height:18px;
padding:0px;
display:block;
font-size:11px;
width:158px;
line-height:18px;
text-indent:5px;
color:#444;
background-color:lavender;
text-decoration:none;
border:1px solid transparent;
}
.dd_menu li:hover ul li a:hover {
height:18px;
background:silver;
color:#000;
float: left;
border:solid 1px #444;
}
</style>
</head>
<body>
<ul class="dd_menu">
<li>Main Menu <span class="smallesttext">▼</span>
<ul>
<div align="left">
<li>Drop Down Link</li>
<li>Drop Down Link</li>
<li>Drop Down Link</li>
<li>Drop Down Link</li>
<li>Drop Down Link</li>
</div>
</ul>
</li>
</ul>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".dd_menu li a").hover(
function(){
$(".dd_menu li ul div li a").hide().fadeIn('slow');
},
function(){
});
$("ul div li").hover(
function(){
$(".dd_menu li ul div li a").show();
},
function(){
;
});
});
</script>
</body>
</html>
Try this. Similarly add what you want on mouse out. If it helped think about accepting the answer.

Change Menu hover to click/active CSS

I tried to change my menu from hover to click but if i click the menu the sub-menus also close so fast I think I need a javascript for onclick and toggle but don't know how to be done. Please help I'am a newbie
/* menu */
#menu{ margin:0px 0px; margin-top:67px; margin-left:90px; list-style:none; color:#fff; line-height:30px; display:inline-block; float:left; height:-100px; width:1000px; }
#menu1{ margin:0px 0px; margin-top:auto; margin-left:-50px; list-style:none; color:#fff; line-height:30px; display:inline-block; float:left; height:auto; width:95px; }
#menu a { color:#000; text-decoration:none; }
#menu > li {background:#fff none repeat scroll 0 0; cursor:pointer; float:left; position:relative;padding:0px 10px; z-index:999 !important;}
#menu > li a:hover {color:#B0D730;}
#menu .logo {background:transparent none repeat scroll 0% 0%; padding:0px; background-color:Transparent;}
/* sub-menus*/
#menu ul { padding:0px; margin:0px; display:block; display:inline;}
#menu li ul { position:absolute; left:-10px; top:0px; margin-top:30px; width:200px; line-height:16px; background-color:#FFF; color:#FFF; /* for IE */ display:none; }
#menu li:hover ul { display:block;}
#menu li ul li{ display:block; margin:5px 20px; padding: 5px 0px; border-top: dotted 1px #606060; list-style-type:none; }
#menu li ul li:first-child { border-top: none; }
#menu li ul li a { display:block; color:#0395CC; }
#menu li ul li a:hover { color:#7FCDFE; }
/* main submenu */
#menu #main { left:0px; top:-20px; padding-top:20px; background-color:#FFF; color:#fff; z-index:999 !important;}
On Click
In your HTML code, you can have Javascript actions happen on click:
<p onclick="clickedText()">Click Me!</p>
The necessary Javascript code would be:
var clickedText = function() {
//Insert Code Here That's On Click
}
To access an element by id use the following Javascript code:
var element = document.getElementById("id");
Then you can use:
element.style.display = "none";
Speed
If something is closing too fast, and you are using display: none;, that is because you are telling the browser to display nothing this exact second.
Your CSS code is telling the browser to not display the submenu.
CSS Code
This is where a sample of your HTML file would come in handy. You are telling the browser the following:
#menu li:hover ul { /*Do stuff*/ }
That means it selects all ul in a hovered upon li that is in #menu.
I think that a CSS Selector Reference would help you a lot.
Suggestions
I suggest that you give a snippet of the HTML code.
I suggest that you try this, this, and this tutorial.
I suggest that you take a look at the :not() CSS selector.

Categories