切换器

动态过渡不同的内容面板。

用法

切换器组件由多个切换器及其相关的内容项组成。将 data-uk-switcher="{connect:'#ID'}" 属性添加到包含切换器的元素中,目标与包含内容项的元素上使用的 ID 相同。将 .uk-switcher 类添加到同一个元素。通常,切换器与其他组件结合使用,这里将展示其中一些。

示例

标记

<!-- This is the container of the toggling elements -->
<ul data-uk-switcher="{connect:'#my-id'}">
    <li><a href="">...</a></li>
</ul>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher">
    <li>...</li>
</ul>

从内容中切换项目

在某些情况下,您可能希望从活动内容中切换到另一个内容部分。这可以使用 data-uk-switcher-item 属性来实现。要定位项目,您需要将数据属性设置为相应内容项的编号。

示例

标记

<!-- This is the nav containing the toggling elements -->
<ul data-uk-switcher="{connect:'#my-id'}">
    <li><a href="">...</a></li>
</ul>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher">
    <li> ... <a href="" data-uk-switcher-item="0">...</a></li>
    <li> ... <a href="" data-uk-switcher-item="1">...</a></li>
</ul>

将属性设置为 nextprevious 将切换到相邻的项目。

标记

<li> ... <a href="" data-uk-switcher-item="next">...</a></li>
<li> ... <a href="" data-uk-switcher-item="previous">...</a></li>

连接多个项目

还可以连接多个内容容器。只需使用其他容器的 ID 扩展 connect 参数即可。

示例

容器 1

  • 来自容器 1 的问候!
  • 来自容器 1 的再次问候!
  • 来自容器 1 的巴辛加!

容器 2

  • 来自容器 2 的问候!
  • 来自容器 2 的再次问候!
  • 来自容器 2 的巴辛加!

标记

<!-- This is the nav containing the toggling elements -->
<ul data-uk-switcher="{connect:'#my-id-one, #my-id-two'}">
    <li><a href="">...</a></li>
</ul>

<!-- These are the containers of the content items -->
<ul id="my-id-one" class="uk-switcher">
    <li>...</li>
</ul>

<ul id="my-id-two" class="uk-switcher">
    <li>...</li>
</ul>

动画

切换器组件允许您在切换项目时向其添加不同的动画。您只需将 animation 参数添加到数据属性并定义要应用的动画。请查看下表,了解提供的动画概述。

描述
fade 元素淡入。
scale 项目向上缩放。
slide-top 项目从顶部滑入。
slide-bottom 项目从底部滑入。
slide-left 项目从左侧滑入。
slide-right 项目从右侧滑入。
slide-horizontal 项目水平滑动,方向取决于项目的相邻位置。
slide-vertical 项目垂直滑动,方向取决于项目的相邻位置。

示例

淡入
  • 你好!
  • 又见面了!
  • 巴辛加!
缩放
  • 你好!
  • 又见面了!
  • 巴辛加!
顶部滑动
  • 你好!
  • 又见面了!
  • 巴辛加!
底部滑动
  • 你好!
  • 又见面了!
  • 巴辛加!
左侧滑动
  • 你好!
  • 又见面了!
  • 巴辛加!
右侧滑动
  • 你好!
  • 又见面了!
  • 巴辛加!
水平滑动
  • 你好!
  • 又见面了!
  • 巴辛加!
垂直滑动
  • 你好!
  • 又见面了!
  • 巴辛加!

标记

<!-- This is the container of the toggling elements -->
<ul data-uk-switcher="{connect:'#my-id', animation: 'fade'}">
    <li><a href="">...</a></li>
</ul>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher">
    <li>...</li>
</ul>

自定义动画

您还可以使用来自动画组件uk-animation-* 类来应用多个动画。这样,您甚至可以创建自己的自定义类,以将不同的过渡效果应用于切换器。

示例

  • 你好!
  • 又见面了!
  • 巴辛加!

标记

<!-- This is the container of the toggling elements -->
<ul data-uk-switcher="{connect:'#my-id', animation: 'uk-animation-fade, uk-animation-slide-left'}">
    <li><a href="">...</a></li>
</ul>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher">
    <li>...</li>
</ul>

带有子导航的切换器

切换器可以轻松应用于子导航组件

示例

标记

<!-- This is the subnav containing the toggling elements -->
<ul class="uk-subnav uk-subnav-pill" data-uk-switcher="{connect:'#my-id'}">
    <li><a href="">...</a></li>
</ul>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher">
    <li>...</li>
</ul>

带有选项卡的切换器

作为规则的例外,将 data-uk-tab="{connect:'#ID'}" 属性添加到选项卡式导航,使用参数“tab”而不是“switcher”,将切换器与标签页组件组合起来。

示例

  • 你好!
  • 又见面了!
  • 巴辛加!
  • 你好!
  • 又见面了!
  • 巴辛加!

标记

<!-- This is the tabbed navigation containing the toggling elements -->
<ul class="uk-tab" data-uk-tab="{connect:'#my-id'}">
    <li><a href="">...</a></li>
</ul>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher uk-margin">
    <li>...</li>
</ul>

注意当使用底部选项卡修饰符时,导航和内容需要以相反的顺序排列,以便内容显示在选项卡之上。


垂直选项卡

使用栅格组件来正确显示具有垂直选项卡式导航的内容。

示例

  • 你好!
  • 又见面了!
  • 巴辛加!
  • 你好!
  • 又见面了!
  • 巴辛加!

示例

<!-- This is the left vertical tabbed navigation -->
<div class="uk-grid">
    <div class="uk-width-medium-1-2">
        <!-- This is the vertical tabbed navigation containing the toggling elements -->
        <ul class="uk-tab uk-tab-left" data-uk-tab="{connect:'#my-id'}">...</ul>
    </div>
    <div class="uk-width-medium-1-2">
        <!-- This is the container of the content items -->
        <ul id="my-id" class="uk-switcher">...</ul>
    </div>
</div>

<!-- This is the right vertical tabbed navigation -->
<div class="uk-grid">
    <div class="uk-width-medium-1-2 uk-push-1-2">
        <!-- This is the vertical tabbed navigation containing the toggling elements -->
        <ul class="uk-tab uk-tab-right" data-uk-tab="{connect:'#my-id'}">...</ul>
    </div>
    <div class="uk-width-medium-1-2 uk-pull-1-2">
        <!-- This is the container of the content items -->
        <ul id="my-id" class="uk-switcher">...</ul>
    </div>
</div>

带有按钮的切换器

切换器还可以应用于来自按钮组件的按钮或按钮组。只需将切换器数据属性添加到按钮组周围的 <div> 元素或具有 .button-group 类的元素即可。

示例

链接
  • 你好!
  • 又见面了!
  • 巴辛加!
链接
  • 你好!
  • 又见面了!
  • 巴辛加!

标记

<!-- This is the container of the toggling buttons -->
<div data-uk-switcher="{connect:'#my-id'}">
    <button class="uk-button" type="button">...</button>
</div>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher uk-margin">...</ul>

<!-- This is the button group containing the toggling buttons -->
<div class="uk-button-group" data-uk-switcher="{connect:'#my-id'}">
    <button class="uk-button" type="button">...</button>
</div>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher uk-margin">...</ul>

带有导航的切换器

要将切换器应用于导航组件,请将数据属性添加到导航 <ul>。使用栅格组件以网格布局排列导航和内容。

示例

标记

<div class="uk-grid">
    <div class="uk-width-medium-1-4">

        <!-- This is the nav containing the toggling elements -->
        <ul class="uk-nav uk-nav-side" data-uk-switcher="{connect:'#my-id'}">
            <li><a href="">...</a></li>
        </ul>
    </div>
    <div class="uk-width-medium-3-4">

        <!-- This is the container of the content items -->
        <ul id="my-id" class="uk-switcher">
            <li>...</li>
        </ul>
    </div>
</div>

JavaScript 选项

这是一个如何通过属性设置选项的示例

data-uk-switcher="{active:1}"
选项 可能的值 默认值 描述
connect CSS 选择器 false 相关项目容器
toggle CSS 选择器 '> *' 切换器 CSS 选择器,在点击时触发内容切换
active 整数 0 初始化时的活动索引
animation 混合值 false 预定义的动画名称或任何 uk-animation 类名称。
swiping 布尔值 true 启用/禁用滑动时内容更改

事件

您可以将回调绑定到以下事件以实现自定义功能

名称 参数 描述
show.uk.switcher 事件,活动项目 在切换器项目显示/更改时

示例

$('[data-uk-switcher]').on('show.uk.switcher', function(event, area){
    console.log("Switcher switched to ", area);
});