HTML
  Home arrow HTML arrow Page 6 - Preloading HTML Content with CSS
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
HTML

Preloading HTML Content with CSS
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2005-01-12

    Table of Contents:
  • Preloading HTML Content with CSS
  • Rotating HTML content with JavaScript
  • Hiding HTML content with CSS
  • Building a HTML content rotator with CSS
  • Building a simple drop-down menu with CSS
  • Code explanation to the rescue

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · But two things should be considered:1) Don't hide things with CSS and show them...
       · I agree with you about your points 1-2. The subject of this article was showing an...
       · Well, that a lot of sites fail is not a reason to keep it that way. In fact,...
       · Hi again,You're correct about that. I'm on the side of doing web development the...
     

    HTML ARTICLES

    - Tabular Database Forms with HTML
    - Using the Find Functions for HTML Database F...
    - Sorting for Database Forms with HTML
    - Edit and Other Database Form Functions with ...
    - More Database Form Functions with HTML
    - Database Form Functions with HTML
    - Using the HTML Table Element as a Recordset
    - Building Single Row Database Forms with HTML
    - Introduction to Database Forms with HTML
    - Another Look at Animation of Geographical Ma...
    - Animation of Geographical Map Regions
    - Changing and Moving Pictures with CSS
    - Clickable Geographical Map Regions
    - Gradient Creation with the HR Element
    - Text on HTML Images: Do it Yourself







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway