Created
September 11, 2013 18:15
-
-
Save devluis/6527564 to your computer and use it in GitHub Desktop.
Add active class to a navigation menu based on URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
#nav { | |
margin:200px auto; | |
width:430px; | |
} | |
#nav ul { | |
list-style:none; | |
background-color:#64abfb; | |
} | |
#nav ul li { | |
display:inline-block; | |
line-height:44px; | |
} | |
#nav ul li a { | |
margin:10px; | |
color:#FFF; | |
padding:4px; | |
font-size:20px; | |
text-decoration:none; | |
} | |
#nav ul li a:hover { | |
border-bottom:3px #FFF solid; | |
} | |
#nav ul li .active { | |
border-bottom:3px #FFF solid; | |
} | |
</style> | |
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js”></script> | |
<script> | |
$(document).ready(function() { | |
$(function() { | |
var pgurl = window.location.href.substr(window.location.href | |
.lastIndexOf("/")+1); | |
$("#nav ul li a").each(function(){ | |
if($(this).attr("href") == pgurl || $(this).attr("href") == '' ) | |
$(this).addClass("active"); | |
}) | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<menu id="nav"> | |
<ul> | |
<li><a href="archivo.html">Home</a></li> | |
<li><a href="archivo1.html">Contact</a></li> | |
<li><a href="archivo3.html">About</a></li> | |
<li><a href="archivo4.html">Portfolio</a></li> | |
</ul> | |
</menu> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment