Easy to use jQuery offcanvas plugin.
npm install jquery-offcanvas  
> An easy to use jQuery offcanvas plugin.
This plugin provides an easy way to put content outside of the canvas and reveal it with a click on a button or any desired element. This is a useful pattern for mobile navigations and more.
As this is a jQuery plugin it depends on the jQuery library v1.7+. For smooth animations Velocity.js is used. Both dependencies are required.
You can install this module either with npm or download it manually. Include jQuery, Velocity.js and the plugin before the closing body tag:
``HTML`
Include the CSS file:
`HTML`
If you are not using npm just adjust the paths to match the file locations.
It's not required to have any specific markup. The plugin handles any positioning itself. The only requirement is a wrapping element around the offcanvas contents.
`JavaScript
var $el = $("#element").offcanvas();
$(".offcanvas-trigger").on("click", function() {
$el.offcanvas("toggle");
});
`
Clicks on the trigger element will toggle the offcanvas element.
Do not try to initialize more than one instance of jQuery.offcanvas on one page!
Options can be set on initialization:
`JavaScript`
$("#element").offcanvas({
origin: "right",
duration: 400
});
You can also set options via data-Attributes, which will overwrite the default value and the value specified on initialization:
`HTML`
...
Type: object `
Default:JavaScript`
{
container: "offcanvas",
element: "offcanvas-element",
inner: "offcanvas-inner",
open: "offcanvas-open",
outer: "offcanvas-outer",
overlay: "offcanvas-overlay"
}
Classes which will be applied to the elements.
If you change class names make sure to update them in the CSS file accordingly.
Type: String body
Default:
Page container, which will be animated. Expects a jQuery selector.
Type: String 220px
Default:
Width of the offcanvas element which will be revealed.
Tip: For better performance avoid using % values.
Type: number 300
Default:
Duration of the animation.
Type: String ease-in-out
Default:
Easing type for show and hide animations. You can use:
- jQuery UI's easings
- CSS3's named easings: ease, ease-in, ease-out, and ease-in-out
For more easing options check the Velocity.js documentation.
Type: String push
Default:
Effect used to transition the offcanvas element into the viewport. Possible values are push and slide-in-over. Check the demos for an impression of the effects.
Type: String left
Default:
Direction the offcanvas element is revealed from. Possible values are left and right.
Type: boolean false
Default:
Adds an overlay to cover the content of the page. A click anywhere on the overlay will hide the offcanvas.
Type: String rgba(0, 0, 0, 0.7)
Default:
Color of the overlay element. Best suited are rgba values to add a decent looking transparency. You can use rgba(0, 0, 0, 0.7) as starting point to play around. The overlay will be smoothly transitioned into the view.
The offcanvas API offers a couple of methods to control the offcanvas element. The methods are called like this:
`JavaScript`
$("#element").offcanvas("show");
Shows the offcanvas element.
Hides the offcanvas element.
Toggles the offcanvas element.
Destroys the jQuery.offcanvas instance and reverts all DOM changes.
jQuery.offcanvas fires several events. Simply listen for them with the jQuery.on function. All events are namespaced with offcanvas.
`JavaScript`
$("#element").on("shown.offcanvas", function() {
// do stuff when offcanvas is revealed and animation is finished
});
Fired once the Plugin is initialized.
Fired when the show method is called.
Fired when the show animation finished.
Fired when the hide method is called.
Fired when the offcanvas gets toggled. Gets fired on both, show and hide methods. The second function argument contains the offcanvas state as boolean.
`JavaScript`
$("#element").on("toggle.offcanvas", function(event, visible) {
console.log(visible) // outputs offcanvas state (true or false)
});
Fired when the hide` animation finished.