js-offcanvas
jQuery accessible Offcanvas plugin, using ARIA
Why it is accessible
- It relies on ARIA Design pattern for Dialogs
- The tab key loops through all of the keyboard focusable items within the offcanvas
- You can close it using Esc.
Features
- Uses CSS transforms & transitions.
- BEM c-offcanvas c-offcanvas--left is-open
- From Any Direction: left, right, top and bottom.
- Overlay, Reveal and Push.
- API & Events
- Package managers Bower & NPM
Getting Started
- Install with npm:
npm install js-offcanvas
- Install with yarn:
yarn add js-offcanvas
JS & CSS
Include the .css
and .js
files in your site.
CDN
HTML
Offcanvas works on a container element with no styles applied.
Menu <!-- Your Main Content goes here -->
Initialize
;
Initialize with HTML
Trigger Button
Include the CSS-Class js-offcanvas-trigger
and data-offcanvas-trigger="id-of-your-offcanvas"
Menu
Offcanvas Element
Include the CSS-Class js-offcanvas
and data-offcanvas-options="{options}"
...
Trigger Enhance
// you have to trigger enhance ;
Options
;
Examples on Codepen
Bootstrap v4
Options
Set instance options by passing a valid object at initialization, or to the public defaults method. Custom options for a specific instance can also be set by attaching a data-offcanvas-options attribute to the target elment. This attribute should contain the properly formatted JSON object representing the custom options.
data-offcanvas-options='{ "modifiers": "left,overlay",... }'
Name | Default | Type |
---|---|---|
modifiers | "left,overlay" | string |
baseClass | "c-offcanvas" | string |
modalClass | "c-offcanvas-bg" | string |
contentClass | "c-offcanvas-content-wrap" | string |
closeButtonClass | "js-offcanvas-close" | string |
role | "dialog" | string |
bodyModifierClass | "has-offcanvas" | string |
supportNoTransitionsClass | "support-no-transitions" | string |
resize | false | boolean |
triggerButton | null | string |
onInit | null | function |
onOpen | null | function |
onClose | null | function |
API
The offcanvas API offers a couple of methods to control the offcanvas and are publicly available to all active instances.
var dataOffcanvas = data'offcanvas-component';
Methods
open
dataOffcanvas;
close
dataOffcanvas;
toggle
dataOffcanvas;
Callbacks
onInit
Fires an event when offcanvas is initialized.
dataOffcanvas { console;};
onOpen
Fires an event when offcanvas is opened.
dataOffcanvas { console;};
onClose
Fires an event when offcanvas is closed.
dataOffcanvas { console;};
Events
jQuery.offcanvas fires several events. Simply listen for them with the jQuery.on function. All events are namespaced with offcanvas.
beforecreate
Fires an event before the offcanvas is initialized.
;
create
Fired once the Plugin is initialized.
;
open
Fired when the open
method is called.
;
close
Fired when the close
method is called.
;
resizing
Fired when the window is resized.
;
clicked
Fired when the trigger-btn is clicked.
;
Dependencies
- jQuery
License
Licensed under the MIT license.