Introduction

In this article we will create a sidebar for our website that looks very similar to the Windows 8 sidebar. This sidebar can be used as navigation menu or it can be used to show other options. The specialty of this sidebar is that it is visible only when the user wants it and for the rest of the time it will be hidden. So let's start.

Logic behind Sidebar

I have used the following logic to create the sidebar for my demo:

Coding the sidebar

Add the following HTML to your file:

<html>

<head>

<meta name="description" content="Locked!final win-8 sidebar." />

<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>

</head>

<body>

<div id="ctnr">

<div id="item">

<!-- for FB -->

<a href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.c-sharpcorner.com%2Fconference2014%2F" onclick="javascript:window.open(this.href,

'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;">

<img id="fb" src="http://static2.wikia.nocookie.net/__cb20130901040905/starwars/images/d/d1/FacebookIcon.png" width="100" height="100" />

</a>

<!-- for twitter -->

<a href="https://twitter.com/share?url=http://www.c-sharpcorner.com/conference2014/" onclick="javascript:window.open(this.href,

'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><img id="tw" src="http://icons.iconarchive.com/icons/fasticon/web-2/256/Twitter-icon.png" width="100" height="100" ></a>

<!-- for G+ -->

<a href="https://plus.google.com/share?url={http://www.c-sharpcorner.com/conference2014/}" onclick="javascript:window.open(this.href,

'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;"><img id="gp"

src="http://icons.iconarchive.com/icons/marcus-roberto/google-play/512/Google-plus-icon.png" width="100" height="100"/></a>

</div>

</div>

</body>

</html>

The preceding HTML is for creating a Sharing sidebar. You can use it as in Google Plus, Facebook and Twitter share sidebar.

Add the following CSS in your file for designing the sidebar.

body{

margin:0;

}

#ctnr{

background-color:black;

width:6%;

height: 100%;

position:fixed;

top:0px;

right:0px;

display:none;

}

#fb{

margin-top:100px;

border-radius:50% ;

}

#tw{

margin-top:100px;

border-radius:50% ;

}

#gp{

margin-top:100px;

border-radius:50% ;

}

#item{

display:none;

}

Now comes the difficult part. The jQuery is a bit tricky but if you have gone through the logic then it's a cakewalk.

Add the following jQuery to the file:

  1. $(document).ready(function(){

  2. $("body").css("width",$(window).width()+"px");

  3. $("body").css("height",$(window).height()+"px");

  4. $(document).mousemove(function(e){

  5. if(e.pageX===$(window).width()-1){

  6. $("#ctnr").show("slide",{direction:"right"},300);

  7. $("#item").show("slide",{direction:"right"},400);

  8. }elseif(e.pageX+$("#ctnr").width()<$(window).width()-1){

  9. $("#ctnr").hide("slide",{direction:"right"},300);

  10. $("#item").hide("slide",{direction:"right"},300);

  11. }

  12. });

  13. });

Line numbers 2-3 set the size of the body equal to the size of the screen. This step is important since we have given a container width in percentage.

Line number 4 adds a mouse movement listener on the document to track the mouse movement on the webpage.

Line number 5 checks whether or not the mouse is touching the right edge of the website.

Line number 7 brings in the side bar container.

Line number 8 brings in the side bar items. We have used two different timings for sliding to mimic a Windows 8 menu style.

Line number 9 checks if the mouse is away from side bar. Line numbers 10-11 hides the sidebar and its items.

Output

Live Demo

Summary

That's all for this article. I hope you have enjoyed reading this article. In case of any doubt feel free to ask in comments.

References