I have this:
<section class="header">
<div class="holder">
<span class="name">Joe Doe</span>
<nav class="">
<ul class="">
<li>Do Something</li>
<li>Do Something Else</li>
<li>Who knows</li>
</ul>
</nav>
</div>
</section>
My script to target that and add class.
$(document).ready(function(){
$(".header nav ul li a").click(function(e){
$(this).addClass('active');
});
)};
Update Css:
.header nav ul li a{
font-size: 15px;
text-transform: uppercase;
color: #959393;
text-decoration: none;
}
.header nav ul li a:hover{
color: #fff;
}
.active{
color: red;
}
The problem:
When I click on the link, it does get the red color, but it instantly change back to the original color and the link still with the class, maybe because the page is reloading or something ?.
I can't use preventDefault() because I need those links to behave as links.
If it gets red when you add !important, your problem can be fixed when you understand css specificity
Check the image below.
a is less specific. #whatever #whatever is more specific and wins.
You can do this with straight-up CSS - no js/jQuery needed. Also, as long as you follow the same specificity, there's no need to append your CSS with !important.
.header nav ul li a{
font-size: 15px;
text-transform: uppercase;
color: #959393;
text-decoration: none;
}
.header nav ul li a:hover{
color: #fff;
}
.header nav ul li a:visited{
color: red;
}
.header nav ul li a:active{
color: red;
}
If the page is reloading, your page is losing the state and the class added by the click will be lost.
To get around that, you have to maintain the state of the page somehow.
One way would be to use sessionStorage and you will need ids for these links.
$(document).ready(function(){
//reapply current active link (if found)
if (!!sessionStorage.activeLink) {
$("#" + sessionStorage.activeLink).addClass('active');
}
$(".header nav ul li a").click(function(e){
//keep track of which link was clicked.
sessionStorage.activeLink = $(this).prop("id");
$(this).addClass('active');
});
)};
Related
I am having a little difficult creating a navbar that when clicked it opens a small window on the same page like on the image.
Create the small window as it's own div:
<div id="myID"> This content will show when I click the navbar</div>
Add the following CSS:
#myID{
display: none;
}
Then use some script to show/hide the element:
$(document).ready(function(){
// change #nav to whatever the ID of the nav element is.
$('#nav').on('click', function(){
// show/hide pop up on click
$('#myID').toggle();
});
});
You can create a Navbar like this. This is the only dummy. In given image, they have one fix element and when you click on nav element according to that they are updating the content of that element.
$('#myNav').find('li a').click(function (e) {
$('li.active').removeClass('active');
$(this).parent('li').addClass('active');
$('#main').html($(this).html());
});
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #567;
}
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: #ccc;
}
.active {
background-color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="myNav">
<li class="active">Home</li>
<li>Service</li>
</ul>
<div id="main">
Home
</div>
It will help you to create a nav bar.
Please can someone tell me how to put a input text box in a dropdown just as this :-
this pic was using bootstrap. I dont want bootstrap just want to use html/css and javascript maybe? pleas ehelp me i stuck on this quite long.
Code: http://codepen.io/anon/pen/OyQYmN
HTML tags can be nested in eachother. Given this, you can change the dropdown's HTML:
<ul class="dropdown-menu">
<li>An option</li>
<li>Another option</li>
<li>Something else here</li>
</ul>
to include the <input type="text">.
To do this without bootstrap you will have to make your own CSS that will treat a list as a drop-down.
-one way to do this is explained here http://www.instructables.com/id/How-to-Create-Custom-CSS3-Dropdown-Menus-CSS-Drop/?ALLSTEPS
now that you have a list that is treated like a drop down, you can add any element you want in it.
I created a demo that uses pure CSS3 as a dropdown menu and added an input for you. Here is the code to accomplish this, demo is at the bottom.
/* Set Dropdown Display to None*/
nav ul ul {
display: none;
}
/* Display Dropdown on hover*/
nav ul li:hover > ul {
display: block;
}
ul li{
display: inline-block;
}
ul li a {
padding: 20px;
}
ul li a:hover{
text-decoration: none;
}
nav ul ul {
background: #222222;
border-radius: 0px;
position: absolute;
}
nav ul ul li{
padding: 15px 0;
}
nav ul ul li a{
color: #fff;
opacity: .5;
}
nav ul ul li a:hover{
color: #fff;
opacity: 1;
}
CSS3 Dropdown Menu: DEMO
I am trying to get the current menu item highlighted, I searched various sited for a solution but I just can't seem to make it work. I hope you can help me!
I have this now:
<div class="menu">
<ul class="highlight">
<li>Home</li>
<li>About</li>
<li>Work</li>
<li>Contact</li>
</ul>
</div>
.menu a{
background-color: #F2F2F2;
color: black;
font-size: 15px;
font-weight: bold;
padding: 10px 10px;
}
.active {
color: black;
}
.menu li {
display: inline;
opacity: 0.5;
}
.menu li:hover {
opacity: 1;
}
I know I have to add the class .active to the menu only whatever I try .menu li keeps overriding it. I really don't know what to do now.
in jquery
<script type="text/javascript">
jQuery(document).ready(function($){
// Get current url
// Select an a element that has the matching href and apply a class of 'active'. Also prepend a - to the content of the link
var url = window.location.href;
$('.menu a[href="'+url+'"]').addClass('current_page_item');
});
</script>
you could add an active class via jQuery if you have a menu that changes tabs without redirecting to a new page
jQuery:
$('.menu a').click(function(event){
event.preventDefault();
$('.menu a').removeClass('active')
$(this).addClass('active');
});
CSS:
.menu a.active {
color: red;
}
Or you could simply add .active class to the new page link that should be active.
http://jsfiddle.net/1ruy1t2h/3/
You can try 2 things:
1. put css for .active after .mennu li.
2. use !important in .active class. eg. .active {color:black !important;}
This should fix your issue.
If problem persists, create a jsfiddle of the issue and send back link.
The HTML could look like this:
<div class="menu">
<ul>
<li>Home</li>
<li class="active">About</li>
<li>Work</li>
<li>Contact</li>
</ul>
</div>
And the CSS could look like this:
.menu li a {
color: black;
font-size: 15px;
font-weight: bold;
}
.menu li.active a {
color: red;
}
.menu li a:hover {
color: blue;
}
This is a fiddle to demonstrate the code: http://jsfiddle.net/e01mvmgz/
CSS
#myMenu ul li {
display: inline;
}
#myMenu ul li a {
background-color:#333333;
text-transform: uppercase;
font-weight: bold;
font-family:"Open Sans", Arial, sans-serif;
font-size:12px;
text-decoration: none;
padding: 1em 2em;
color: #000000;
border-left:1px solid #333333;
border-right:1px solid #333333;
border-top:1px solid #333333;
}
#myMenu ul li a:hover {
color: #fff;
background-color: #999999;
}
.selection {
background-color: #000000;
border-bottom:10px solid #000000;
border-top:9px solid #000000;
}
HTML
<div align="right" id="myMenu">
<ul>
<li>Structure</li>
<li id="style">Style</li>
<li>Details</li>
</ul>
JavaScript
$(function () {
$("li:first-child").addClass("selection");
$('li').click(function () {
$('#myMenu li').removeClass('selection');
$(this).addClass('selection');
});
});
I want to add black colour to selected items background, but the above code is not working.
If I remove background-color:#333333 from #myMenu ul li a it works.
There are a couple of issues here.
You have tied your click handler to li so the context will be the li element
Your background color is set on the anchor: #myMenu ul li a initially
Assuming you want .selection to apply to the anchor tag, it has less specificity than the class for the anchor.
Change your code and CSS to:
CSS:
#myMenu ul li a.selection {
background-color: #000000;
border-bottom:10px solid #000000;
border-top:9px solid #000000;
}
Code:
$(function () {
$("li:first-child").addClass("selection");
// Or move your handler to the anchor rather than the list item
$('li').click(function () {
$('#myMenu li a').removeClass('selection');
$(this).find('a').addClass('selection');
});
});
Demo Fiddle
You are adding the background color via CSS to the <a> element. You are targeting the <li> element with your jQuery. For all you know, the jQuery could be working properly, but you can't see it because the <a> element has a background color.
Try changing the jQuery to target the <a> element instead. Be sure to return false or use preventDefault() when targeting the <a> element.
Also, as a side note, unless you have something else going on which I don't know about, it would be more efficient just hard code the selection class to the first element instead of targeting it with jQuery. Just a tip.
Also to add to Chris's answer...the css for #myMenu ul li a will always outweigh the .selection because it is has more specificity. You will need to add !important to any styles in .selection if you want them to override the styles in #myMenu ul li a
Generally although jQuery does auto selection automatically I personally like to handle it myself, just because then I know my code is "selecting" what I want.
So Like this:
$(function () {
$("li:first-child").addClass("selection");
$('li').each(function(){ //loop all li elements
$(this).click(function () {
$('#myMenu li.selection').removeClass('selection'); //select the li
// with the selection class
$(this).addClass('selection');
});
});
});
But basically an issue I see with how you did it is there is no margin on you anchor, or padding on your li, witch could make the changes to the li invisible.
Used with the following it should tell you if this works
.selection{
background-color: #000000;
border-bottom:10px solid #000000;
border-top:9px solid #000000;
padding:5px; //change to suit your needs
}
So, I have DOM that looks like this:
<ul id="nav">
<li><a>Hello</a></li>
<li>
<a>OuterMenu</a>
<ul>
<li><a>InnerMenu1</a>
<ul><li><a>InnerMenu2</a></li><li><a>Item 1</a></li><li><a>Item 2</a></li></ul>
</li>
</ul>
</li>
</ul>
which looks like this:
+Hello +OuterMenu
----InnerMenu1--------------InnerMenu2
----Other list item Item 1
Item 2
That is, the first menu is horizontal, the next menu is directly below the first menu, and all subsequent inner menus appear directly to the right [see example here].
This works fine, but I need the hover styles for each outer menu to persist as each inner menu is selected. When the user is hovering over Item 1, Item 1, InnerMenu, and OuterMenu should be highlighted, and when the user moves off of the whole menu tree, then and only then should OuterMenu no longer be highlighted. Is there a better way to do this than trying to manage a hover and mouseout event on every single list item?
I'm looking for a clean implementation here.
Check out Stu Nicholls great css-only work on just this issue.
I donĀ“t know what you have already, but if you use something like:
#nav > li:hover > a {
// outer menu highlight
}
it should highlight the outer menu also when you are on a sub-menu item.
The same technique can be applied to all levels, but it depends on your browser compatibility requirements as li:hover will not work in older IE versions.
For completeness
/* second level */
#nav > li > ul > li:hover > a {
}
/* third level */
#nav > li > ul > li > ul > li:hover > a {
}
Simply using the :hover psuedo-class on your li will apply even when you are over a descendant element. Here's a working example showing that this is true: http://jsfiddle.net/eMyHE/; hover over InnerMenu2 and you'll see InnerMenu1 and OuterMenu highlight.
Also, you might be interested in my 8-years-old CSS-only hierarchical menu tests, part of some ancient code that uses JavaScript for hierarchical menus.
This isn't my work, I'm just passing it on. It looks like it's the same answer as JakeParis's, but in JSFiddle form. http://jsfiddle.net/XPE3w/7/ This is for HTML with a ul>li>a structure (see the link if this doesn't make sense).
ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
}
ul li {
display: block;
position: relative;
float: left;
}
li ul {
display: none;
}
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 1px solid #ffffff;
padding: 5px 15px 5px 15px;
background: #2C5463;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover {
background: #617F8A;
}
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a {
background: #617F8A;
}
li:hover li a:hover {
background: #95A9B1;
}