【问题标题】:menu borders, current menu borders css菜单边框,当前菜单边框 css
【发布时间】:2013-07-03 09:15:52
【问题描述】:

我有这种菜单结构

<nav id="navigation">
    <ul class="menu">
        <li class="current-menu-item currrent_page_item"><a href="#">Home</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Portfolio</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
</nav>

还有一个 CSS 用于菜单

#navigation{float: right; padding: 10px 0px; display: block;}
#navigation li{float: left; display: inline;}
#navigation li a{
padding: 10px;
text-decoration: none;
font-size: 20px;
color: #fff;

display: block;

}

#navigation li{
padding: 0px 2px;
border-left: 1px solid #292929;
border-right: 1px solid #605f5f;
}
#navigation li:first-child{
border-left: none !important;
}
 #navigation li:last-child{
border-right: none !important; 
}

.current-menu-item a, .current_page_item a{
background: #414141;
border: 1px solid #4b4b4b !important;
border-bottom: 1px solid #323232 !important;

-moz-box-shadow: inset 0 0 10px #353333;
-webkit-box-shadow: inset 0 0 10px #353333;
box-shadow: inset 0 0 10px #353333;

-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;

behavior:url('border-radius.htc'); 
}

正如您在样式中看到的那样,它在当前菜单中生成了一个带有左右边框和带有框阴影的推式背景的菜单,我的问题是,菜单左侧和右侧的边框看起来当前菜单在两者之间时很难看,我想摆脱这些边框。我的意思是,例如,当当前菜单是 About 菜单时,现在必须删除 Contact 菜单的左侧边框以及 Portfolio 菜单的右侧边框,这样才能看起来整洁而酷。

目前我正在寻找一种方法来实现它,但到目前为止还没有运气。

【问题讨论】:

  • 只使用左边框,右边框只用于最后一个,或者右边框会更好看(主页按钮旁边没有边框)
  • 我不知道你为什么要隐藏周围元素的边框而不是当前的边框!你能解释一下为什么吗?

标签: javascript jquery html css


【解决方案1】:

您可以使用纯 CSS 执行此操作,如下所示(请参阅 JSFiddle here):

#navigation {
    float: right;
    padding: 10px 0px;
    display: block;
}
#navigation li {
    float: left;
    display: inline;
}
#navigation li a {
    padding: 10px;
    text-decoration: none;
    font-size: 20px;
    color: #fff;
    display: block;
}
#navigation li {
    padding: 0px 2px;
    border-left: 2px solid #292929;
}
#navigation li:first-child,
.current-menu-item, 
.current_page_item,
.current-menu-item + li, 
.current_page_item + li
{
    border-left: none !important;
}

.current-menu-item a, 
.current_page_item a {
    background: #414141;
    border: 1px solid #4b4b4b !important;
    border-bottom: 1px solid #323232 !important;
    -moz-box-shadow: inset 0 0 10px #353333;
    -webkit-box-shadow: inset 0 0 10px #353333;
    box-shadow: inset 0 0 10px #353333;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
    behavior:url('border-radius.htc');
}

基本上,这个想法是这样的:

  • 仅使用左边框
  • 删除边框
    • 第一项(#navigation li:first-child
    • 当前项目 (.current-menu-item, .current_page_item)
    • 紧跟在当前项目之后的项目 (.current-menu-item + li, .current_page_item + li)

【讨论】:

  • (+1) 很好的方法。 Not supported by some older versions,不过还是不错的。
  • 这个工作非常感谢你,但我需要两个边框,但这很好,我会保存这个以备将来参考。
  • 纯粹出于兴趣,为什么需要两个边框?
  • (+1)。我总是更喜欢不涉及 Jquery 的解决方案
  • @vee:这就是我想要输出的目标
【解决方案2】:

你可以使用一点 jquery:

$('.current-menu-item').css('border','none');
$('.current-menu-item').next('li').css('border-left','none');
$('.current-menu-item').prev('li').css('border-right','none');

演示:http://jsfiddle.net/sDFzS/

【讨论】:

  • 太棒了!像钻石一样工作!谢谢你,当我读到你的帖子时,我意识到这很容易,我只是不知道,当我处理这个问题时,jquery 在我的脑海里有点乱。呵呵
  • 我知道这种感觉 - 一直发生在我身上!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 2014-05-31
相关资源
最近更新 更多