13.02.2013 Views

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

250<br />

Switching between CSS states with JavaScript<br />

In the days when JavaScript was vilified for the way it was used on the Web, in-line scripting, event handlers, and modification<br />

of style properties were simply how things were done. These ad hoc changes usually had something to do with modifying and<br />

defining absolutely everything on the fly—height, width, background color, and various other style properties.<br />

We have progressed significantly from those dark days and work hard to separate our HTML from our<br />

CSS and our JavaScript. Modern techniques strive to ensure that our pages work with JavaScript both on<br />

and off. This is referred to as “unobtrusive scripting” and is the standard you’ll find elsewhere in this book,<br />

as well as in other modern scripting books, such as Jeremy Keith’s book DOM Scripting (friends of ED,<br />

ISBN: 1-59059-533-5).<br />

In our current crop of web browsers, we have a much more predictable environment in which to work. This predictability<br />

provides us with the ability to change properties beyond height and width, and get creative with things like switching<br />

between predefined states that include CSS positioning. With all the changes to CSS that are needed stored in a series of CSS<br />

rules, we can simply use some JavaScript to change the class of the appropriate containing element as required.<br />

For the purposes of our sliding navigation we have two core states:<br />

On initial page load, our navigational panes appear at the bottom of the page (see Figure 10-3).<br />

When a navigational item is activated (either with the keyboard or the mouse), the navigational pane moves to the<br />

top of the page (see Figure 10-4).<br />

We use JavaScript to change the class of the navigational pane to be “exposed,” which gives it the proper positioning at the<br />

top of the page just below the navigation. We create a style rule that positions any of the panes in the correct place (from<br />

the style sheet, wscslide.css):<br />

/* apply to any div with class="exposed"<br />

that is in the */<br />

div#drops div.exposed {<br />

position: absolute;<br />

top: 0;<br />

left: 0;<br />

padding: 0; /* added to remove doubled-up padding when positioned */<br />

}<br />

Note that this works because we are positioning these panes within the nearest relatively positioned parent,<br />

div#inner-wrap:<br />

div#inner-wrap {<br />

background-color: #ccc;<br />

color: #000;<br />

position: relative;<br />

margin: 0;<br />

padding: 0.5em 0;<br />

}<br />

We also use JavaScript to remove the class exposed to return the pane to the bottom of the page.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!