//simple sidebar v1.0.3 by dcdeiv https://github.com/dcdeiv // gplv2 http://www.gnu.org/licenses/gpl-2.0-standalone.html (function( $ ) { $.fn.simplesidebar = function( options ) { //declaring all global variables var sbw, align, callbacka, callbackb, //allowing user customisation defaults = { settings: { opener: undefined, wrapper: undefined, //html tag is not safe. please, use a container/wrapper div ignore: undefined, data: 'ssbplugin', animation: { duration: 500, //milliseconds (0.5s = 500ms) easing: 'swing' } }, sidebar: { align: undefined, width: 350, //pixels gap: 64, //pixels closinglinks: 'a', style: { zindex: 3000 } }, mask: { style: { backgroundcolor: 'black', opacity: 0.5, filter: 'alpha(opacity=50)' //ie8 and earlier } } }, config = $.extend( true, defaults, options ), $sidebar = this, $opener = $( config.settings.opener ), $wrapper = $( config.settings.wrapper ), $ignore = $( config.settings.ignore ), dataname = config.settings.data, duration = config.settings.animation.duration, easing = config.settings.animation.easing, defalign = config.sidebar.align, sbmaxw = config.sidebar.width, gap = config.sidebar.gap, $links = config.sidebar.closinglinks, defstyle = config.sidebar.style, maskdef = config.mask.style, winmaxw = sbmaxw + gap, //selecting all fixed elements except the sidebar and the ignore elements $fixedel = $( '*' ) .not( $ignore ) .not( $sidebar ) .filter(function() { return $( this ).css( 'position' ) == 'fixed'; }), $absolel = $( '*' ) .not( $ignore ) .filter(function() { return $( this ).css( 'position' ) == 'absolute'; }), //selecting all elements. $elements = $fixedel .add( $absolel ) .add( $sidebar ) .add( $wrapper ) .not( $ignore ), w = $( window ).width(), maskdef = { position: 'fixed', top: -200, right: -200, left: -200, bottom: -200, zindex: config.sidebar.style.zindex - 1 }, maskstyle = $.extend( {}, maskdef, maskdef ); //adding default style to $sidebar $sidebar .css( defstyle ) //wrapping inner content to let it overflow .wrapinner( '
' ); var subwrapper = $sidebar.children().filter(function() { return $( this ).data( dataname ) === 'sub-wrapper' ; }); subwrapper.css({ width: '100%', height: '100%', overflow: 'auto' }); //appending to 'body' the mask-div and adding its style $( 'body' ).append( '
' ); var maskdiv = $( 'body' ).children().filter(function(){ return $( this ).data( dataname ) === 'mask' ; }); maskdiv .css( maskstyle ) .hide(); //animate $elements to the right var animatetoright = function() { var nsbw = $sidebar.width(); $elements.each(function() { $( this ).animate({ marginleft: '+=' + nsbw, marginright: '-=' + nsbw }, { duration: duration, easing: easing, complete: callbacka }); }); }, //animate $elements to the left animatetoleft = function() { var nsbw = $sidebar.width(); $elements.each(function() { $( this ).animate({ marginleft: '-=' + nsbw, marginright: '+=' + nsbw }, { duration: duration, easing: easing, complete: callbackb }); }); }, //hiding overflow [callback(a/b)] overflowtrue = function() { $( 'body, html' ).css({ overflow: 'hidden' }); $( maskdiv ).fadein(); }, //adding overflow [callback(a/b)] overflowfalse = function() { $( maskdiv ).fadeout(function() { $( 'body, html' ).css({ overflow: 'auto' }); }); }; //assigning value to sbw if ( w < winmaxw ) { sbw = w - gap; } else { sbw = sbmaxw; } //testing config.sidebar.align if( defalign === undefined || defalign === 'left' ) { align = 'left'; } else { align = 'right'; } //sidebar initial position if ( 'left' === align ) { $sidebar.css({ position: 'fixed', top: 0, left: 0, bottom: 0, width: sbw, marginleft: -sbw }); callbacka = overflowtrue; callbackb = overflowfalse; $opener.click( animatetoright ); maskdiv.click( animatetoleft ); $sidebar.on( 'click', $links, animatetoleft ); } else { $sidebar.css({ position: 'fixed', top: 0, bottom: 0, right: 0, width: sbw, marginright: -sbw }); callbacka = overflowfalse; callbackb = overflowtrue; $opener.click( animatetoleft ); maskdiv.click( animatetoright ); $sidebar.on( 'click', $links, animatetoright ); } //adding responsive to $sidebar $( window ).resize(function() { var rsbw, sbmar, w = $( this ).width(); if ( w < winmaxw ) { rsbw = w - gap; } else { rsbw = sbmaxw; } $sidebar.css({ width: rsbw }); //fixing $element position according to $sidebar new width (rsbw) if ( 'left' === align ) { sbmar = parseint( $sidebar.css( 'margin-left' ) ); if ( 0 > sbmar ) { $sidebar.css({ marginleft: -rsbw }); } else { $elements.not( $sidebar ) .css({ marginleft: + rsbw, marginright: - rsbw }); } } else { sbmar = parseint( $sidebar.css( 'margin-right' ) ); if ( 0 > sbmar ) { $sidebar.css({ marginright: -rsbw }); } else { $elements.not( $sidebar ) .css({ marginleft: - rsbw, marginright: + rsbw }); } } }); return this; }; })( jquery );