Home arrow JavaScript arrow Page 4 - Creating a Scrolling Navigation Bar Effect with CSS and JavaScript
JAVASCRIPT

Creating a Scrolling Navigation Bar Effect with CSS and JavaScript


Welcome to the third part of a four-part series that shows you how to build collapsible navigation bars with CSS and JavaScript. In this part, you'll learn how to combine the functionality of the Prototype and Scriptaculous libraries to construct a pretty useful scrolling navigational bar.

Author Info:
By: Alejandro Gervasio
Rating: 3 stars3 stars3 stars3 stars3 stars / 2
October 05, 2009
TABLE OF CONTENTS:
  1. · Creating a Scrolling Navigation Bar Effect with CSS and JavaScript
  2. · Building the dynamic navigation bar
  3. · Adding a scrolling effect with the Scriptaculous DHTML framework
  4. · Finishing the dynamic navigation bar

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Creating a Scrolling Navigation Bar Effect with CSS and JavaScript - Finishing the dynamic navigation bar
(Page 4 of 4 )

Undoubtedly, the best way to see how the different parts that make up this dynamic bar work as expected is to include them all in one single (X)HTML file. So, below I included the definition of this file, to let you examine it in detail:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Example on building an dynamic navigation bar</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff;

}

h2{

margin: 0;

font: bold 18px Arial, Helvetica, sans-serif;

color: #000;

}

p{

font: normal 12px Arial, Helvetica, sans-serif;

color: #000;

}

#navbar{

padding: 10px;

background: #ffc;

}

#navbar ul{

list-style: none;

}

#navbar li{

display: inline;

padding-right: 4%;

}

#navbar a:link,#navbar a:visited{

font: normal 12px Arial, Helvetica, sans-serif;

color: #039;

text-decoration: none;

}

#navbar a:hover{

text-decoration: underline;

}

#maincol{

position: relative;

padding: 30px 10px 30px 10px;

background: #eee;

}

#footer{

padding: 10px;

background: #ffc;

}

#switcher{

position: absolute;

top: 0;

left: 0;

width: 150px;

padding: 2px;

background: #999;

border: 1px solid #000;

text-align: center;

font: bold 11px Arial, Helvetica, sans-serif;

color: #fff;

cursor: default;

}

</style>

<script language="javascript" src="scriptaculous/js/prototype.js"></script>

<script language="javascript" src="scriptaculous/js/scriptaculous.js"></script>

<script language="javascript">

// display/hide navigation bar using the 'Slide' effect

function toggleNavBar(){

new Effect.toggle('navbar','slide');

}

// initialize navigation bar switcher

function initializeElement(){

Event.observe($('switcher'),'click',toggleNavBar,false);

}

Event.observe(window,'load',initializeElement,false);

</script>

</head>

<body>

<div id="navbar">

<h2>This is the navigation bar of the web page</h2>

<ul>

<li><a href="#">Link 1</a></li>

<li><a href="#">Link 2</a></li>

<li><a href="#">Link 3</a></li>

<li><a href="#">Link 4</a></li>

<li><a href="#">Link 5</a></li>

<li><a href="#">Link 6</a></li>

</ul>

</div>

<div id="maincol">

<div id="switcher">Turn on/off navbar</div>

<h2>This is the center column of the web page</h2>

<p>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien </p>

<p>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien </p>

<p>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien </p>

</div>

<div id="footer">

<h2>This is the footer section of the web page</h2>

<p>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien </p>

<p>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien </p>

<p>

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas enim. Nulla facilisi. Vestibulum accumsan augue vulputate justo. Fusce faucibus. Sed blandit, neque sed lacinia nonummy, diam quam imperdiet justo, at dictum augue nunc a neque. Sed urna lacus, tincidunt at, aliquam id, fringilla id, felis. Vivamus feugiat molestie quam. Sed id dolor. Sed ac purus id sapien </p>

</div>

</body>

</html>

As shown above, I coded one (X)HTML file that includes the structural markup of the navigational bar, along with its CSS styles and the JavaScript code required to make it work.

Of course, in this case I can’t directly provide you with a functional example that shows you how the navigation bar scrolls up and down across the web page each time the switcher is clicked on, since it uses the source files of Prototype and Scriptaculous together. The following images, however, should give you some idea of this process is accomplished:





As always, feel free to use all of the code samples included in this tutorial. You can learn more about how it works by introducing your own modifications to this scrolling navigational bar. It’ll be an educational experience, believe me!

Final thoughts

In this third episode of the series, you learned how to combine the functionality of the Prototype and Scriptaculous libraries to construct a pretty useful scrolling navigational bar.

In the final article, I’m going to polish the scrolling effect applied to the previous navigation bar by using another animation that comes bundled with the Scriptaculous framework. Don’t miss it!


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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials