Offcanvas侧滑导航
Build hidden sidebars into your project for navigation, shopping carts, and more with a few classes and our JavaScript plugin.使用一些类和我们的JavaScript插件,在项目中构建隐藏的侧栏,用于导航、购物车等。
How it works工作原理
Offcanvas is a sidebar component that can be toggled via JavaScript to appear from the left, right, or bottom edge of the viewport. Offcanvas是一个边栏组件,可以通过JavaScript进行切换,从视口的左、右或下边缘显示。Buttons or anchors are used as triggers that are attached to specific elements you toggle, and 按钮或锚定被用作附加到您切换的特定元素的触发器,data
attributes are used to invoke our JavaScript.data
属性被用于调用我们的JavaScript。
Offcanvas shares some of the same JavaScript code as modals.Offcanvas与modals共享一些相同的JavaScript代码。Conceptually, they are quite similar, but they are separate plugins.从概念上讲,它们非常相似,但它们是独立的插件。Similarly, some source Sass variables for offcanvas’s styles and dimensions are inherited from the modal’s variables.类似地,offcanvas样式和维度的一些源Sass变量继承自模态变量。When shown, offcanvas includes a default backdrop that can be clicked to hide the offcanvas.显示时,offcanvas包含一个默认背幕,单击该背幕可以隐藏offcanvas。Similar to modals, only one offcanvas can be shown at a time.与情态动词类似,一次只能显示一个offcanvas。
Heads up! Given how CSS handles animations, you cannot use 考虑到CSS处理动画的方式,您不能在margin
or translate
on an .offcanvas
element. .offcanvas
元素上使用margin
或translate
。Instead, use the class as an independent wrapping element.相反,将该类用作独立的包装元素。
prefers-reduced-motion
media query. prefers-reduced-motion
媒体查询。Examples例子
Offcanvas components非画布组件
Below is an offcanvas example that is shown by default (via 下面是默认情况下显示的offcanvas示例(通过.show
on .offcanvas
). .offcanvas
上的.show
)。Offcanvas includes support for a header with a close button and an optional body class for some initial Offcanvas包括对带有关闭按钮的标题的支持,以及对某些初始padding
. padding
的可选主体类的支持。We suggest that you include offcanvas headers with dismiss actions whenever possible, or provide an explicit dismiss action.我们建议您尽可能将offcanvas头包含在disclose操作中,或者提供一个明确的disclose操作。
Offcanvas
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvas" aria-labelledby="offcanvasLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasLabel">Offcanvas</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
Content for the offcanvas goes here. You can place just about any Bootstrap component or custom elements here.
</div>
</div>
Live demo现场演示
Use the buttons below to show and hide an offcanvas element via JavaScript that toggles the 使用下面的按钮通过JavaScript显示和隐藏offcanvas元素,该JavaScript将元素上的.show
class on an element with the .offcanvas
class..show
类与.offcanvas
类切换。
.offcanvas
hides content (default)隐藏内容(默认).offcanvas.show
shows content显示内容
You can use a link with the 您可以使用带有href
attribute, or a button with the data-bs-target
attribute. href
属性的链接,或带有data-bs-target
属性的按钮。In both cases, the 在这两种情况下都需要data-bs-toggle="offcanvas"
is required.data-bs-toggle="offcanvas"
。
Offcanvas
<a class="btn btn-primary" data-bs-toggle="offcanvas" href="#offcanvasExample" role="button" aria-controls="offcanvasExample">
Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample" aria-controls="offcanvasExample">
Button with data-bs-target
</button>
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div>
Some text as placeholder. In real life you can have the elements you have chosen. Like, text, images, lists, etc.
</div>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
Dropdown button
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
</div>
</div>
Placement安置
There’s no default placement for offcanvas components, so you must add one of the modifier classes below;offcanvas组件没有默认放置,因此必须添加下面的一个修饰符类;
.offcanvas-start
places offcanvas on the left of the viewport (shown above)将offcanvas放置在视口左侧(如上所示).offcanvas-end
places offcanvas on the right of the viewport将画布放置在视口右侧.offcanvas-top
places offcanvas on the top of the viewport将画布放置在视口顶部.offcanvas-bottom
places offcanvas on the bottom of the viewport将画布放置在视口底部
Try the top, right, and bottom examples out below.尝试下面的顶部、右侧和底部示例。
Offcanvas top
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasTop" aria-controls="offcanvasTop">Toggle top offcanvas</button>
<div class="offcanvas offcanvas-top" tabindex="-1" id="offcanvasTop" aria-labelledby="offcanvasTopLabel">
<div class="offcanvas-header">
<h5 id="offcanvasTopLabel">Offcanvas top</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
...
</div>
</div>
Offcanvas right
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight" aria-controls="offcanvasRight">Toggle right offcanvas</button>
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight" aria-labelledby="offcanvasRightLabel">
<div class="offcanvas-header">
<h5 id="offcanvasRightLabel">Offcanvas right</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
...
</div>
</div>
Offcanvas bottom
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasBottom" aria-controls="offcanvasBottom">Toggle bottom offcanvas</button>
<div class="offcanvas offcanvas-bottom" tabindex="-1" id="offcanvasBottom" aria-labelledby="offcanvasBottomLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasBottomLabel">Offcanvas bottom</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body small">
...
</div>
</div>
Backdrop背幕
Scrolling the 当offcanvas及其背幕可见时,滚动<body>
element is disabled when an offcanvas and its backdrop are visible. <body>
元素被禁用。Use the 使用data-bs-scroll
attribute to toggle <body>
scrolling and data-bs-backdrop
to toggle the backdrop.data-bs-scroll
属性以切换<body>
滚动,使用data-bs-backdrop
以切换背幕。
Colored with scrolling
Try scrolling the rest of the page to see this option in action.
Offcanvas with backdrop
.....
Backdroped with scrolling
Try scrolling the rest of the page to see this option in action.
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasScrolling" aria-controls="offcanvasScrolling">Enable body scrolling</button>
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasWithBackdrop" aria-controls="offcanvasWithBackdrop">Enable backdrop (default)</button>
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasWithBothOptions" aria-controls="offcanvasWithBothOptions">Enable both scrolling & backdrop</button>
<div class="offcanvas offcanvas-start" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="offcanvasScrolling" aria-labelledby="offcanvasScrollingLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasScrollingLabel">Colored with scrolling</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<p>Try scrolling the rest of the page to see this option in action.</p>
</div>
</div>
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasWithBackdrop" aria-labelledby="offcanvasWithBackdropLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasWithBackdropLabel">Offcanvas with backdrop</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<p>.....</p>
</div>
</div>
<div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="offcanvasWithBothOptions" aria-labelledby="offcanvasWithBothOptionsLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasWithBothOptionsLabel">Backdroped with scrolling</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<p>Try scrolling the rest of the page to see this option in action.</p>
</div>
</div>
Accessibility可访问性
Since the offcanvas panel is conceptually a modal dialog, be sure to add 由于offcanvas面板在概念上是一个模态对话框,请确保对aria-labelledby="..."
—referencing the offcanvas title—to .offcanvas
. .offcanvas
添加aria-labelledby="..."
,引用offcanvas的标题。Note that you don’t need to add 请注意,你不需要添加role="dialog"
since we already add it via JavaScript.role="dialog"
,因为我们已经通过JavaScript添加了它。
Sass
Variables变量
$offcanvas-padding-y: $modal-inner-padding;
$offcanvas-padding-x: $modal-inner-padding;
$offcanvas-horizontal-width: 400px;
$offcanvas-vertical-height: 30vh;
$offcanvas-transition-duration: .3s;
$offcanvas-border-color: $modal-content-border-color;
$offcanvas-border-width: $modal-content-border-width;
$offcanvas-title-line-height: $modal-title-line-height;
$offcanvas-bg-color: $modal-content-bg;
$offcanvas-color: $modal-content-color;
$offcanvas-box-shadow: $modal-content-box-shadow-xs;
$offcanvas-backdrop-bg: $modal-backdrop-bg;
$offcanvas-backdrop-opacity: $modal-backdrop-opacity;
Usage用法
The offcanvas plugin utilizes a few classes and attributes to handle the heavy lifting:offcanvas插件利用几个类和属性来处理繁重的工作:
.offcanvas
hides the content隐藏内容.offcanvas.show
shows the content显示内容.offcanvas-start
hides the offcanvas on the left隐藏左侧的画布.offcanvas-end
hides the offcanvas on the right隐藏右侧的画布.offcanvas-bottom
hides the offcanvas on the bottom隐藏底部的画布
Add a dismiss button with the data-bs-dismiss="offcanvas"
attribute, which triggers the JavaScript functionality. Be sure to use the <button>
element with it for proper behavior across all devices.
Via data attributes通过data属性
Toggle切换
Add 向元素添加data-bs-toggle="offcanvas"
and a data-bs-target
or href
to the element to automatically assign control of one offcanvas element. data-bs-toggle="offcanvas"
和data-bs-target
或href
以自动分配某个offcanvas元素的控制。The data-bs-target
attribute accepts a CSS selector to apply the offcanvas to. data-bs-target
属性接受CSS选择器以应用offcanvas。Be sure to add the class 确保将offcanvas
to the offcanvas element. .offcanvas
类添加到offcanvas元素。If you’d like it to default open, add the additional class 如果希望它默认打开,请添加额外的show
..show
类。
Dismiss撤除
Dismissal can be achieved with the 撤除可以通过offcanvas内按钮上的data
attribute on a button within the offcanvas as demonstrated below:data
属性实现,如下所示:
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
or on a button outside the offcanvas using the 或在offcanvas外的按钮上使用data-bs-target
as demonstrated below:data-bs-target
,如下所示:
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#my-offcanvas" aria-label="Close"></button>
Via 通过JavaScript
Enable manually with:通过以下方式手动启用:
var offcanvasElementList = [].slice.call(document.querySelectorAll('.offcanvas'))
var offcanvasList = offcanvasElementList.map(function (offcanvasEl) {
return new bootstrap.Offcanvas(offcanvasEl)
})
Options选项
Options can be passed via data attributes or JavaScript. 选项可以通过data属性或JavaScript传递。For data attributes, append the option name to 对于data属性,请将选项名称追加到data-bs-
, as in data-bs-backdrop=""
.data-bs-
后面,譬如使用data-bs-backdrop=""
。
backdrop |
boolean | true |
|
keyboard |
boolean | true |
|
scroll |
boolean | false |
Methods方法
Asynchronous methods and transitions异步方法和转换
All API methods are asynchronous and start a transition. 所有API方法都是异步的,并开始转换。They return to the caller as soon as the transition is started but before it ends. 转换一开始,但在转换结束之前,它们就返回给调用方。In addition, a method call on a transitioning component will be ignored.此外,对转换组件的方法调用将被忽略。
See our JavaScript documentation for more information.有关更多信息,请参阅我们的JavaScript文档。
Activates your content as an offcanvas element. 将内容激活为非画布元素。Accepts an optional options 接受可选的选项object
.object
。
You can create an offcanvas instance with the constructor, for example:您可以使用构造函数创建offcanvas实例,例如:
var myOffcanvas = document.getElementById('myOffcanvas')
var bsOffcanvas = new bootstrap.Offcanvas(myOffcanvas)
toggle |
shown.bs.offcanvas or hidden.bs.offcanvas event occurs).shown.bs.offcanvas 事件或hidden.bs.offcanvas 事件发生前) |
show |
shown.bs.offcanvas event occurs).hidden.bs.offcanvas 事件发生前)。 |
hide |
hidden.bs.offcanvas event occurs).hidden.bs.offcanvas 事件发生前)。 |
getInstance |
|
getOrCreateInstance |
Events事件
Bootstrap’s offcanvas class exposes a few events for hooking into offcanvas functionality.Bootstrap的.offcanvas
类公开了一些事件,用于连接到offcanvas功能。
show.bs.offcanvas |
show instance method is called.show 实例方法时,此事件立即激发。 |
shown.bs.offcanvas |
|
hide.bs.offcanvas |
hide method has been called.hide 方法后,将立即触发此事件。 |
hidden.bs.offcanvas |
var myOffcanvas = document.getElementById('myOffcanvas')
myOffcanvas.addEventListener('hidden.bs.offcanvas', function () {
// do something...
})