Creating Panel Menu with JQuery

Today, I am gonna show you how to create a very useful menu panel. Take a look at the demo of that first.

Well, this type of menu turns out to be very useful if you have a lot of links but you can’t show them in a drop down menu which may get very long. Other than that, it can also be used to allow quick form submissions or showing some piece of text. Basically, it is a div you can put just about anything in it. Let’s move on to creating this menu panel.

HTML

First of all, we create links which when hovered will show the associated panel.

  <div id="container">
    <ul id="menu">
      <li><a href="#" rel="panel_1">Link One</a></li>
      <li><a href="#" rel="panel_2">Link Two</a></li>
      <li><a href="#" rel="panel_3">Link Three</a></li>
    </ul>
  </div>

I will be using links’ rel attribute to refer to corresponding div element which is actually our panel element. Here is the html markup for our panel:

<div id="panel_1">
    <div class="left">
        <!-- The content for this panel goes here -->
    </div>
</div>

As can be seen, this panel has id panel_1 which is the same as rel value of the link shown above. That is how other two panels should be constructed.

CSS

First of all, we make sure that our menu is centered on the screen, so this is how we center the container div:

#container
{
  margin:0px auto;
  width:350px;
}

We make sure that there are no bullets there on the lists:

ul#menu
{
  list-style:none;
}

By default, list items are placed vertically because they are block-level elements, so we make them inline using this CSS so that they appear side by side with one pixel gap:

ul#menu li
{
  display:inline;
  background:#0099CC;
  float:left;
  margin-right:1px;
}

Then we make menu links as block-level elements because only after that we will be able to apply a width to them:

ul#menu li a
{
  color:#ffffff;
  font-weight:bold;
  text-decoration:none;
  display:block;
  width:100px;
  text-align:center;
  padding:5px;
}

Here is the CSS for any panel containing text. We will need to apply class text to any panel containing only text.

.text
{
  width:300px;
  text-align:justify;
  background:#333333;
  color:#CCCCCC;
  padding:15px;
}

Here is the CSS for any other panel:

div.left
{
  width:130px;
}

div.left ul
{
  list-style:none;
  margin-left:0px;
  padding:15px;
  background:#333333;
  color:#FFFFFF;
}

So for panels containing text, we will be using class text and for rest of the panels we will be using class left.

JQuery

First of all we create a global variable which will be equalized to opened panel later, so that we could use that variable later to operate on that opened panel like closing it.

<script type="text/javascript">

  var opened_one = null;

</script>

Now as mostly what we do, we use JQuery’s ready method to run our code. Our code will be run as soon as DOM becomes ready:

<script type="text/javascript">

  var opened_one = null;

  $(function(){
    // code here
  }

</script>

First of all we hide all panels using hide() method:

  // hide all panels first
  $('div[id*="panel"]').hide();

What it means is that hide all divs that have text panel anywhere in their id values. The *= means find given text anywhere in the provided attribute value.

To be able to actully position the panels right after the hovered links, we need to make them absolute positioned (I assume you have good understanding of CSS):

  // make the panels absolute positioned
  $('div[id*="panel"]').css('position', 'absolute');

Now we write the code for menu links which will be run when they are hovered upon using hover event:

  $('#menu a').hover(function(){
    // code here
  });

First we hide if there is a panel already open and get hovered link’s rel attribute value and store that in variable link_rel. We can get the attribute value using attr() method.

	// hide previously opened panel
	$(opened_one).hide();

	var link_rel = $(this).attr('rel');

Now we get the position of the hovered link (menu item) using offset() method and use that information to position the panels right after them using css() method of JQuery:

    //get the position of the hovered element
    var pos = $(this).offset();

   // get the panel near by hovered element now
    $('div#' + link_rel).css({
      'left': pos.left + 'px',
      'top': (pos.top + $(this).height() + 10) + 'px',
      'zIndex': '5000'
    });

As can be seen, we set the top of panel equal to that of hovered link plus its height plus 10 which we need because we have assigned in CSS with 5 pixels of padding. As per CSS box model, we need to calculate paddings for two sides thus equaling to 10. We also set the z-index of current panel to a larger value so that it appears over any other opened panel although possibility of this is less but we still want to make sure that it makes sense.

Then we show the corresponding panel using fadeIn() animation and once that it is opened/shown, we set the variable opened_one to this panel so that we could hide it later if another panel is to be opened.

    // finaly show the relevant hidden panel
    $('div#' + link_rel).fadeIn('slow');

   // this is currently opened
   opened_one = $('div#' + link_rel);

We want to hide the panel if the mouse moves away from its area using fadeOut():

    // hide it back when mouse arrow moves away
    $('div#' + link_rel).hover(function(){}, function(){
		$(this).fadeOut('slow');
    });

That’s all there is to it. Of couse explaining it takes more time and a lot of theory but if you think about it and practice, it should not be that hard to create this very useful menu panel using JQuery.

Download Code.

Creating Stylish Sliding Menu With JQuery

Today, we are going to create a very stylish sliding menu as shown in the picture on the left with the help of JQuery. Before we move on, please have a look at the demo of this menu.

In order to create this menu what is usually known as accordion, we will be adding HTML of course, CSS to style the menu and finally JQuery to create the kind of effect this menu has. Of course, once you create this menu, you can style it anyway you want; for example you can create background, active or hover images which can make this menu even more attractive or simply use colors instead. Ok, so let’s move on to creating this menu.

.

HTML

We will be using unordered lists for our menu. Here is how it looks like:

   <ul class="parent">
    	<li>
            <a href="#" class="slide">Title One</a>

            <ul class="child">
            	<li><a href="#">Link One</a></li>
                <li><a href="#">Link Two</a></li>
                <li><a href="#">Link Three</a></li>
                <li><a href="#">Link Four</a></li>
                <li><a href="#">Link Five</a></li>
            <!--<span class="hiddenSpellError" pre=""-->ul>

         </li>
    <!--<span class="hiddenSpellError" pre=""-->ul>
<!-- and more -->

As can be seen, there are two lists, the one with the class parent and other with that of child. The first list item of the parent list contains a link which is actually a heading that when clicked will show sub-links which are there in the child list. So the same markup structure goes for the rest of the headings and links.

The CSS

Here is the CSS for the menu:

a
{
	outline:none;
}

ul
{
	list-style:none;
}

ul.parent
{
	margin:0px;
}

ul.parent a
{
	display:block;
	width:200px;
	border-bottom:1px solid #336699;
	background:#3399FF;
	color:#FFFFFF;
	font-weight:bold;
	text-decoration:none;
	padding:7px;
}

ul.parent a:hover
{
	color:#000000;
	background:#66CC66
}

ul.child a
{
	background:#333333;
	color:#FFFFFF;
	border-bottom:1px solid #f6f6f6;
	font-weight:normal;
	margin-left:-40px;
	padding:7px;
	width:200px;
	display:block;
	line-height:10px;
}

ul.child a:hover
{
	color:#3333333;
	background:#CCCCCC
}

In some browsers, when you click on a link, an outline (sort of dotted border) is created around it, so we disable that behavior by setting:

a
{
	outline:none;
}

Then we remove the list style so that there are no bullets for ul lists and also remove any margins around it by setting value to 0:

ul
{
	list-style:none;
}

ul.parent
{
	margin:0px;
}

After that we set links’ display style to block so that we could apply a width to them and also set some other styles such as background:

ul.parent a
{
	display:block;
	width:200px;
	border-bottom:1px solid #336699;
	background:#3399FF;
	color:#FFFFFF;
	font-weight:bold;
	text-decoration:none;
	padding:7px;
}

Finally we set styles when mouse is over the links using :hover pseudo selector:

ul.parent a:hover
{
	color:#000000;
	background:#66CC66
}

JQuery

We use JQuery’s ready event which fires when DOM (document object model) becomes ready, the short version of this event is:

	$(function(){
	// code here
	});

When the page has loaded, we want to make sure that only the first heading and links are shown which can be done as follows:

	// hide all links except for the first
	$('ul.child:not(:first)').hide();

For each selected heading we set its color to #FF9900, so we want to change the color of the first heading so as to make it feel like selected by default when the page has loaded:

	$("a.slide:first").css("background-color","#FF9900");

Then we write the code for each heading’s click event:

	$('ul.parent a.slide').click(function(){
	});

What it means is that a link with class slide which is inside ul with class parent gets clicked, run the code specified. First, we set background color and mouse over and mouse out color for the current clicked heading and other headings:

	$('ul.parent a.slide').css("background-color","#3399FF");

	$('ul.parent a.slide').mouseout(function(){
		$(this).css("background-color","#3399FF");
	});

	$('ul.parent a.slide').mouseover(function(){
		$(this).css("background-color","#66CC66");
	});

	$(this).mouseout(function(){
		$(this).css("background-color","#FF9900");
	});

We now slide up all child elements:

	// slide all up
	$('ul.child').slideUp('slow');

Then we show the links of the current clicked heading:

	// show the links of current heading
	$(this).next().find('a').show();

$(this) refers to current clicked heading, that is $(‘ul.parent a.slide’, the next() method finds the next element in the hierarchy with respect to clicked element which is ul with class child, we find links inside that using find() method and finally show them. Finally, we slide down the ul with class child and prevent the default action of clicked link or heading.

Finally, we slide down the current heading:

  // slide down current heading
  $(this).next().slideDown('fast');

And we prevent default action of clicked heading which is actually
a link using:

// prevent default action
return false;

Here is the entire JQuery code:

	$(function(){
		// hide all links except for the first
		$('ul.child:not(:first)').hide();
		$("a.slide:first").css("background-color","#FF9900");

		$('ul.parent a.slide').click(function(){
			$('ul.parent a.slide').css("background-color","#3399FF");

			$('ul.parent a.slide').mouseout(function(){
				$(this).css("background-color","#3399FF");
			});

			$('ul.parent a.slide').mouseover(function(){
				$(this).css("background-color","#66CC66");
			});

			$(this).mouseout(function(){
				$(this).css("background-color","#FF9900");
			});

			// slide all up
			$('ul.child').slideUp('slow');

			// show the links of current heading
			$(this).next().find('a').show();

			// slide down current heading
			$(this).next().slideDown('fast');

			// prevent default action
			return false;
		});
	});

Download Code.

Creating Dropdown Menu Using CSS

dropdownIf you have been creating facebook applications, you know in a FBML application, the traditional javascript code is not allowed. The facebook platform has its own implementation of the javascript named FBJS (FaceBook JavaScript). The FBJS is very limited as compared to traditional javascript. So you would find yourself in trouble if you wanted to create a dropdown menu using FBJS. Now because traditional javascript is not allowed in a FBML application and FBJS is troublesome when it comes to creating menus, we will have to find some other solution to creating the dropdown menu. Well, CSS exhibits great flexibility to solve this problem. The CSS’s display property is used effectively to create the dropdown menu.

Here is the CSS code:

	/* Drop Down Menu Start */
	#container
	{
		width: 760px;
		margin:0 auto;
		font-size:11pt;
		background:#edeff4;
		font-size:12px;
		font-family:tahoma, verdana, arial;
	}
	#menu{position:absolute; z-index:1; border-top:1px solid #f6f6f6; font-weight:bold; margin-bottom:0px; left:0px; margin-left:0px; margin-top:-15px; color:#3b5898; font-size:11px; font-family:tahoma; } /* adjust margin property if needed */
	#menu ul .item{display:none;}
	#menu ul:hover .item{display:block;background:#edeff4;padding:5px;margin:1px; color:#3b5898;cursor:pointer; font-weight:normal;}
	#menu ul:hover .item a{color:#000000;text-decoration:none; font-size:11px; font-family:tahoma;cursor:pointer;}
	#menu ul:hover .item a:hover{color:#3b5898;cursor:pointer;}
	#menu ul:hover{color:#3b5898;background:#3b5898;color:#ffffff;}
	#menu ul{width:150px;float:left;margin:0px;padding:1px;background:#edeff4;list-style:none;cursor:pointer;}
	.clear{clear:both;height:10px;}
	/* Drop Down Menu End */

And here is the html code for the dropdown menu:

<div id="container" style="background:#edeff4; width:760px; position:relative; margin:auto;" align="center">
	<div id="menu">

		<ul id="item1">
			<li class="top" style="padding:6px;">Take Action &nbsp;<img border="0" align="absmiddle" src="img/drop-down-arrow.gif" /></li>
			<div align="left"><li class="item"><a href="#">My ECO Footprint</a></li></div>
			<div align="left"><li class="item"><a href="#">Reduce My Impact</a></li></div>
			<div align="left"><li class="item"><a href="#">Play Green Challenge</a></li></div>
			<div align="left"><li class="item"><a href="index.php?page=invite" style="outline:none;">Spread The Word</a></li></div>
			<div align="left"><li class="item"><a href="#">Green Ideas</a></li></div>
			<div align="left"><li class="item"><a href="#">ECO Products</a></li></div>
		</ul>

		<ul id="item2">
			<li class="top" style="padding:6px;">My Green World &nbsp;<img border="0" align="absmiddle" src="img/drop-down-arrow.gif" /></li>
			<div align="left"><li class="item"><a href="#">Check My World</a></li></div>
			<div align="left"><li class="item"><a href="#">Visit Other Worlds</a></li></div>
			<div align="left"><li class="item"><a href="#">Green Rankings</a></li></div>
			<div align="left"><li class="item"><a href="#">My Green Badge</a></li></div>
		</ul>

		<ul id="item3">
			<li class="top" style="padding:6px;">The Stores &nbsp;<img border="0" align="absmiddle" src="img/drop-down-arrow.gif" /></li>
			<div align="left"><li class="item"><a href="#">Green My World</a></li></div>
			<div align="left"><li class="item"><a href="#">Send Green Gifts</a></li></div>
			<div align="left"><li class="item"><a href="#">Get Green Coins</a></li></div>
			<div align="left"><li class="item"><a href="#">Get Carbon Reductions</a></li></div>
		</ul>

		<ul style="background:#edeff4; width:113px; cursor:default;">
			<li style="padding:6px;background:#edeff4;">&nbsp;</li>
		</ul>

		<ul id="item4">
			<div align="right"><li class="top" style="padding:6px;">About &nbsp;<img border="0" align="absmiddle" src="img/drop-down-arrow.gif" /></li></div>
			<div align="left"><li class="item"><a href="#">Global Warming?</a></li></div>
			<div align="left"><li class="item"><a href="#">About Greenbook</a></li></div>
			<div align="left"><li class="item"><a href="#">Contact Us</a></li></div>
		</ul>
	</div>

See the demo here.