Preloading HTML Content with CSS - Building a simple drop-down menu with CSS
(Page 5 of 6 )
So far, we‘ve shown a couple of examples to clearly demonstrate how powerful and handy this hiding technique can be for generating on demand content. It would be good to expand out those capabilities to build up different applications, exposing the whole gamut of possibilities when using CSS to hide Web page elements. So, let’s start increasing our creativity by creating a simple drop-down menu, which is very suitable for our sample needs.
The basic logic to creating the drop-down menu consists of hiding and displaying selected <div> elements using the CSS display property, accordingly to the occurrence of an event -- generally, when the user clicks on a specific area or passes the mouse over it. Having defined the general concept, all we have to do is create our menu.
As usual, here is the code for our JavaScript functions:
<script language="javascript">
// loads global functions
loadGlobalFunctions=function(){
// switches on-off menu display
switchMenu=function(){
menu=document.getElementById('menu'+this.id);
if(menu.className=='hidden'){
menu.className='menu';
}
else{
menu.className='hidden';
}
}
// gets all <a> elements
as=document.getElementsByTagName('a');
navAs=[];
// makes array from <a> elements with class name 'navbar'
for(i=0;i<as.length;i++){
if(/\bnavbar\b/.test(as[i].className)){
navAs[navAs.length]=as[i];
// assigns onclick event handler to navbar <a> elements
navAs[i].onclick=switchMenu;
}
}
}
// executes code once page is loaded
window.onload=loadGlobalFunctions;
</script>
The CSS rules are the following:
<style type="text/css">
#button0 {
position: absolute;
width: 15%;
top: 10px;
left: 0px;
}
#button1 {
position: absolute;
width: 15%;
top: 10px;
left: 15%;
}
#button2 {
position: absolute;
width: 15%;
top: 10px;
left: 30%;
}
#menu0 {
position: absolute;
width: 14%;
top: 30px;
left: 0px;
}
#menu1 {
position: absolute;
width: 14%;
top: 30px;
left: 15%;
}
#menu2 {
position: absolute;
width: 14%;
top: 30px;
left: 30%;
}
.hidden {
display: none;
}
a.navbar:link,a.navbar:visited {
display: block;
background: #0f0;
font: bold 11px "Verdana", Arial, Helvetica, sans-serif;
color: #000;
padding: 2px;
text-align: center;
text-decoration: none;
border: 1px solid #000;
}
a.navbar:hover {
text-decoration: underline;
}
.menu {
display: block;
background: #ffc;
font: normal 11px "Verdana", Arial, Helvetica, sans-serif;
color: #000;
padding: 4px;
border: 1px solid #000;
}
</style>
Finally, the HTML markup is listed below:
<div id="button0"><a href="#" class="navbar" id="0">Company</a></div>
<div id="button1"><a href="#" class="navbar" id="1">Products</a></div>
<div id="button2"><a href="#" class="navbar" id="2">Contact</a></div>
<div class="hidden" id="menu0">
Profile<br />
Staff
</div>
<div class="hidden" id="menu1">
Web Compilers<br />
HTML Editors<br />
PHP Editors<br />
Database Software
</div>
<div class="hidden" id="menu2">
Technical Support<br />
Knowledge Base<br />
Administrative Support
</div>
In order to explain the logic behind this code, we’ll see in detail how it works, since this is the subject of the next section.
Next: Code explanation to the rescue >>
More HTML Articles
More By Alejandro Gervasio