Grow your CSS skills. Land your dream job.

Fade-in Spoiler Revealer

Published by Chris Coyier

spoilerrevealer.jpg

jQuery has some really simple built in features for "fading in" and "fading out" different page elements. I thought we could exploit some of those functions for a really simple Spoiler Revealer.

View DemoDownload Files

Between some smart CSS and jQuery, we can keep our markup really clean. Take a look at the usage here:

<p>In the movie Citizen Kane, Charles Foster Kane's last word "Rosebud" 
turns out to <span class="spoiler">be a sled.</span></p>

That's right, just "spoiler" in a span with the class of spoiler. The jQuery will find and hide all of this text, and replace it with a "reveal spoiler" button. Upon clicking that button, the button fades away and is replaced with the text from inside the span. Check out the code:

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() { 
	
	$("span.spoiler").hide();
	
	 $('<a class="reveal">Reveal Spoiler &gt;&gt;</a> ').insertBefore('.spoiler');

	$("a.reveal").click(function(){
		$(this).parents("p").children("span.spoiler").fadeIn(2500);
		$(this).parents("p").children("a.reveal").fadeOut(600);
	});

}); 
</script>

Super simple. Just a quick little example to show how nice and easy jQuery is to work with and how it can compliment and extend what you are already doing with CSS!

Comments

  1. Permalink to comment#

    Awesome tidbit!

    Now… how long before we see “jquery-tricks.com?” LOL!

  2. David
    Permalink to comment#

    Thanks Chris, that’s what I needed!

  3. Nice idea Chris! Thanks for spoiling The Village for me though! Actually, I thought The Village was pretty good.

  4. Romz
    Permalink to comment#

    $(this).parents(“p”).children(“span.spoiler”).fadeIn(2500);
    could be easily replaced with

    $(this).next().fadeIn(2500)

    Have fun with jQuery

  5. Permalink to comment#

    Now… how long before we see “jquery-tricks.com?” LOL!

    i thought the same thing :p

  6. Thanks

  7. This is an interesting trick, but to absolutely avoid spoiling everyone, it should probably follow principles of progressive enhancement:

    To keep from spoiling people with no JavaScript support, including people reading the content in a feed reader or on a mobile device, you should prepend many, many br tags before the spoiler, as well as a message saying to scroll down to read the spoiler. You could then use your jQuery code to render the br tags and the “scroll to read” message invisible. Your function to render the spoiler itself visible would leave the br tags and the message invisible. The result would be a spoiler alert that works well for everyone, regardless of user-agent capabilities.

  8. I decided to mock up and blog about the enhancement I suggested in comment 7 above. Here’s the link:

    http://blogs.pathf.com/agileajax/2008/04/jquery-fade-in.html

  9. I tried to do this..
    But I cant!
    I used the:
    script type=”text/javascript” src=”MY OWN URL I UPLOADED THE .JS”> /script>

    $(document).ready(function() {

    $(“span.spoiler”).hide();

    $(‘Reveal Spoiler >> ‘).insertBefore(‘.spoiler’);

    $(“a.reveal”).click(function(){
    $(this).parents(“p”).children(“span.spoiler”).fadeIn(2500);
    $(this).parents(“p”).children(“a.reveal”).fadeOut(600);
    });

    });
    /script>

    And in the text..
    p>In the movie Citizen Kane, Charles Foster Kane’s last word “Rosebud”
    turns out to span class=”spoiler”>be a sled. /span> /p>

    And it doesnt makes the spoiler.. dont know why.. :S
    I had to take out the > other ones b/c it erases that part..
    any help? using blogger… :S

  10. Can you put up an example page where you are getting the problem so we can take a look at it? Seems like you are doing it right…

  11. hey great trick there, really makes me feel to implement in my website.
    Skillsheaven

    Thanks.

  12. Robert
    Permalink to comment#

    Any idea on how to make the revealed text a link?

    In other words, in your example above if you wanted to link the revealed answer to an imdb.com page, how could you do it without making the “reveal” text a link?

  13. joelmoney
    Permalink to comment#

    Chris, I think to validate you need to escape the closing a tag.

    Reveal Spoiler &gt;&gt;</a>

    should be:

    Reveal Spoiler &gt;&gt;<\/a>

    That validates and seems to work without problems.

This comment thread is closed. If you have important information to share, you can always contact me.

*May or may not contain any actual "CSS" or "Tricks".