Tech Note: Flippy Triangle Style

Charles Johnsonfollow me on twitter
Fri Jun 27, 2008 at 5:50 pm PDT • Views: 509

You may have noticed that I've organized the stuff in our left sidebar with a series of "accordion" menus; some call this style the Flippy Triangle, since a little triangle indicates whether the menu is currently expanded or not, by ... flipping, believe it or not.

Here's the technique I'm using to create those Javascript-driven menus; you might be surprised by how little code it takes, with the assistance of the excellent Javascript library jQuery. I'll assume you already have jQuery installed and get right into the code.

The HTML for the menus is very simple: a paragraph containing a link at the top (the "trigger" for the menu), and a DIV containing a series of links to the items in the menu. In the CSS file, the class "slider" is set to "display: none" so that the DIVs underneath the triggers are hidden by default. Here's a simplified version of the HTML:

Now for the jQuery magic. Most jQuery-related applications use the $(document).ready function to install Javascript event handlers, and that's how we attach the Flippy Triangle code to the triggers, to show or hide their associated content.

And that, my coding friends, is that. Really. That's the whole thing, except for some details about styling the various elements with CSS (colors, spacing, etc.).

Without going through it line by line, that little bit of code first finds all A tags with a class of "flippy" and attaches an anonymous function to their click events. The click handler gets the text of the trigger (e.g., "&#x25BA LGF Hits"), then does a quick DOM traversal to find the next DIV with a class of "slider." If that DIV is hidden, it's displayed with a "slideDown" effect; if it's visible, it slides up and disappears. Before hiding/showing the DIV, the flippy triangle character is flipped. Finally, the click handler returns a value of FALSE, so the trigger link doesn't do anything embarrassing or annoying.

One little gotcha is that the character entities '►' and '▼' (which look like ► and ▼) are treated by Javascript as one character, not eight. That's why the code that gets the trigger text uses "substr(1)" instead of "substr(8)".

jQuery is the cat's pajamas.

Advertisement

729 comments

^ back to top ^

Name:

Pass:

Register Forgot Your Password? Account Settings Re-send Confirmation (To log in, cookies must be enabled in your browser!)

Turn off ads by subscribing!
For about 33 cents a day, our subscription option turns off all advertisements at LGF!
Read more...


► LGF Headlines

  • Loading...

► Tweeted Articles

  • Loading...

► Tweeted Pages

  • Loading...

► Top 10 Comments

  • Loading...

► Bottom Comments

  • Loading...

► Recent Comments

  • Loading...

► Tools/Info

► LGF Hits

► Resources

► Never Forget

► Statistics

► Tag Cloud

► Contact

You must have Javascript enabled to use the contact form.
Your email:

Subject:

Message:


Messages may be published in our weblog, unless you request otherwise.
Tech Note:
Using the Contact Form

More Partners

Compare Electricity Prices in your area. Texas Electricity is deregulated; you have the right to choose Texas Electric Rates from among many Texas Electric Companies.

Don't crush that dwarf, hand me the pliers.

TwitterFacebook
LGF Pages
Recent Pages

Daniel Ballard
Mayor Antonio Villaraigosa Screws Over L.A. With $100 Million Credit Bubble That Could Burst Once He Leaves Office - Los Angeles
40 minutes ago
Views: 37 • Comments: 0
Tweets: 0 • Rating: 0

Daniel Ballard
The All-About-Me Mayor: Antonio Villaraigosa's Frenetic Self-Promotion - Page 1 - News - Los Angeles
41 minutes ago
Views: 33 • Comments: 0
Tweets: 0 • Rating: 0

Aigle
Jodi Rudoren Enters the Scene: Tweets or Tea Leaves?
46 minutes ago
Views: 14 • Comments: 0
Tweets: 0 • Rating: 0

researchok
US 'Very Closely' Watching Syrian Chemical Weapons  | ajc.com
55 minutes ago
Views: 35 • Comments: 0
Tweets: 0 • Rating: 0

Channeling Confucius
Susan G. Komen for the Cure's Limited Impact
1 hour, 6 minutes ago
Views: 39 • Comments: 0
Tweets: 0 • Rating: 0

Bob Dillon
End of an Era: NASA Shuts Down Its Last Mainframe
1 hour, 24 minutes ago
Views: 45 • Comments: 1
Tweets: 0 • Rating: 2

Daniel Ballard
Villaraigosa Will Chair Democratic National Convention
1 hour, 35 minutes ago
Views: 48 • Comments: 1
Tweets: 0 • Rating: 2

Bob Dillon
Chemist Applies Google Software to Molecules
1 hour, 52 minutes ago
Views: 43 • Comments: 0
Tweets: 0 • Rating: 2

Daniel Ballard
Meme: What Photographers Actually Do
2 hours, 7 minutes ago
Views: 60 • Comments: 1
Tweets: 0 • Rating: 2

Aigle
New Online Newspaper in Israel
2 hours, 26 minutes ago
Views: 66 • Comments: 0
Tweets: 0 • Rating: 0

 Frank says:

Sopranos!? That's why God made the rocket launcher and grenade!