Skip to content

suwidadi/simpler-sidebar

 
 

Repository files navigation

simpler-sidebar

GitHub version Bower version npm version MIT adnd GPL-2 license

A jQuery plugin that allows to create a side nav as in modern mobile apps. It aims to simplicity so that everybody can use it no matter if expert programmers or not.

simpler-sidebar is a fork of simple-sidebar.


Download

Run one of these commands in your bash according to your needs.

git clone https://github.com/dcdeiv/simpler-sidebar.git

bower install simpler-sidebar

npm install simpler-sidebar

Or download the latest version from the releases page.

If you are updating, remember to read the Release History and to check for incompatibility issues.

Getting Started

You will need to prepare a specified HTML template in order to make it work properly. The code below is just an example from which you can and have to draw inspiration. Along with this plugin you are provided with some demo pages in the demo directory.

<div id="navbar">
	<!--
	#navbar is positioned fixed.

	It does not matter what element #toggle-sidebar is, give it a selector (in this example #toggle-sidebar).
	-->
	<span id="toggle-sidebar" class="button icon"></span>
</div>

<div id="sidebar">
	<!--
	simpler-sidebar will handle #sidebar's position.

	To let the content of your sidebar overflow, especially when you have a lot of content in it, you have to add a "wrapper" that wraps all content.
	-->
	<div id="sidebar-wrapper" class="sidebar-wrapper">
		<!--
		Links below are just an example. Give each clickable element, for example links, a class to trigger the closing animation.
		-->
		<a class="close-sidebar" href="#">Link</a>
		<a class="close-sidebar" href="#">Link</a>
		<a class="close-sidebar" href="#">Link</a>
		<a class="close-sidebar" href="#">Link</a>
	</div>
</div>

If you add the sidebar-wrapper (and you should), remember to give it this style attributes:

.sidebar-wrapper {
	position: relative;
	height: 100%;
	overflow: auto;
}

At the bottom of the web page, just before the </body> tag, include the jQuery library. If you are interested in better easing, include the jQuery-UI library too. Eventually include simpler-sidebar.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="simpler-sidebar/dist/jquery.simpler-sidebar.min.js"></script>

Call the simpler-sidebar plugin function and fill it with the options you need. Here is an example of some required options. Read the Options section for further informations.

<script>
	$(document).ready(function() {
		$('#sidebar').simplerSidebar({
			opener: '#toggle-sidebar',
			sidebar: {
				align: 'left', //or 'right' - This option can be ignored, the sidebar will automatically align to right.
				width: 300, //You can ignore this option, the sidebar will automatically size itself to 300px.
				closingLinks: '.close-sidebar' // If you ignore this option, the plugin will look for all links and this can be buggy. Choose a class for every object inside the sidebar that once clicked will close the sidebar.
			}
		});
	});
</script>

Options

You can access options in two ways.

The first way is to add an option in the plugin itself. For example:

$('#sidebar').simplerSidebar({
	opener: '#toggle-sidebar',
	animation: {
		duration: 1000,
		easing: 'easeInOutBounce'
	},
	sidebar: {
		align: 'right',
		width: 500,
		closingLinks: '.close'
	},
	mask: {
		display: false
	}
});

The second way to access options is by using the following key:

$.fn.simplerSidebar.settings

After this key, you have to add the proper option, for example:

$.fn.simplerSidebar.settings.animation.duration = 1000

All keys must be put above the main plugin function and there should not be duplicates between them since keys override the options in the plugin function. Read the Options List for further informations about all available options.

According to the example above, here is the other way to tweak options by using the key:

$.fn.simplerSidebar.settings.animation.duration = 1000;
$.fn.simplerSidebar.settings.animation.easing = 'easeInOutBounce';
$.fn.simplerSidebar.settings.sidebar.align = 'right';
$.fn.simplerSidebar.settings.sidebar.width = 500;
$.fn.simplerSidebar.settings.sidebar.closingLinks = '.close';
$.fn.simplerSidebar.settings.mask.display = false;

$('#sidebar').simplerSidebar({
	opener: '#toggle-sidebar',
});

You can also override multiple options by using the key but it is not safe and it could be buggy, especially when you try to override sidebar. However you can safely override css such as $.fn.simplerSidebar.settings.mask.css, for example:

$.fn.simplerSidebar.settings.mask.css = {
	backgroundColor: 'black',
	opacity: 0.5,
	filter: 'Alpha(opacity=50)'
};

Options List

  • opener: selector for the button/icon that will trigger the animation.
  • attr: is the data-* attribute that makes the plugin works. If simplersidebar is somehow causing you issues, you can change it.
  • top: is the position-top of the entire plugin. You can choose whatever number you want (better if you choose it according to the navbar's height) or let it be 0 by ignoring it.
  • animation:
    • duration: the duration of the animation in milliseconds.
    • easing: the type of animation. For more animations include the jQuery-UI library and check out this page. I strongly suggest not to play with easing because they haven't been tested all yet. I suggest to use simple easing like easeOutQuint.
  • sidebar:
    • align: default is undefined which means that is aligned to right. If you want to align it to left, write left.
    • width: the max width of the sidebar, this option is default to 300, please change it as you please.
    • gap: the gap is the space between the left margin of the sidebar and the left side of the window (and viceversa). It is useful so that the user can click that space to close the sidebar.
    • closingLinks: links or elements that close the sidebar. I suggest to choose a class and give it to all links and other elements such as icons, banner, images, etc, that are links or that are supposed to be clicked. By default it is a so every link in the sidebar will close the sidebar. You can use multiple selectors too but, avoid using nested selector otherwise the function will be triggered twice. For example you can select 'a, .close-sidebar' but if an element is <a class=".close-sidebar"> the animation will be triggered twice.
    • css: here you can store all css, anyway I suggest not to add more css attributes to the one below.
      • zIndex: by default is is 3000 but you have to change it to the higher z-index number in your css plus 1.
  • mask:
    • display: true or false. false will remove this option.
    • css: here you can store all css attributes to give the mask div. However I suggest to do it in your css file except for these below. You can call this div by its data attribute for example: [data-simplersidebar="mask"].
      • backgroundColor: the color of the mask. By default is 'black'.
      • opacity: by default is 0.5.
      • filter: IE opacity 0.5 = 50 and so on: 'Alpha(opacity=50)'.

Contributing

Help me improve simpler-sidebar and make it as perfect as possible, but first read the contribution guidelines.

Release History

  • v1.4.3 (2015-12-13) -
    • Add support to jQuery v1.11.3.
    • Rename tests folder to demo.
    • Rename demo pages.
    • Fix README (#14).
    • Release under MIT and GPL-2.0 licenses.
  • v1.4.0 (2015-08-19) - Fix resize issue #7.
  • v1.3.4 (2015-07-08) - Enhancement in the README.md, package.json, and bower.json files.
  • v1.3.3 (2015-07-02) -
    • Add Grunt. Simpler-Sidebar files are moved to dist/ and renamed to jquery.simpler-sidebar.js and jquery.simpler-sidebar.min.js.
    • Fix sidebar.closingLinks and sidebar.align.
  • v1.2.3 (2015-06-23) - Fix animations functions.
  • v1.2.2 (2015-06-16) - Add jQuery as dependency of NPM and Bower (#3)
  • v1.2.0 (2015-05-18) - Add support to AJAX and mask.display, change dataName to attr;
  • v1.1.1 (2015-05-15) - Add support to left sidebar.
  • v1.0.0 (2015-05-14) - Initial release.

License

Copyright (c) 2015 Davide Di Criscito

Dual licensed under the MIT and GPL-2.0 licenses.

About

A simple side nav in jQuery.

Resources

License

GPL-2.0, MIT licenses found

Licenses found

GPL-2.0
LICENSE-GPL
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 80.1%
  • JavaScript 17.5%
  • CSS 2.4%