如何根据所选项目改变菜单背景
这让我感到畏惧,我看不到要走的路。这是我第一次在这里提出问题,所以如果我不遵守协议,请纠正我。谢谢!!!
我需要根据所选项目更改菜单的背景,因此当活动时,左侧的项目将显示为橙色,而右侧的项目将显示为灰色。
此外,分隔颜色的三角形必须保持在活动菜单项的右侧。
对于第一个元素,很容易,但第二个以及后面的元素,我无法使代码工作,因为它超出了菜单的边界。
例如,我有 [individuos] [empresas] [corredores] [proveedores]
当 empresas 处于活动状态时,individuos 和 empresas 应为橙色,而 corredores 和 proveedores 应为灰色。
如果选择corredores,则individuos、empresas 和corredores 应为橙色,而provedores 为灰色。
我想发布一张图片来说明,但作为新手,我不被允许。
#navigation {
position: absolute;
line-height: 40px;
padding: 0;
margin: 0;
margin-left: 210px;
width: 730px;
background-color: #757373;
}
#navigation ul li a {
text-decoration: none;
float: left;
padding: 0 40px 0 10px;
}
#navigation .empresas .active {
background: url(images/the-background.png) no-repeat right;
}
这个不错
这个不是,看到 INDIVIDUOS 应该是橙色
This is daunting me and I can't see the way to go. This is the first time I put a question here, so correct me if I´m not following protocol. Thanks!!!
I need the background of the menu to change according to the selected item so when active, the items to the left will show in orange while keeping the items on the right in gray.
Also, the triangle separating the colors have to keep to the right of the active menu item.
For the first element, is easy, but the second and forward, I cant make the code work as it cuts on the boundaries of the menu.
For example, I have [individuos] [empresas] [corredores] [proveedores]
When empresas is active, individuos and empresas should be orange while corredores and provedores should be gray.
If corredores is selected, then individuos, empresas and corredores should be orange while proveedores is gray.
I wanted to post an image to illustrate but as newbie I am not allowed.
#navigation {
position: absolute;
line-height: 40px;
padding: 0;
margin: 0;
margin-left: 210px;
width: 730px;
background-color: #757373;
}
#navigation ul li a {
text-decoration: none;
float: left;
padding: 0 40px 0 10px;
}
#navigation .empresas .active {
background: url(images/the-background.png) no-repeat right;
}
This one is good
This one is not, see that INDIVIDUOS should be orange
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这个小提琴提供了一些灵感?
CSS:
HTML:
更新
请参阅带有“箭头”的更新的小提琴等等。如果您希望此功能也适用于 IE7,请添加
span
元素,而不是使用:before
和:after
。Maybe this fiddle provides some inspiration?
CSS:
HTML:
Update
See updated fiddle with the "arrow" and more. If you want this to work also in IE7, then add
span
elements instead of using:before
and:after
.解决方案
好的,问题解决了,这是我见过的最干净的技巧。我不需要向 php、类等添加任何其他内容。一位同事指出了解决方案。所需要的只是 z-index 和定位方面的一些技巧。感谢@peter 和@PeterSmith 的投入。
THE SOLUTION
Ok, problem solved and it was the cleanest trick I´ve seen. I did not need to add anything else to the php, classes or the like. The solution was pointed by a colleague. All that it was needed was some trickery with z-index and positioning. Thank @peter and @PeterSmith for your input.
编辑:刚刚看到下面关于 .active 类的最新评论。
如果您可以在页面中添加一些 javascript,则可以为每个 li 项目运行 for 循环,将“orange”附加到其类,直到找到 .active 类,然后其余的将“grey”附加到该类。等我有空的时候我会尝试一下小提琴手
Edit: Just saw your latest comment below about the .active class.
If you can add some javascript to your pages, you could run through a for loop for each li item, appending "orange" to its class, until you find the .active class, then the rest would have "grey" appended to the class. I'll try a fiddler later when I've got some free time