Preloading HTML Content with CSS - Code explanation to the rescue
(Page 6 of 6 )
The code seems a little complex, but really isn't at all. For the sake of clarity, let’s carefully examine what it does.
First, we defined three contextual selectors in the CSS, corresponding to the menu buttons that will compose the navigation bar. They are absolute positioned (you can change this to fit your own needs), and widths are set properly to 15 percent. So, our navigation bar consists of “button0,” “button1,” and “button2” respectively.
In the next step, we declared the three menus corresponding to each button, similarly defined as we did before: “menu1,” “menu2,” and “menu3." Again, these menus have widths of 14 percent and are absolute positioned.
Once we have defined the main containers for the menu, we need to declare the proper classes to specify the look of buttons and menus. In fact, they are very simplistic. Declaring the “navbar” and “menu” classes in turn does this. Feel free to modify these setting to your own taste (backgrounds, colors, borders, etc.).
Then, we define our “workhorse” class, named “hidden,” which is used to hide the menu divs from view.
Finally, we’re going to dive into the HTML markup section. As we can see, we define the three containing divs for each button, including the links, properly styled.
<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>
Please note the ids attributes assigned to each <a> element. They’re relevant to making our menu work. With the use of JavaScript, we’ll use these ids to obtain the corresponding <div> menu element either for hiding or displaying it when the user clicks on the link. In a moment, more of this will be explained in the JavaScript section. Just keep reading.
The JavaScript code usually begins defining our global function, to be executed once the page is loaded.
Next, we declare the switchMenu() function, which takes care of hiding and displaying accordingly the menus for each button. Please, take a look at the following line:
menu=document.getElementById('menu'+this.id);
The code is executed each time the user clicks on the link. When it is clicked, the corresponding link id is passed to the switchMenu() function, obtaining the id for the <div> menu element, hiding or showing it alternatively. So, for each link clicked, the expression is evaluated in the following manner:
menu= document.getElementById('menu0'); // gets the <div> with id=’menu0’;
menu= document.getElementById('menu1'); // gets the <div> with id=’menu1’;
menu= document.getElementById('menu2'); // gets the <div> with id=’menu1’;
As you can see, we’re obtaining the proper <div> menus that match to each link. If the class name of the element is "hidden" (menu is hidden), then is set to "menu" (menu is displayed), and vice versa, achieving the desired menu effect.
Of course, there are many ways of doing this using the DOM, and certainly id names are not fully W3C compliant, but for the purposes of the example, this is more than enough. The rest of the code is pretty self-explanatory. The script iterates over all the <a> elements present in the document, building an array with those found with "navbar" as class name attribute, and assigning the onclick event handler to each one of them.
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;
}
}
Finally, the full code is executed when the page finishes loading:
window.onload=loadGlobalFunctions;
The complete code for the menu is listed here:
<html>
<head>
<title>DROP DOWN MENU EXAMPLE</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<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>
<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>
</head>
<body>
<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>
</body>
</html>
The example can be viewed here: Example3.
And that’s all. We now have a functional drop-down menu, which relies on the CSS display property to work seamlessly. Just imagine the possible additions to it, or even better, make your own version by improving the script and becoming a great coder yourself. The challenge is really fun!
Conclusion
Through this article, we have appreciated the power of preloading selected content within Web pages, by hiding it for further programmatic manipulation. As seen in the examples shown here, possibilities are endless to fully exploit this ability, expanding our capabilities even more for adding richness to websites and finding better ways of delivering content to end users. The combination of using the CSS display property and a little JavaScript rewards you with awesome power.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |