创建一个可用于网站内容导航的导航栏。
导航栏组件由navbar容器、navbar本身、以及一个或多个导航菜单组成。
Element | Description |
---|---|
uk-navbar |
添加到<nav> 元素中以定义导航栏组件。 |
.uk-navbar-container |
将此类添加到同一<nav> 元素或父元素为导航栏添加背景样式。 |
.uk-navbar-left .uk-navbar-center .uk-navbar-right |
添加到其中一个<div> 元素中以对齐导航。 |
.uk-navbar-nav |
添加到<ul> 元素中创建导航菜单,使用 <a> 元素作为列表中的菜单项。 |
.uk-parent |
添加此类指示父级菜单项。 |
.uk-active |
添加此类指示活动菜单项。 |
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href=""></a></li>
<li class="uk-parent"><a href=""></a></li>
<li><a href=""></a></li>
</ul>
</div>
</nav>
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
</nav>
您可以在导航栏容器中放置多个导航,您可以在同一导航栏中将它们左对齐、居中和右对齐。
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">...</div>
<div class="uk-navbar-center">...</div>
<div class="uk-navbar-right">...</div>
</nav>
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div class="uk-navbar-right">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
</nav>
可以通过鼠标悬停或点击来切换启用导航栏中的导航菜单。只需将 mode: click
选项添加到 uk-navbar
属性即可。
<nav class="uk-navbar-container" uk-navbar="mode: click">...</nav>
<nav class="uk-navbar-container uk-margin" uk-navbar="mode: click">
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
</nav>
当网站首屏为大图或者大面积背景色时,您可能希望将导航栏设为透明。此时只需将.uk-navbar-transparent
class 添加到 <nav>
元素即可。如果有需要的话,还可以添加Inverse 组件 中的.uk-light
或 .uk-dark
class 以调整导航栏的颜色。
<nav class="uk-navbar-container uk-navbar-transparent" uk-navbar>...</nav>
<div class="uk-position-relative">
<img src="/skin/ukv3/images/light.jpg" alt="">
<div class="uk-position-top">
<nav class="uk-navbar-container uk-navbar-transparent" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
</ul>
</div>
</nav>
</div>
</div>
要定义导航栏的副标题,请在导航单元的<a>
元素中创建<div>
元素。再将 .uk-navbar-subtitle
class 添加到另一个 <div>
元素。
<li>
<a href="">
<div>
...
<div class="uk-navbar-subtitle"></div>
</div>
</a>
</li>
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active">
<a href="#">
<div>
Active
<div class="uk-navbar-subtitle">Subtitle</div>
</div>
</a>
</li>
<li>
<a href="#">
<div>
Parent
<div class="uk-navbar-subtitle">Subtitle</div>
</div>
</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li>
<a href="#">
<div>
Item
<div class="uk-navbar-subtitle">Subtitle</div>
</div>
</a>
</li>
</ul>
</div>
</nav>
您还可以将自定义内容添加到导航栏,例如文本、图标、按钮或表单。将.uk-navbar-item
class 添加到放置内容的 <div>
容器元素中。
<div class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<a href="" class="uk-navbar-item uk-logo"></a>
<ul class="uk-navbar-nav">...</ul>
<div class="uk-navbar-item">...</div>
</div>
</div>
将 Utility 组件中的.uk-logo
class 添加到<a>
或 <div>
元素以指示您的品牌。
<nav class="uk-navbar-container uk-margin" uk-navbar>
<div class="uk-navbar-left">
<a class="uk-navbar-item uk-logo" href="#">Logo</a>
<ul class="uk-navbar-nav">
<li>
<a href="#">
<span class="uk-icon uk-margin-small-right" uk-icon="icon: star"></span>
Features
</a>
</li>
</ul>
<div class="uk-navbar-item">
<div>Some <a href="#">Link</a></div>
</div>
<div class="uk-navbar-item">
<form action="javascript:void(0)">
<input class="uk-input uk-form-width-small" type="text" placeholder="Input">
<button class="uk-button uk-button-default">Button</button>
</form>
</div>
</div>
</nav>
您可以创建LOGO居中并且导航菜单拆分在左右两侧的导航栏。只需将uk-navbar-center-left
class 类添加到导航栏容器中的其中一个导航栏,再将 uk-navbar-center-right
class 类添加到同一导航栏容器中的另一个导航栏。
Note 需要额外的 div
元素来支持IE 11。
<div class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-center">
<div class="uk-navbar-center-left"><div>...</div></div>
<a href="" class="uk-navbar-item uk-logo"></a>
<div class="uk-navbar-center-right"><div>...</div></div>
</div>
</div>
<nav class="uk-navbar-container uk-margin" uk-navbar>
<div class="uk-navbar-center">
<div class="uk-navbar-center-left"><div>
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
</ul>
</div></div>
<a class="uk-navbar-item uk-logo" href="#">Logo</a>
<div class="uk-navbar-center-right"><div>
<ul class="uk-navbar-nav">
<li><a href="#">Item</a></li>
</ul>
</div></div>
</div>
</nav>
将 .uk-navbar-toggle
class 和 uk-navbar-toggle-icon
属性添加到 <a>
或 <div>
元素以创建作为切换器的图标。默认情况下,切换没有附加JavaScript. 举例,您可以从 Off-canvas 组件 添加一个抽屉式导航,或者从 Modal 组件中添加一个模态对话框。
<div class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<a class="uk-navbar-toggle" uk-navbar-toggle-icon href=""></a>
</div>
</div>
<nav class="uk-navbar uk-navbar-container uk-margin">
<div class="uk-navbar-left">
<a class="uk-navbar-toggle" uk-navbar-toggle-icon href="#"></a>
</div>
</nav>
<nav class="uk-navbar uk-navbar-container uk-margin">
<div class="uk-navbar-left">
<a class="uk-navbar-toggle" href="#">
<span uk-navbar-toggle-icon></span> <span class="uk-margin-small-left">Menu</span>
</a>
</div>
</nav>
.uk-navbar-toggle-animate
<a class="uk-navbar-toggle uk-navbar-toggle-animate" uk-navbar-toggle-icon href=""></a>
<nav class="uk-navbar uk-navbar-container uk-margin" uk-navbar>
<div class="uk-navbar-left">
<a class="uk-navbar-toggle uk-navbar-toggle-animate" uk-navbar-toggle-icon href="#" aria-label="Open Menu"></a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</div>
</nav>
导航栏可以包含Dropdown 组件中的下拉菜单。只需将 .uk-navbar-dropdown
添加到下拉菜单中,这样它就能完美适应导航菜单的样式。将.uk-navbar-dropdown-nav
class 添加到下拉菜单内部的导航菜单上。
<ul class="uk-navbar-nav">
<li>
<a href=""></a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">...</ul>
</div>
</li>
</ul>
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div class="uk-navbar-right">
<ul class="uk-navbar-nav">
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
</ul>
</div>
</nav>
要创建父图标,只需将uk-navbar-parent-icon
属性添加到<span>
元素。
<ul class="uk-navbar-nav">
<li>
<a href="">Parent <span uk-navbar-parent-icon></span></a>
<div class="uk-navbar-dropdown">…</div>
</li>
</ul>
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent <span uk-navbar-parent-icon></span></a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div class="uk-navbar-right">
<ul class="uk-navbar-nav">
<li>
<a href="#">Parent <span uk-navbar-parent-icon></span></a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
</ul>
</div>
</nav>
The Dropdown 组件 允许您下拉菜单中的内容分为多个列。可以容纳最多五列。需要添加以下类class之一。如果容易内放不下这些列时,它们将堆叠显示。
Class | Description |
---|---|
.uk-navbar-dropdown-width-2 |
两列 |
.uk-navbar-dropdown-width-3 |
三列 |
.uk-navbar-dropdown-width-4 |
四列 |
.uk-navbar-dropdown-width-5 |
五列 |
<div class="uk-navbar-dropdown uk-navbar-dropdown-width-2">
<div class="uk-navbar-dropdown-grid uk-child-width-1-2" uk-grid>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">...</ul>
</div>
<div>...</div>
</div>
</div>
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li>
<a href="#">Two Columns</a>
<div class="uk-navbar-dropdown uk-navbar-dropdown-width-2">
<div class="uk-navbar-dropdown-grid uk-child-width-1-2" uk-grid>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
</nav>
默认情况下,下拉菜单位于导航栏项目下方并左对齐。要更改对齐方式,请将align
选项设置为uk-navbar
属性。
位置 | 描述 |
---|---|
left |
将下拉菜单向左对齐。 |
right |
将下拉菜单向右对齐。 |
center |
将下拉菜单与中心对齐。 |
<div uk-navbar="align: center"></div>
<nav class="uk-navbar-container" uk-navbar="align: center">
<div class="uk-navbar-center">
<ul class="uk-navbar-nav">
<li>
<a href="#">Center</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
</ul>
</div>
</nav>
默认情况下,下拉菜单与其导航栏项目对齐。要将下拉列表定位到不同的元素,只需向uk-navbar
属性添加target: SELECTOR
选项。
<nav class="uk-navbar-container" uk-navbar="target: !.uk-navbar">…</nav>
<nav class="uk-navbar-container" uk-navbar="target: !.uk-navbar; align: center;">
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown uk-navbar-dropdown-width-2">
<div class="uk-navbar-dropdown-grid uk-child-width-1-2" uk-grid>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</div>
</div>
</li>
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown uk-navbar-dropdown-width-3">
<div class="uk-navbar-dropdown-grid uk-child-width-1-3" uk-grid>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
<div class="uk-navbar-right">
<ul class="uk-navbar-nav">
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
</ul>
</div>
</nav>
要使下拉菜单两端对齐,请使用Drop 组件 及其stretch
选项。下面的示例演示了下拉菜单与父导航栏的边界两端对齐。
<div class="uk-navbar-dropdown" uk-drop="boundary: !.uk-navbar; stretch: x; flip: false">…</div>
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown" uk-drop="boundary: !.uk-navbar; stretch: x; flip: false">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown" uk-drop="boundary: !.uk-navbar; stretch: x; flip: false">
<div class="uk-navbar-dropdown-grid uk-child-width-1-2" uk-grid>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
</nav>
将下拉菜单的宽度扩展到下拉菜单的宽度并不带默认样式地显示该下拉菜单。要将下拉菜单放在这样的下拉栏里,请将dropbar: true
选项添加到 uk-navbar
.
<nav class="uk-navbar-container" uk-navbar="dropbar: true;">...</nav>
<div class="uk-navbar-dropbar"></div>
<div class="uk-position-relative">
<nav class="uk-navbar-container" uk-navbar="dropbar: true">
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li>
<a href="#">Item</a>
<div class="uk-navbar-dropdown uk-navbar-dropdown-width-2">
<div class="uk-navbar-dropdown-grid uk-child-width-1-2" uk-grid>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div>
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
<div class="uk-navbar-right">
<ul class="uk-navbar-nav">
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-header">Header</li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
</ul>
</div>
</nav>
<div class="uk-navbar-dropbar"></div>
</div>
将导航栏包装在一个容器中,并且该容器包含Sticky 组件中的uk-sticky
属性,可以创建一个比较基础的粘性导航栏。
导航栏带有一个uk-navbar-sticky
修饰class,可以为粘性状态优化样式(例如附加的框阴影)。要让sticky组件可以动态添加和删除这个class,请设置 cls-active: uk-navbar-sticky
。 为确保将类添加到导航栏容器,还需设置 sel-target: .uk-navbar-container
。
Note 您可以在 粘性导航测试页面查看更多示例。
<div uk-sticky="sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky">
<nav class="uk-navbar-container" uk-navbar>...</nav>
</div>
<div uk-sticky="sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; bottom: #transparent-sticky-navbar">
<nav class="uk-navbar-container" uk-navbar style="position: relative; z-index: 980;">
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
</nav>
</div>
您可以显示一个Dropbar,而不是使用Dropdown,这意味着子导航以全宽度显示在导航栏下方。 只需在uk-navbar
属性内设置dropbar: true
即可。
<div uk-sticky="sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky">
<nav class="uk-navbar-container" uk-navbar="dropbar: true;">...</nav>
</div>
<div uk-sticky="sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; bottom: #transparent-sticky-navbar">
<nav class="uk-navbar-container" uk-navbar="dropbar: true;" style="position: relative; z-index: 980;">
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
</nav>
</div>
使用透明导航栏时,标记包含 .uk-navbar-transparent
class 以及 .uk-light
或 .uk-dark
。 当导航栏为粘性时,通常需要删除这些类并在导航栏返回到非粘性状态时再重新添加它们。 要这样的话请设置 cls-inactive: uk-navbar-transparent uk-light
.
默认情况下是即时切换的,这样看上去不太理想。 那么我们可以设置top: 200
使导航栏先消失,然后在用户滚动200px之后导航栏才再次出现。 在这种情况下,我们还可以定义导航栏出现时的动画,只需设置 animation: uk-animation-slide-top
.
<div uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent uk-light; top: 200">
<nav class="uk-navbar-container" uk-navbar>...</nav>
</div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum.
<div class="uk-section-primary uk-preserve-color">
<div uk-sticky="animation: uk-animation-slide-top; sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; cls-inactive: uk-navbar-transparent uk-light; top: 200">
<nav class="uk-navbar-container">
<div class="uk-container uk-container-expand">
<div uk-navbar>
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li>
<a href="#">Parent</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active"><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
</li>
<li><a href="#">Item</a></li>
</ul>
</div>
</div>
</nav>
</div>
<div class="uk-section uk-light">
<div class="uk-container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum.</p>
<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum.</p>
</div>
</div>
</div>
可以在组件属性中使用以下选项,用分号隔开多个选项。了解更多
Option | Value | Default | Description |
---|---|---|---|
align |
String | left |
下拉菜单的对齐方式:left , right , center |
dropbar |
Boolean | false |
启用或禁用下拉栏行为。 |
dropbar-anchor |
CSS selector | false |
如果设置了该选项,将在锚元素之后插入dropbar。 |
stretch |
Boolean, x , y |
true |
在两个(true)轴或给定轴上的拉伸下拉列表。 |
mode |
String | click, hover |
下拉菜单的触发模式: click , hover |
delay-show |
Number | 0 |
hover悬停模式下显示下拉菜单的延迟时间(以毫秒为单位)。 |
delay-hide |
Number | 800 |
hover悬停模式下隐藏下拉菜单的延迟时间(以毫秒为单位)。 |
boundary |
CSS selector | window |
下拉菜单不能超过的区域会导致它翻转和移动。默认情况下为距离最近的滚动祖先。 |
target |
Boolean, CSS selector | false |
下拉列表定位到的元素(true 为相对对于窗口)。 |
target-x |
布尔值,CSS 选择器 | false |
下拉列表定位到元素的 X 轴(true 对于窗口)。 |
target-y |
布尔值,CSS 选择器 | false |
下拉列表定位到元素的 Y 轴(true 对于窗口)。 |
offset |
Number | 0 |
下拉容器的偏移量。 |
animation |
String | uk-animation-fade |
以空格分隔的动画名称。动画输出用逗号分隔。 |
animate-out |
Boolean | false |
关闭 drop 时使用动画。 |
duration |
Number | 200 |
下拉栏过渡效果的持续时间。 |
container |
布尔值 | false |
通过选择器定义目标容器,以指定下拉菜单应附加在 DOM 中的哪个位置。 |
了解更多JavaScript 组件的内容。
UIkit.navbar(element, options);
将在附加了此组件的元素上触发以下事件:
Name | Description |
---|---|
beforeshow |
元素显示前触发。返回preventDefault() 阻止触发 |
show |
=显示一个项目后触发 |
shown |
元素的显示动画完成后触发。 |
beforehide |
在隐藏物品之前触发。可以通过调用preventDefault() 阻止触发。 |
hide |
Fires after an item's hide animation has started. |
hidden |
Fires after an item is hidden. |