javascript - How can I change the styles of my menu on click? -
how can change style of active menu/submenu selected user? when click submenu, want submenu , parent menu have different style (like hover, permanent).
/*jquery time*/ $(document).ready(function(){ $("#accordian h3").click(function(){ //slide link lists $("#accordian ul ul").slideup(); //slide down link list below h3 clicked - if closed if(!$(this).next().is(":visible")) { $(this).next().slidedown(); } }) })
/*custom font text*/ @import url(http://fonts.googleapis.com/css?family=nunito); /*css file fontawesome - iconfont using. css file imported contains font-face declaration. more info: http://fortawesome.github.io/font-awesome/ */ @import url(http://thecodeplayer.com/uploads/fonts/fontawesome/css/font-awesome.min.css); /*basic reset*/ * {margin: 0; padding: 0;} body { background: #4eb889; font-family: nunito, arial, verdana; } #accordian { background: #004050; width: 250px; margin: 100px auto 0 auto; color: white; /*some cool shadow , glow effect*/ box-shadow: 0 5px 15px 1px rgba(0, 0, 0, 0.6), 0 0 200px 1px rgba(255, 255, 255, 0.5); } /*heading styles*/ #accordian h3 { font-size: 12px; line-height: 34px; padding: 0 10px; cursor: pointer; /*fallback browsers not supporting gradients*/ background: #003040; background: linear-gradient(#003040, #002535); } /*heading hover effect*/ #accordian h3:hover { text-shadow: 0 0 1px rgba(255, 255, 255, 0.7); } /*iconfont styles*/ #accordian h3 span { font-size: 16px; margin-right: 10px; } /*list items*/ #accordian li { list-style-type: none; } /*links*/ #accordian ul ul li { color: white; text-decoration: none; font-size: 11px; line-height: 27px; display: block; padding: 0 15px; /*transition smooth hover animation*/ transition: 0.15s; } /*hover effect on links*/ #accordian ul ul li a:hover { background: #003545; border-left: 5px solid lightgreen; } /*lets hide non active lis default*/ #accordian ul ul { display: none; } #accordian li.active ul { display: block; }
<script src="http://thecodeplayer.com/uploads/js/jquery-1.7.1.min.js"></script> <script src="http://thecodeplayer.com/uploads/js/prefixfree-1.0.7.js"></script> <div id="accordian"> <ul> <li> <h3><span class="icon-dashboard"></span>dashboard</h3> <ul> <li><a href="#">reports</a></li> <li><a href="#">search</a></li> <li><a href="#">graphs</a></li> <li><a href="#">settings</a></li> </ul> </li> <!-- keep li open default --> <li class="active"> <h3><span class="icon-tasks"></span>tasks</h3> <ul> <li><a href="#">today's tasks</a></li> <li><a href="#">urgent</a></li> <li><a href="#">overdues</a></li> <li><a href="#">recurring</a></li> <li><a href="#">settings</a></li> </ul> </li> <li> <h3><span class="icon-calendar"></span>calendar</h3> <ul> <li><a href="#">current month</a></li> <li><a href="#">current week</a></li> <li><a href="#">previous month</a></li> <li><a href="#">previous week</a></li> <li><a href="#">next month</a></li> <li><a href="#">next week</a></li> <li><a href="#">team calendar</a></li> <li><a href="#">private calendar</a></li> <li><a href="#">settings</a></li> </ul> </li> <li> <h3><span class="icon-heart"></span>favourites</h3> <ul> <li><a href="#">global favs</a></li> <li><a href="#">my favs</a></li> <li><a href="#">team favs</a></li> <li><a href="#">settings</a></li> </ul> </li> </ul> </div>
here jsfiddle.
just add javascript code:
$("#accordian h3").click(function(){ $('.active').removeclass('active'); $(this).addclass('active'); }); $('#accordian li a').click(function(){ $('.submenu-active').removeclass('submenu-active'); $(this).addclass('submenu-active'); });
and css:
.active{ text-shadow: 0 0 1px rgba(255, 255, 255, 0.7); color: red; /* note intentionally added red color illustrate better */ } .submenu-active{ background: #003545; border-left: 5px solid lightgreen; }
try demo
explanation:
when click on menu item want remove .active
class active item , set clicked item. same applied submenu item add .submenu-active
class instead .active
Comments
Post a Comment