html - adding active for menu 2 levels, hiliting 2 levels menu jquery -
i have 2-level menu , when click on first level highlighting in yellow , working fine.
however, when choose category second level, main category in first level highlighted user know 1 of main categories he's browsing.
this site: http://www.marseille-fitness.com/bodypump.php
<ul class="mainnav"> <li><a href="rpm.php">cours collectifs</a> <ul class="dropdown"> <li><a href="rpm.php">rpm</a></li> <li><a href="bodypump.php">bodypump</a></li> <li><a href="bodyattack.php">bodyattack</a></li> <li><a href="bodycombat.php">bodycombat</a></li> <li><a href="bodyjam.php">bodyjam</a></li> <li><a href="bodybalance.php">bodybalance</a></li> <li><a href="cxworx.php">cxworx</a></li> </ul> </li> </ul> <script> $("ul.mainnav li").hover(function(){ $(this).find("ul.dropdown").css({"display" : "block"}).fadeto('500', '1.0'); }, function() { $(this).find("ul.dropdown").fadeto('500', '0.0').css({"display" : "none"}); }); //class active in menu start $(function(){ var url = window.location.pathname; var urlregexp = new regexp(url.replace(/\/$/,'')); // create regexp match current url pathname , remove trailing slash if present collide link in navigation in case trailing slash wasn't present there // grab every link navigation $('.mainnav li a').each(function(){ // , test href against url pathname regexp if(urlregexp.test(this.href)){ $(this).addclass('active'); } }); }); </script>
css
@import url(http://fonts.googleapis.com/css?family=raleway:600,800); ul.mainnav { margin:0; padding:0; list-style-type:none; background-color: #f0eff0; height: 28px; font-family: 'raleway', sans-serif; color: #606060; float: right; margin-right: 100px; } .menu-wrapper{ background-color: #f0eff0; height: 3.4em; } .mainnav li { margin:0; padding:0; list-style-type:none; height: 28px; line-height:28px; float: left; color: #606060; font-size: 14px; } .mainnav li { padding: 0 20px; line-height: 3.8em; display:block; color: #606060; text-decoration:none; font-family: 'raleway', sans-serif; font-weight: 800; } .mainnav li:hover > { background: #ffffff; } .mainnav li a:hover{ background: #ffffff; } .mainnav li ul.dropdown { margin:0; padding:0; position: absolute; display: none; background: #fff; opacity: 0.0; } .mainnav li ul.dropdown li { float: left; height: 28px; line-height: 28px; margin: 0; padding: 0; font-size: 12px; color: #606060; /*font-weight: normal;*/ } .mainnav li ul.dropdown li { font-family: 'raleway', sans-serif; font-weight: 400; line-height: 3em; height: 28px; display:block; padding: 0 20px; color: #606060; } .mainnav li ul.dropdown li a:hover { background: #cfcfcf; } .img-logo-coach-menu { position: absolute; margin-left: 5em; } .active { background: yellow; }
change loop
var url = window.location.pathname; var urlregexp = new regexp(url.replace(/\/$/,'')); $('.mainnav li a').each(function(){ // , test href against url pathname regexp if(urlregexp.test(this.href)){ $(this).addclass('active'); $(this).closest('.mainnav>li').children('a').addclass("active"); } });
the closest
function finds first (closest) ancestor matching selector, can add class it's direct child.
Comments
Post a Comment