创建滑入滑出页面的抽屉式侧边栏,特别适用于创建移动端的导航。
要应用此组件,请将uk-offcanvas
属性添加到父级<div>
元素并使用以下class。
Class | Description |
---|---|
.uk-offcanvas-bar |
添加到子级<div> 元素。 |
.uk-offcanvas-close |
将这个class和uk-close 属性添加到<a> 或 <button> 元素创建一个关闭按钮。 |
您可以使用任何元素来切换抽屉式侧边栏。要启用相关的JavaScript,需要添uk-toggle
属性。 <a>
元素需要链接到抽屉容器的id。如果您使用的不是<a>而是其他元素(比如按钮),那么只需添加uk-toggle="target: #ID"
属性指向抽屉容器的ID。
<body>
<!-- This is a button toggling the off-canvas -->
<button uk-toggle="target: #my-id" type="button"></button>
<!-- This is an anchor toggling the off-canvas -->
<a href="#my-id" uk-toggle></a>
<!-- This is the off-canvas -->
<div id="my-id" uk-offcanvas>
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
</div>
</div>
</body>
<button class="uk-button uk-button-default uk-margin-small-right" type="button" uk-toggle="target: #offcanvas-usage">Open</button>
<a href="#offcanvas-usage" uk-toggle>Open</a>
<div id="offcanvas-usage" uk-offcanvas>
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
<h3>Title</h3>
<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.</p>
</div>
</div>
要添加一个遮罩来遮挡页面,需要将overlay: true
参数添加到uk-offcanvas
属性。
<div id="my-id" uk-offcanvas="overlay: true">...</div>
<button class="uk-button uk-button-default" type="button" uk-toggle="target: #offcanvas-overlay">Open</button>
<div id="offcanvas-overlay" uk-offcanvas="overlay: true">
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
<h3>Title</h3>
<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.</p>
</div>
</div>
将flip: true
参数添加到uk-offcanvas
属性中以调整其对齐方式,使其从右侧滑入。
<div id="my-id" uk-offcanvas="flip: true">...</div>
<button class="uk-button uk-button-default" type="button" uk-toggle="target: #offcanvas-flip">Open</button>
<div id="offcanvas-flip" uk-offcanvas="flip: true; overlay: true">
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
<h3>Title</h3>
<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.</p>
</div>
</div>
默认情况下抽屉为滑入页面,您还可以使用以下属性改变抽屉进入页面的方式:
Parameter | Description |
---|---|
mode: slide |
抽屉滑入并遮罩页面内容,这是默认模式。 |
mode: push |
抽屉滑入并推动网站页面。 |
mode: reveal |
抽屉滑入,并且在推动页面时显示其内容。 |
mode: none |
抽屉不带动画地直接显示出来并遮盖页面 |
<div id="my-id" uk-offcanvas="mode: push">...</div>
<button class="uk-button uk-button-default uk-margin-small-right" type="button" uk-toggle="target: #offcanvas-slide">Slide</button>
<button class="uk-button uk-button-default uk-margin-small-right" type="button" uk-toggle="target: #offcanvas-push">Push</button>
<button class="uk-button uk-button-default uk-margin-small-right" type="button" uk-toggle="target: #offcanvas-reveal">Reveal</button>
<button class="uk-button uk-button-default uk-margin-small-right" type="button" uk-toggle="target: #offcanvas-none">None</button>
<div id="offcanvas-slide" uk-offcanvas="overlay: true">
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
<h3>Title</h3>
<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.</p>
</div>
</div>
<div id="offcanvas-push" uk-offcanvas="mode: push; overlay: true">
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
<h3>Title</h3>
<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.</p>
</div>
</div>
<div id="offcanvas-reveal" uk-offcanvas="mode: reveal; overlay: true">
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
<h3>Title</h3>
<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.</p>
</div>
</div>
<div id="offcanvas-none" uk-offcanvas="mode: none; overlay: true">
<div class="uk-offcanvas-bar">
<button class="uk-offcanvas-close" type="button" uk-close></button>
<h3>Title</h3>
<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.</p>
</div>
</div>
您可以在抽屉内使用Nav 组件 创建适合移动设备的导航。
<div id="my-id" uk-offcanvas>
<div class="uk-offcanvas-bar">
<ul class="uk-nav uk-nav-default">...</ul>
</div>
</div>
<button class="uk-button uk-button-default uk-margin-small-right" type="button" uk-toggle="target: #offcanvas-nav-primary">Primary Nav</button>
<button class="uk-button uk-button-default" type="button" uk-toggle="target: #offcanvas-nav">Default Nav</button>
<div id="offcanvas-nav-primary" uk-offcanvas="overlay: true">
<div class="uk-offcanvas-bar uk-flex uk-flex-column">
<ul class="uk-nav uk-nav-primary uk-nav-center uk-margin-auto-vertical">
<li class="uk-active"><a href="#">Active</a></li>
<li class="uk-parent">
<a href="#">Parent</a>
<ul class="uk-nav-sub">
<li><a href="#">Sub item</a></li>
<li><a href="#">Sub item</a></li>
</ul>
</li>
<li class="uk-nav-header">Header</li>
<li><a href="#"><span class="uk-margin-small-right" uk-icon="icon: table"></span> Item</a></li>
<li><a href="#"><span class="uk-margin-small-right" uk-icon="icon: thumbnails"></span> Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#"><span class="uk-margin-small-right" uk-icon="icon: trash"></span> Item</a></li>
</ul>
</div>
</div>
<div id="offcanvas-nav" uk-offcanvas="overlay: true">
<div class="uk-offcanvas-bar">
<ul class="uk-nav uk-nav-default">
<li class="uk-active"><a href="#">Active</a></li>
<li class="uk-parent">
<a href="#">Parent</a>
<ul class="uk-nav-sub">
<li><a href="#">Sub item</a></li>
<li><a href="#">Sub item</a></li>
</ul>
</li>
<li class="uk-nav-header">Header</li>
<li><a href="#"><span class="uk-margin-small-right" uk-icon="icon: table"></span> Item</a></li>
<li><a href="#"><span class="uk-margin-small-right" uk-icon="icon: thumbnails"></span> Item</a></li>
<li class="uk-nav-divider"></li>
<li><a href="#"><span class="uk-margin-small-right" uk-icon="icon: trash"></span> Item</a></li>
</ul>
</div>
</div>
可以在组件属性中使用以下选项,用分号隔开多个选项。 了解更多
Option | Value | Default | Description |
---|---|---|---|
mode |
String | slide |
抽屉的动画模式:slide , reveal , push 或 none . |
flip |
Boolean | false |
将抽屉放置到右侧 |
overlay |
Boolean | false |
显示抽屉和遮罩层 |
esc-close |
Boolean | true |
当按下Esc 键时关闭抽屉。 |
bg-close |
Boolean | true |
点击背景时关闭抽屉。 |
container |
String | false |
通过选择器定义一个目标容器,以指定将抽屉添加到DOM的位置。将其设置为false 将阻止此行为。 |
mode
是 Primary 的选项,如果它是属性中的唯一选项,则可以省略它的键。
<span uk-offcanvas="push"></span>
了解更多JavaScript 组件的内容。
UIkit.offcanvas(element, options);
将在附加了此组件的元素上触发以下事件:
Name | Description |
---|---|
beforeshow |
在显示元素之前触发。 |
show |
显示一个元素后触发。 |
shown |
元素的显示动画完成后触发。 |
beforehide |
在隐藏元素之前触发。 |
hide |
元素的隐藏动画开始后触发。 |
hidden |
元素被隐藏后触发。 |
以下方法可用于该组件:
UIkit.offcanvas(element).show();
显示抽屉
UIkit.offcanvas(element).hide();
隐藏抽屉