跳转到主要内容

文档

搜索

轻松创建美观的搜索。

用法

搜索组件包含一个搜索表单和搜索输入框本身。

类/属性 描述
.uk-search 将此类添加到容器元素以定义搜索组件。
.uk-search-input 将此类添加到 <input> 元素以创建搜索字段。
<form class="uk-search" aria-label="Search">
    <input class="uk-search-input" type="search" placeholder="" aria-label="">
</form>

默认情况下,搜索没有其他样式。在此示例中,我们使用的是 .uk-search-default 修饰符。

<form class="uk-search uk-search-default">
    <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
</form>

搜索图标

要创建搜索图标,请将 uk-search-icon 属性添加到 <span> 元素。要更改对齐方式,请添加 .uk-search-icon-flip 类。

<form class="uk-search uk-search-default">
    <span uk-search-icon></span>
    <input class="uk-search-input" type="search" placeholder="" aria-label="">
</form>

<form class="uk-search uk-search-default">
    <input class="uk-search-input" type="search" placeholder="" aria-label="">
    <span class="uk-search-icon-flip" uk-search-icon></span>
</form>
<div class="uk-margin">
    <form class="uk-search uk-search-default">
        <span uk-search-icon></span>
        <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
    </form>
</div>

<div class="uk-margin">
    <form class="uk-search uk-search-default">
        <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
        <span class="uk-search-icon-flip" uk-search-icon></span>
    </form>
</div>

可点击

要启用操作,请使用 <a><button> 元素来创建图标。

<form class="uk-search uk-search-default">
    <button uk-search-icon></button>
    <input class="uk-search-input" type="search" placeholder="" aria-label="">
</form>

<form class="uk-search uk-search-default">
    <input class="uk-search-input" type="search" placeholder="" aria-label="">
    <button class="uk-search-icon-flip" uk-search-icon></button>
</form>
<div class="uk-margin">
    <form class="uk-search uk-search-default">
        <button uk-search-icon></button>
        <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
    </form>
</div>

<div class="uk-margin">
    <form class="uk-search uk-search-default">
        <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
        <button class="uk-search-icon-flip" uk-search-icon></button>
    </form>
</div>

默认修饰符

要应用默认搜索样式,请添加 .uk-search-default 类。

<form class="uk-search uk-search-default">…</form>
<form class="uk-search uk-search-default">
    <span uk-search-icon></span>
    <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
</form>

要在 导航栏组件 中的导航栏中使用时应用专用的搜索样式,请添加 .uk-search-navbar 类。

<form class="uk-search uk-search-navbar">…</form>
<nav class="uk-navbar-container" uk-navbar>
    <div class="uk-navbar-left">

        <div class="uk-navbar-item">
            <form class="uk-search uk-search-navbar">
                <span uk-search-icon></span>
                <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
            </form>
        </div>

    </div>
</nav>

中等修饰符

要增加搜索的大小,例如在下拉菜单中使用搜索时,请添加 .uk-search-medium 类。

<form class="uk-search uk-search-medium">…</form>
<form class="uk-search uk-search-medium">
    <span uk-search-icon></span>
    <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
</form>

大型修饰符

要增加搜索的大小,例如在全屏模态框中使用搜索时,请添加 .uk-search-large 类。

<form class="uk-search uk-search-large">…</form>
<form class="uk-search uk-search-large">
    <span uk-search-icon></span>
    <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search">
</form>

切换

要在使用 <a><button> 元素时向搜索图标应用悬停状态,请添加 .uk-search-toggle 类。要创建搜索图标本身,请添加 uk-search-icon 属性。

<a class="uk-search-toggle" href="" uk-search-icon></a>
<a class="uk-search-toggle" href="" uk-search-icon></a>

导航栏中的搜索

搜索图标可以用作在导航栏内打开搜索的切换开关 – 作为覆盖层、下拉菜单或下拉列表 – 或在模态框外打开。只需将 .uk-navbar-toggle 类添加到图标即可。

<div class="uk-navbar-container" uk-navbar>
    <div class="uk-navbar-left">
        <a class="uk-navbar-toggle" uk-search-icon uk-toggle href=""></a>
    </div>
</div>

覆盖层

<nav class="uk-navbar-container uk-margin" uk-navbar>

    <div class="nav-overlay uk-navbar-left">

        <a class="uk-navbar-item uk-logo" href="#">Logo</a>

        <ul class="uk-navbar-nav">
            <li class="uk-active"><a href="#">Active</a></li>
            <li><a href="#">Item</a></li>
        </ul>

    </div>

    <div class="nav-overlay uk-navbar-right">

        <a class="uk-navbar-toggle" uk-search-icon uk-toggle="target: .nav-overlay; animation: uk-animation-fade" href="#"></a>

    </div>

    <div class="nav-overlay uk-navbar-left uk-flex-1" hidden>

        <div class="uk-navbar-item uk-width-expand">
            <form class="uk-search uk-search-navbar uk-width-1-1">
                <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search" autofocus>
            </form>
        </div>

        <a class="uk-navbar-toggle" uk-close uk-toggle="target: .nav-overlay; animation: uk-animation-fade" href="#"></a>

    </div>

</nav>

下拉

<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 class="uk-active"><a href="#">Active</a></li>
            <li><a href="#">Item</a></li>
        </ul>

    </div>
    <div class="uk-navbar-right">

        <div>
            <a class="uk-navbar-toggle" href uk-search-icon></a>
            <div class="uk-drop" uk-drop="mode: click; pos: left-center; offset: 0">
                <form class="uk-search uk-search-navbar uk-width-1-1">
                    <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search" autofocus>
                </form>
            </div>
        </div>

    </div>
</nav>
<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 class="uk-active"><a href="#">Active</a></li>
            <li><a href="#">Item</a></li>
        </ul>

    </div>
    <div class="uk-navbar-right">

        <div>
            <a class="uk-navbar-toggle" href uk-search-icon></a>
            <div class="uk-navbar-dropdown" uk-drop="mode: click; cls-drop: uk-navbar-dropdown; boundary: !.uk-navbar; flip: false">

                <div class="uk-grid-small uk-flex-middle" uk-grid>
                    <div class="uk-width-expand">
                        <form class="uk-search uk-search-navbar uk-width-1-1">
                            <input class="uk-search-input" type="search" placeholder="Search" aria-label="Search" autofocus>
                        </form>
                    </div>
                    <div class="uk-width-auto">
                        <a class="uk-drop-close" href="#" uk-close></a>
                    </div>
                </div>

            </div>
        </div>

    </div>
</nav>
<nav class="uk-navbar uk-navbar-container uk-margin">
    <div class="uk-navbar-left">

        <a class="uk-navbar-item uk-logo" href="#">Logo</a>

        <ul class="uk-navbar-nav">
            <li class="uk-active"><a href="#">Active</a></li>
            <li><a href="#">Item</a></li>
        </ul>

    </div>
    <div class="uk-navbar-right">

        <a class="uk-navbar-toggle" href="#modal-full" uk-search-icon uk-toggle></a>

    </div>
</nav>

<div id="modal-full" class="uk-modal-full uk-modal" uk-modal>
    <div class="uk-modal-dialog uk-flex uk-flex-center uk-flex-middle" uk-height-viewport>
        <button class="uk-modal-close-full" type="button" uk-close></button>
        <form class="uk-search uk-search-large">
            <input class="uk-search-input uk-text-center" type="search" placeholder="Search" aria-label="Search" autofocus>
        </form>
    </div>
</div>

可访问性

将适当的 WAI-ARIA 角色、状态和属性设置为搜索组件。

<input class="uk-search-input" type="search" aria-label="…">

国际化

搜索组件使用以下翻译字符串。了解有关翻译组件的更多信息。

默认 描述
切换 打开搜索 切换按钮的 aria-label 属性。
提交 提交搜索 提交按钮的 aria-label 属性。