UIkit3.x文档

导航栏

创建一个可用于网站内容导航的导航栏。

用法

导航栏组件由导航栏容器、导航栏本身和一个或多个导航菜单组成。

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>

导航栏可以包含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>

多列

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>

边界对齐

下拉菜单可以与导航栏的边界对齐。只需将boundary-align: true 参数附加到 uk-navbar 性即可。添加 align: left, align: centeralign: right 选项可以更改对齐方式。默认左对齐。

<nav class="uk-navbar-container" uk-navbar="boundary-align: true; align: center;">...</nav>
  • <nav class="uk-navbar-container" uk-navbar="boundary-align: true; 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 组件 及其定位功能。下面的示例演示了下拉菜单与父导航栏的边界两端对齐。

<div class="uk-navbar-dropdown" uk-drop="boundary: .parent; boundary-align: true; pos: bottom-justify;">...</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: !nav; boundary-align: true; pos: bottom-justify;">
                        <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: !nav; boundary-align: true; pos: bottom-justify;">
                       <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

将下拉菜单的宽度扩展到下拉菜单的宽度并不带默认样式地显示该下拉菜单。要将下拉菜单放在这样的下拉栏里,请将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>

推动/Push

默认情况下,下拉栏是遮罩层形式.添加 dropbar-mode: push; 选项,可以将下拉栏改为向下推开下方内容展示出来。

Note The push mode does not work well if the navbar is contained within a sticky component.

<nav class="uk-navbar-container" uk-navbar="dropbar: true; dropbar-mode: push">...</nav>
<div class="uk-navbar-dropbar"></div>
  • <div class="uk-position-relative">
    
        <nav class="uk-navbar-container" uk-navbar="dropbar: true; dropbar-mode: push">
    
            <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>

您可以显示一个下拉栏,而不是使用下拉菜单,这意味着子导航以全宽度显示在导航栏下方。 只需在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
mode String click, hover 下拉菜单的触发模式: click, hover
delay-show Number 0 hover悬停模式下显示下拉菜单的延迟时间(以毫秒为单位)。
delay-hide Number 800 hover悬停模式下隐藏下拉菜单的延迟时间(以毫秒为单位)。
boundary CSS selector window 维持下拉菜单可见性的参考元素
boundary-align Boolean false 将下拉列表与边界对齐。
offset Number 0 下拉容器的偏移量。
dropbar Boolean false 启用或禁用下拉栏行为。
dropbar-mode String slide 下拉栏显示的模式:slide, push
duration Number 200 下拉栏过渡效果的持续时间。

JavaScript

了解更多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.