将类添加到主菜单 ul,而不使用 Drupal 7 中的模块
我试图让我的主题将 CSS 类名称添加到 Drupal 7 主菜单中的“ul”元素中。HTML
模拟代码:
<ul class="topnav">
<li><a href="#">Home</a></li>
<li>
<a href="#">Tutorials</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</ul>
如您所见,“第一级”ul 的类名称为“topnav”,以下嵌套 ul 的类名称为“subnav”。我正在连接一个 jQuery 下拉菜单,并希望在不使用模块的情况下应用它。是的,我已经尝试过“菜单属性”模块,但这在这里不起作用。
我已经搜索了一整天,似乎找不到完整的解决方案。我知道 superfish / 和 sf 附带的其他主题,但我想要自己的解决方案。
编辑: 好吧,我已经更进一步了。我有一个带有 subs 的菜单,jquery 最初隐藏了 subs,但是,我无法让 jquery 在给定事件上显示 subs:将鼠标悬停在“ul.menu li span”上
这是我的 CSS:
ul.menu {
list-style: none;
padding: 0 20px;
margin: 0;
float: left;
width: 920px;
background: #222;
font-size: 1.2em;
background: url(topnav_bg.gif) repeat-x;
}
ul.menu li {
float: left;
margin: 0;
padding: 0 15px 0 0;
position: relative; /*--Declare X and Y axis base for sub navigation--*/
}
ul.menu li a{
padding: 10px 5px;
color: #000;
display: block;
text-decoration: none;
float: left;
}
ul.menu li a:hover{
background: url(topnav_hover.gif) no-repeat center top;
}
ul.menu li span { /*--Drop down trigger styles--*/
width: 17px;
height: 35px;
float: left;
background: url(../images/bullet.png) no-repeat center top;
}
ul.menu li span{background-position: center bottom; cursor: pointer;} /*--Hover effect for trigger--*/
ul.menu li ul {
list-style: none;
position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
left: 0; top: 35px;
background: #333;
margin: 0; padding: 0;
float: left;
width: 170px;
border: 1px solid #111;
}
ul.menu li ul.menu li{
margin: 0; padding: 0;
border-top: 1px solid #252525; /*--Create bevel effect--*/
border-bottom: 1px solid #444; /*--Create bevel effect--*/
clear: both;
width: 170px;
}
html ul.menu li ul.menu li a {
float: left;
width: 145px;
background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
padding-left: 20px;
}
html ul.menu li ul.menu li a:hover {
background: #222 url(dropdown_linkbg.gif) no-repeat 10px center;
}
jQuery:
jQuery(function($) {
$('ul.menu li ul').hide();
$("ul.menu li ul").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
$("ul.menu li span").hover(function() { //When trigger is clicked...
// alert("No error!");
//Following events are applied to the subnav itself (moving subnav up and down)
$(this).parent().find("ul.menu li ul").slideDown('fast').show(); //Drop down the subnav on click
$(this).parent().hover(function() {
}, function(){
$(this).parent().find("ul.menu li ul").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
});
//Following events are applied to the trigger (Hover events for the trigger)
}).hover(function() {
$(this).addClass("subhover"); //On hover over, add class "subhover"
}, function(){ //On Hover Out
$(this).removeClass("subhover"); //On hover out, remove class "subhover"
});
});
如果我编辑这一行: $(this).parent().find("ul.menu li ul").slideDown('fast').show();
到此: ("ul.menu li ul").slideDown('fast').show();
它将显示所有父项的所有子菜单 - 显然我只想显示父项的子菜单我将鼠标悬停在菜单上。当我尝试添加回来 $(this).parent()...
它根本不会显示任何子项。
谢谢。
I'm trying to get my theme to add CSS class names to the 'ul' elements in the main-menu in Drupal 7.
HTML mock-up code:
<ul class="topnav">
<li><a href="#">Home</a></li>
<li>
<a href="#">Tutorials</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</ul>
As you can see, the "first level" ul has a class name of "topnav", the following nested ul has a class name of "subnav". I'm hooking up a jQuery drop down menu and want to apply it without the use of modules. Yes, I've tried the "Menu Attributes" module but that doesn't work here.
I've been searching all day and cannot seem to find a complete solution. I know about superfish / and other themes that come with sf, but I'm wanting my own solution.
Edit:
Ok, I've gotten further. I have a menu with subs on which jquery is initially hiding the subs, however, I can't get jquery to show the subs on a given event: hover over "ul.menu li span"
Here's my CSS:
ul.menu {
list-style: none;
padding: 0 20px;
margin: 0;
float: left;
width: 920px;
background: #222;
font-size: 1.2em;
background: url(topnav_bg.gif) repeat-x;
}
ul.menu li {
float: left;
margin: 0;
padding: 0 15px 0 0;
position: relative; /*--Declare X and Y axis base for sub navigation--*/
}
ul.menu li a{
padding: 10px 5px;
color: #000;
display: block;
text-decoration: none;
float: left;
}
ul.menu li a:hover{
background: url(topnav_hover.gif) no-repeat center top;
}
ul.menu li span { /*--Drop down trigger styles--*/
width: 17px;
height: 35px;
float: left;
background: url(../images/bullet.png) no-repeat center top;
}
ul.menu li span{background-position: center bottom; cursor: pointer;} /*--Hover effect for trigger--*/
ul.menu li ul {
list-style: none;
position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
left: 0; top: 35px;
background: #333;
margin: 0; padding: 0;
float: left;
width: 170px;
border: 1px solid #111;
}
ul.menu li ul.menu li{
margin: 0; padding: 0;
border-top: 1px solid #252525; /*--Create bevel effect--*/
border-bottom: 1px solid #444; /*--Create bevel effect--*/
clear: both;
width: 170px;
}
html ul.menu li ul.menu li a {
float: left;
width: 145px;
background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
padding-left: 20px;
}
html ul.menu li ul.menu li a:hover {
background: #222 url(dropdown_linkbg.gif) no-repeat 10px center;
}
jQuery:
jQuery(function($) {
$('ul.menu li ul').hide();
$("ul.menu li ul").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
$("ul.menu li span").hover(function() { //When trigger is clicked...
// alert("No error!");
//Following events are applied to the subnav itself (moving subnav up and down)
$(this).parent().find("ul.menu li ul").slideDown('fast').show(); //Drop down the subnav on click
$(this).parent().hover(function() {
}, function(){
$(this).parent().find("ul.menu li ul").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
});
//Following events are applied to the trigger (Hover events for the trigger)
}).hover(function() {
$(this).addClass("subhover"); //On hover over, add class "subhover"
}, function(){ //On Hover Out
$(this).removeClass("subhover"); //On hover out, remove class "subhover"
});
});
If i edit this line: $(this).parent().find("ul.menu li ul").slideDown('fast').show();
to this: ("ul.menu li ul").slideDown('fast').show();
It will show ALL the submenus for all parent items - obiviously I only want to show the submenu of the parent menu that I'm hovering over. When I try to add back in $(this).parent()...
It won't show any subs at all.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
谢谢大家的帮助。下面是更正后的工作代码,用于获取我在原始帖子中提到的与 Drupal 7 一起使用的教程:
注意:我在这里使用主菜单作为块。
CSS:
jQuery:
部分 page.tpl.php:
此实现无需将任何其他类注入到 Drupal 的菜单 HTML 中即可工作。
Thank you all for your help. Here's the corrected working code to get the tutorial i mentioned in my original post to work with Drupal 7:
Note: I'm using the main-menu as a block here.
CSS:
jQuery:
Partial page.tpl.php:
This implementation works without having to inject any additional classes into Drupal's menu HTML.
我使用 drupal 菜单块模块,并希望向主菜单项(第一级)添加一个 css 类,用于此类响应式 jQuery 导航。
http://jasonweaver.name/lab/flexiblenavigation/
我需要
中的 css 类
链接和第一个类。
查看 item-with-ul 和 link-with-ul
在这里你可以看到我启动的 Drupal 7
I use the drupal menu block module and want to add to main menu item (first level) a css class for such responsive jQuery Navigation.
http://jasonweaver.name/lab/flexiblenavigation/
I need the css class in the
<a href=""></a>
Link and first<li></li>
class.See item-with-ul and link-with-ul
Here you see my started Drupal 7
有点不清楚你的问题是什么。您似乎也有点不确定 css 定位是如何工作的。 Blender 上面的原始评论是一种定位方式(尽管它需要是一个类,而不是
topnav
的 id,因为你的 html 将它作为一个类)。正如 Blender 所说,这会将样式应用于任何第二级 ul 。此外,由于您的第二级已经有一个类
subnav
,因此可以通过以下方式实现相同的定位:假设该类没有出现在其他地方,或者
如果它在
topnav
之外的其他地方使用代码> 列表。但是,您的问题似乎可能是如何在导航菜单中定位特定的
subnav
。一种方法可能是:这只会激活鼠标悬停在
subnav
所在的特定li
上的样式。如果我可以假设您的a< /code> 标签最终将具有真正的 href,那么您也可以通过这些来专门定位。例如,如果您的主页链接是
Home
那么将匹配紧随该链接的
ul
(这似乎就是您在评论中提到的)。It is a little unclear what your problem is. It also seems that you are a little uncertain how css targeting works. Blender's original comment above is one way to target (though it needs to be a class not an id for the
topnav
since your html has it as a class).This would apply styles to any second level
ul
just as Blender said. Additionally, since your second level already has a classsubnav
the same targeting can be achieved by:assuming the class does not show up elsewhere or by
if it is used elsewhere besides in the
topnav
list.However, it seems that your question may be how to target a specific
subnav
in the navigation menu. One way might be:which will only activate the styles on the mouse hover over the particular
li
that thesubnav
resides in. If I can assume that youra
tags will eventually have real hrefs, then you could also specifically target by those. For example if your Home link is<a href="/Home.html">Home</a>
thenwould match the
ul
that immediately follows that link (which seems to be what you mention in your comment).对于 Drupal 7,尝试在主题的
template.php
文件中放置类似的内容:这种方法的警告是我们对类名进行硬编码,因此它不允许任何其他模块具有请说出此处出现的类名称。我发誓有一种方法可以在渲染数组级别挂钩菜单的构建,我们可以轻松地向属性类数组添加/删除类,但就我的一生而言,我现在无法追踪该方法。
For Drupal 7, try placing something like this in your theme's
template.php
file:The caveat to this approach is we're hard coding the class name, so it will not allow any other module to have a say in the class names that appear here. I swear there is a way to hook into the building of the menu at a render array level, where we can easily append/remove classes to the attributes class array, but for the life of me I can't track down that approach right now.