dsfds2353 2015-09-30 15:50
浏览 126
已采纳

如何在点击时更改菜单的样式?

How can I change the style of active menu/submenu which is selected by user? When I click a submenu, I want that submenu and its parent menu to have a different style (like hover, but permanent).

/*jQuery time*/
$(document).ready(function(){
    $("#accordian h3").click(function(){
        //slide up all the link lists
        $("#accordian ul ul").slideUp();
        //slide down the link list below the h3 clicked - only if its closed
        if(!$(this).next().is(":visible"))
        {
            $(this).next().slideDown();
        }
    })
})
/*custom font for text*/
@import url(http://fonts.googleapis.com/css?family=Nunito);
/*CSS file for fontawesome - an iconfont we will be using. This CSS file imported contains the 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 and 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 for 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 a {
    color: white;
    text-decoration: none;
    font-size: 11px;
    line-height: 27px;
    display: block;
    padding: 0 15px;
    /*transition for smooth hover animation*/
    transition: all 0.15s;
}
/*hover effect on links*/
#accordian ul ul li a:hover {
    background: #003545;
    border-left: 5px solid lightgreen;
}
/*Lets hide the non active LIs by 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>
        <!-- we will keep this LI open by 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 is a JSFiddle.

</div>
  • 写回答

3条回答 默认 最新

  • dtujfmfs06058 2015-09-30 16:03
    关注

    Just add this 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 this CSS:

    .active{
        text-shadow: 0 0 1px rgba(255, 255, 255, 0.7);
        color: red; /* NOTE i INTENTIONALLY ADDED RED COLOR TO ILLUSTRATE BETTER */
    }
    .submenu-active{
        background: #003545;
        border-left: 5px solid lightgreen;
    }
    

    try this DEMO

    Explanation:

    When you click on a menu item you want to remove the .active class from the previously active item and set it to the clicked item. The same is applied to the submenu item where I add .submenu-active class instead .active

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失