这是一个演示,带有 cmets 和说明。它重用了 Foundation 的大部分 .top-bar 类和功能,但使用自定义 jQuery 而不是 TopBar JS,以获得手风琴效果。
HTML 修改
以下代码示例摘自 Foundation 的 TopBar 文档。进行 html cmets 中描述的更改,将 .top-bar 转换为手风琴动画样式。
<!-- IMPORTANT: remove the "data-topbar" attribute from .top-bar,
otherwise the topbar plugin will initialize. -->
<nav class="top-bar" data-topbar role="navigation">
...
<!-- IMPORTANT: remove the .dropdown class from the dropdown menu -->
<ul class="dropdown">
CSS
Foundation 的 .dropdown 类不适用于我们的目的,但很多样式都很有用,尤其是对于桌面屏幕尺寸。在示例中,我们基于嵌套的 ul 选择器重写了类样式,但您可以为此使用任意类名。
/* Opens the mobile menu */
.top-bar.opened {
overflow: visible;
}
/* The rest replaces the Foundation .dropdown class */
.top-bar-section ul ul {
z-index: 999;
display: none;
}
@media only screen and (min-width: 40.063em) {
.top-bar-section ul ul {
position: absolute;
left: 0;
top: auto;
min-width: 100%;
}
}
.top-bar-section li.active ul {
display: block;
}
.top-bar-section ul ul li {
white-space: nowrap;
width: 100%;
}
/* Positions the arrow relative to the menu item */
.top-bar-section .has-dropdown > a {
position: relative;
}
.top-bar-section .has-dropdown > a:after {
top: 1.25rem;
}
/* Hover state */
.top-bar-section .has-dropdown:hover > ul {
display: block;
}
JS 初始化
不幸的是,由于 Foundation 的 CSS 中的 !important 标志,动画在桌面屏幕尺寸下不起作用,这里:
@media only screen and (min-width: 40.063em) {
.top-bar-section ul { height: auto !important; }
}
要使手风琴动画在大屏幕上工作,您需要删除该样式声明或重写 .top-bar-section 类,这将是相当多的工作。在示例实现中,菜单在单击和悬停时起作用,除非您在小屏幕上,否则它不会动画。
// Init foundation as usual
$(document).foundation();
/* Register event handlers for .top-bar accordion menu */
// This opens the menu
$('.top-bar').on('click', '.toggle-topbar', function(e) {
e.preventDefault();
$('.top-bar').toggleClass('opened');
});
// This does the accordion animation
$('.top-bar-section').on('click', '.has-dropdown > a', function(e){
e.preventDefault();
$('.top-bar-section ul ul').slideUp();
$(e.target).next().not(':visible').slideDown();
});
来源:http://www.sepiariver.ca/demos/foundation-topbar-accordion/