11

Posted by Melissa Penta on January 25, 2010

Simple jQuery Content Reveal Script

After developing a few websites that have content reveal and accordion scripts, I decided to take what I learned and write my own simple, very customizable content reveal script. Since the web has helped me so much with learning about jQuery, I wanted to share my code.

This script helps creates clean layouts where there is a lot of content. To see the final result, take a look at the demo at the bottom of this post.

The Javascript

First, download the jQuery library (I recommend the minified version since it is lightweight). Link the library to your page using the following code:

<script src="path-to-directory/jquery-1.4.min.js" type="text/javascript"></script>
  • Be sure to change the path to the directory that you keep your jQuery library in.

Paste this code between the <head></head> tags; if you paste it into a seperate javascript file, remove the <script> opening and closing tags:

<script type="text/javascript">
$(document).ready(function() {
	//expand all or hide all options
	$('#tools a.expand').click(function() { $('#reveal .content').slideDown('slow'); $('#reveal .header a').addClass('active'); });
	$('#tools a.hide').click(function() { $('#reveal .content').slideUp('slow'); $('#reveal .header a').removeClass('active'); });
	//individual header click toggle
	$('#reveal .header a').click(function(){
		//collapse and remove active class, if the div is opened it will close
		var activeHeader = $(this).hasClass('active');
		$(this).removeClass('active').parent('div').next('.content').slideUp('slow');
		//open the div if it is closed and add the active class
		if(activeHeader==0)
		{ $(this).addClass('active').parent('div').next('.content').slideDown('slow'); }
	});
});
</script>

I color coded some of the classes that the script calls. If you want to change the class names be sure to also change the classes in both the HTML and CSS. I have the tags colored to match in the CSS and HTML code snippets. The code is also commented to explain what each part does.

The CSS

Paste this code between the <head></head> tags; if you paste it into a seperate css file, remove the <style> opening and closing tags:

<style type="text/css">
#reveal { color:#444; font-family:Arial, Helvetica, sans-serif; font-size:12px; width:300px; }
#reveal #tools { color:#39f; cursor:pointer; font-size:10px; margin-bottom:3px; text-align:right; }
#reveal .header { border:1px solid #999; cursor:pointer; margin-bottom:3px; -moz-border-radius:5px; -webkit-border-radius:5px; padding:3px 0; }
#reveal .header a { background:url(path-to-directory/arrow-inactive.png) no-repeat right; color:#00aef0; display:block; margin:0 10px; }
#reveal .header a.active { background:url(path-to-directory/arrow-active.png) no-repeat right; }
#reveal .content { display:none; padding:10px; }
</style>

Most of this code is styling. Important parts of the CSS are in red font:

  • cursor:pointer – Since there is no href tag, the links for Expand All, Hide All and the headers need the hand cursor
  • display:none – The content is initially hidden from view to keep the layout clean.
    Note: If you want the content to be revealed when the page loads, you can remove this property; however you will also want to add the active class to each header in the HTML otherwise the script will not work properly on the initial hit.
  • background:url() – You may want to have an indicator on your header that lets the user know that there is more content. A basic graphic for that is an arrow that points to the right while inactive and down while active. Be sure to change the path to the directory that holds your background image. If you would like to use the arrows from the example, you can download them here and here.

The HTML

Paste this code inbtween the <html></html> tags where you want the content reveal to show up:

<div id="reveal">

    <div id="tools">
        <a class="expand">Expand All +</a>
	<a class="hide">Hide All -</a>
    </div> 

    <div class="header"><a>
	Header One Toggle
    </a></div>
    <div class="content">
	Content under the first header
    </div>

    <div class="header"><a>
	Header Two Toggle
    </a></div>
    <div class="content">
	Content under the second header
    </div>

    <div class="header"><a>
	Header Three Toggle
    </a></div>
    <div class="content">
	Content under the third header
    </div>

</div>

To quickly explain what each part of the code is used for:

  • reveal id – a container div for the code
  • tools id – holds the Expand All and Hide All links
  • header class – the header link for each content div
  • content class – the content container (initially hidden on load of the page)

Demo

As long as all of the code is pasted in the correct areas of your page and you changed the directory paths to where the jQuery library and arrow images reside, then you should come up with the following result:

Donec accumsan laoreet tincidunt. Vestibulum semper dui et est pellentesque venenatis. Aliquam vel libero diam. Quisque elementum, mi sed rutrum dictum, tellus erat ullamcorper purus, eget gravida tortor metus ut nunc.

Donec accumsan laoreet tincidunt. Vestibulum semper dui et est pellentesque venenatis. Aliquam vel libero diam. Quisque elementum, mi sed rutrum dictum, tellus erat ullamcorper purus, eget gravida tortor metus ut nunc.
Donec accumsan laoreet tincidunt. Vestibulum semper dui et est pellentesque venenatis. Aliquam vel libero diam. Quisque elementum, mi sed rutrum dictum, tellus erat ullamcorper purus, eget gravida tortor metus ut nunc.

Topics: ,

Add a Comment



 

Showing 11 Comments

  1. Cakezula

    I can’t seem to get this to work. I’ve the paths correct and the script in the right places. It looks proper but when I click on the headers or expand/hide all nothing happens. Thoughts?

  2. Melissa Penta

    Edit: I think I know the problem. While putting the color code inside the script for the post, a quote disappeared from the post on this line:
    $(this).removeClass(‘active’).parent(‘div’).next(‘.content’).slideUp(‘slow’);

    The post has been updated :)

  3. Cakezula

    EXCELLENT! That was it. Thanks Melissa!

  4. Melissa Penta

    No problem!

    If it weren’t for your reply, I wouldn’t have caught my small error – so thank you too!

  5. Bianca

    Hey! Finally someone who can explain and display this easily! Excellent job, thanks so much for sharing the code. You’re a life-saver.

  6. Bede Constantinides

    Thanks for this Melissa, it’s just what I needed.

    Would you be kind enough to give me some guidance on making one of the items be open by default on page load?

    Many thanks
    Bede

  7. Melissa Penta

    Bede,
    There will be two things that you’d want to do to make an individual box opened by default. See the bolded parts below:

    <div class=”header”> <a class=”active”>
    Header One Toggle
    </a> </div>
    <div class=”content” style=”display:block”>
    Content under the first header
    </div>

  8. Bede

    Thanks Melissa, what a simple solution! Unfortunately it doesn’t seem to be working for me… here’s some of the code I’m trying.

    Make an Enquiry

    Name

    Email

    Telephone (optional)

    Enquiry

  9. Bede

    Ok, I’m trying again…

    <div id="reveal">
    <div class="header">
    <a class=”active”>Make an Enquiry</a>
    </div>
    <div class="content" style=”display:block”>
    <div class="contactForms">
    <form action="contact-handler.php?et=enquiry" method="post">
    <div class="">
    <label for="name">Name</label><br />
    <input type="text" name="name" id="name" /><br />
    <label for="email">Email</label><br />
    <input type="text" name="email" id="email" /><br />
    <label for="phone">Telephone <span>(optional)</span></label><br />
    <input type="text" name="phone" id="phone" /><br />
    <label for="enquiry">Enquiry</label><br />
    <textarea name="enquiry" id="enquiry" cols="15" rows="6"></textarea><br />
    <input type="submit" value="Submit">
    </div>
    </form>
    </div>
    </div>
    <div class="header">

  10. Saoron

    how can you make a manual close button for each slide?

  11. Barb

    HI ; Thanks for the great post, I am trying to do something similar but have 3 images across and when a user clicks on an image,(there is one text box below) so when as I said the user clicks an image the text changes to corresponds with the image. Can you help me with this?
    Thanks so much