0

我正在尝试为我的网站制作自定义 CSS 导航,但我遇到了一些问题,您可以在下面找到代码。我正在制作 CSS 垂直导航,代码如下:

<style type="text/css">

/* GT stylesheet */
#cssmenu > ul {
list-style: none;
margin: 0;
padding: 0;
vertical-align: baseline;
line-height: 1;
}

/* The container */
#cssmenu > ul {
display: block;
position: relative;
width: 150px;
}

/* The list elements which contain the links */
#cssmenu > ul li {
    display: block;
    position: relative;
    margin: 0;
    padding: 0;
    width: 250px;   
}

    /* General link styling */
    #cssmenu > ul li a {
        /* Layout */
        display: block;
        position: relative;
        margin: 0;
        border-top: 1px dotted #3a3a3a;
        border-bottom: 1px dotted #1b1b1b;
        padding: 11px 20px;
        width: 210px;

        /* Typography */
        font-family: Helvetica, Arial, sans-serif;
        color: #d8d8d8;
        text-decoration: none;
        text-transform: uppercase;
        text-shadow: 0 1px 1px #000;
        font-size: 13px;
        font-weight: 300;


        /* Background & effects */
        background: #282828;
    }

    /* Rounded corners for the first link of the menu/submenus */
    #cssmenu > ul li:first-child>a {
        border-top-left-radius: 4px;
        border-top-right-radius: 4px;
        border-top: 0;
    }

    /* Rounded corners for the last link of the menu/submenus */
    #cssmenu > ul li:last-child>a {
        border-bottom-left-radius: 4px;
        border-bottom-right-radius: 4px;
        border-bottom: 0;
    }


    /* The hover state of the menu/submenu links */
    #cssmenu > ul li>a:hover, #cssmenu > ul li:hover>a {
        color: #000;
        text-shadow: 0 1px 0 rgba(0, 0, 0, .3);
        background: #A9CA6F;
        background: -webkit-linear-gradient(bottom, #A9CA6F, #B4D876);
        background: -ms-linear-gradient(bottom, #A9CA6F, #B4D876); 
        background: -moz-linear-gradient(bottom, #A9CA6F, #B4D876);
        background: -o-linear-gradient(bottom, #A9CA6F, #B4D876);
        border-color: transparent;
    }

    /* The arrow indicating a submenu */
    #cssmenu > ul .has-sub>a::after {
        content: '';
        position: absolute;
        top: 16px;
        right: 10px;
        width: 0px;
        height: 0px;

        /* Creating the arrow using borders */
        border: 4px solid transparent;
        border-left: 4px solid #d8d8d8; 
    }

    /* The same arrow, but with a darker color, to create the shadow effect */
    #cssmenu > ul .has-sub>a::before {
        content: '';
        position: absolute;
        top: 17px;
        right: 10px;
        width: 0px;
        height: 0px;

        /* Creating the arrow using borders */
        border: 4px solid transparent;
        border-left: 4px solid #000;
    }

    /* Changing the color of the arrow on hover */
    #cssmenu > ul li>a:hover::after, #cssmenu > ul li:hover>a::after {
        border-left: 4px solid #fff;
    }

    #cssmenu > ul li>a:hover::before, #cssmenu > ul li:hover>a::before {
        border-left: 4px solid rgba(0, 0, 0, .3);
    }
</style>

问题是我无法将点击的链接更改为:#A9CA6F。我希望当我使我<li>的 id/class 处于活动状态时保持这种颜色:#A9CA6F 有帮助吗?

提前致谢!

演示这是小提琴的链接!

4

4 回答 4

1

添加这个...

#cssmenu li.active a {background-color:#A9CA6F;}

我认为您需要做的就是将背景颜色添加到a并非常明确地定位它。这是小提琴 - 我在你的 CSS 底部添加了一行。如果您没有足够具体地定位它,它不会推翻默认样式。

编辑

稍微详细说明一下,之前在您的 CSS 中您已经说过:

#cssmenu > ul li a {...}

因此,为了定位您的活动课程,您必须比这具体,否则上述规则将覆盖所有其他规则 - 因此:

#cssmenu > ul li.active a {...}

并不是...

li.active a  {...}

因此,例如,在 CSS 规则之间的无拘无束的斗争中......

  1. #menu a会打a.active
  2. a.active会打败只是a
  3. #wrapper #menu a会打#menu a

:-) HTH

于 2013-05-14T08:26:50.407 回答
1

试试这个并检查这个小提琴http://jsfiddle.net/sarfarazdesigner/WT9Uw/3/

#cssmenu > ul li.active > a, #cssmenu > ul li.active > a {
   background-color:#A9CA6F;            
   color: #000;
}
于 2013-05-14T08:31:17.787 回答
1

如果它也将在同一页面内导航,您可以通过一些 javascript 尝试它。这是一个jQuery解决方案:

$("#cssmenu li").click(function(){
    $("#cssmenu li").removeClass('active');
    $(this).addClass('active');
});

jsfiddle

它将类移动到已单击.active的任何位置。li在 CSS 中,您可以添加一个看起来像这样的选择器(到悬停选择器):

#cssmenu > ul li.active a
于 2013-05-14T08:34:00.180 回答
1

添加以下代码很简单:

#cssmenu > ul li.active a{
    background-color: #A9CA6F;
}

演示

于 2013-05-14T08:34:21.513 回答