Quick Tip: How to Create a Theme-Switcher in 200 Seconds
videos

Quick Tip: How to Create a Theme-Switcher in 200 Seconds

Have you ever seen sites that offer some kind of “color-switcher” within the header section? Want to know how easy it is to replicate? I’ll show you in 200 seconds, using jQuery.


The Screencast

Granted, this is a very simple example. What more do you expect in 200 seconds! :) But, this can easily be extended to import new stylesheets, if you wish.

The Final jQuery

var colorOptions = 'black, blue, orange, red, green'.split(', '),
	colorDivs = [],
	colorsContainer = $('#colorsContainer');

for ( var i = 0, len = colorOptions.length; i < len; i++ ) {
	var div = $('
').css('background', colorOptions[i])[0]; colorDivs.push(div); } colorsContainer.append(colorDivs); $('#header').hover(function() { colorsContainer .fadeIn(200) .children('div') .hover(function() { $('h2').css('color', $(this).css('backgroundColor')); }); }, function() { colorsContainer.fadeOut(200); });

Conclusion

I had to zoom through this screencast, so feel free to discuss/ask questions in the comments! I hope you enjoyed it! Are you liking the “two-a-week” quick tips that all of the Tuts sites are now doing?

Add Comment

Discussion 63 Comments

  1. Fredrik says:

    I really like these quicktips. Not that the actual tip is useful ;) but there is always something small that you will find.

    Thanks

  2. This tip can be taken and used in a myriad of other applications – it doesn’t have to be limited to what it currently is. Imagination is key here; learn the lesson and adapt it to your needs.

    I love these tips Jeffrey, thanks once again!

  3. Vadim says:

    It can be very useful and can generate new ideas of designing sites.

  4. Yheng says:

    Wow nice quick tips there Jeffrey! I never thought this could be done in 200 sec. now it does.

  5. piyanistll says:

    very nice post thanks for tutorial jeffrey

  6. Neat tip!

    And yes, keep up the quick tips; it’s a great idea!

  7. WP Tricks says:

    I need to tried this trick. jQuery is my favorite tools so far ;)

  8. Wez Pyke says:

    I love these quick tips, always something useful in there.

  9. lickynee says:

    These quick tips are my favorite kind of posts on Nettuts Jeffrey! Keep it up :D

  10. Alex Wolfe says:

    Nice 1, quick tips are great.

  11. good tutorial. thank for share

  12. I have been wondering how this was done for quite some time now, this is great too because i’m trying to learn more JQuery.

    Great Tip Jeffery

  13. Eugene says:

    Jeffrey, such tips are just great. Keep em coming…:D

  14. Now if you could just set/check a cookie so that you could maintain state in regards to the color choice, I think it would add a lot of substance to the tutorial. Good job for 200 seconds. I’d be happy if I could do it in 400!

  15. Amazing and quick one! Thank you Jeffrey!

  16. Micko says:

    Nice tip.
    What happened to js from null ch4, I’m sure you said you’re gonna release it last week.

  17. begs says:

    Very nice job!

    But i wonder about:
    for ( var i = 0, len = colorOptions.length; i < len; i++ ) {

    why do you use "len = colorOptions.length" it's not used lter in the code, so why not easily and short:

    for ( var i = 0; i < colorOptions.length; i++ ) {

    • Jeffrey Way says:
      Author

      It’s not a huge deal in this case. But we cache the length in a variable to prevent the JavaScript from having to determine the length of our array every iteration. This is a best practice.

      • Micko says:

        So it’s faster…

      • Shane says:

        Yeah, this is a best practice when using for loops in any language. You can do the assignment to the length variable (I’d say assignment, rather than cache in this case) at the top of the loop as Jeffrey has written it, or just before the for loop as a single statement.

        var len = colorOptions.length;

  18. Rolfje says:

    Thanks! This is awsone, was looking for sonething like this.

  19. leo rapirap says:

    I find it kinda funny when Jeffrey says “i just don’t have much time at all” but still manages to produce a very clear/concise and informative screencast.

    thanks for the time Jeffrey!

  20. it’ll be better if you using with jquery cookie plugin to store theme setting

  21. Aashish says:

    GR8 jeff,

    If you don’t mind which screencast software you are using???, I also want to create one for nettuts

  22. Alexander says:

    Great speed in the tutorial Jeff. I think I will implement this as a font size-changer on one of my sites!

    I was just wondering how you could reference and change a stylesheet in the javascript?

  23. DeannRie says:

    Спасибо!!!

  24. thanks
    iam using pure javascript to i can do that
    but with jquery
    of course this will be butifull

  25. Great tool to add that much more user interaction.

  26. Davidmoreen says:

    That is awesome, a really unique feature implmented in 3 minutes!

  27. Cedric says:

    This isn’t what I consider a theme switcher. It’s a basic color switcher for single elements. A theme switcher should switch a whole style and not only a few colors on a page. Anyway nice tutorial, thanks!

  28. Bobbi says:

    This is very similar to code I have been working (read: failing) on, but I want the div background color to change when refreshed or loaded. All the examples I keep finding have it based on clicking or hovering.

    Can anyone help?

  29. Julius says:

    This is barely easy!

  30. Cory Mathews says:

    This is not a theme switcher. This is just a color switcher. A Theme switcher would be changing the css file that is the primary.

  31. Ed Baxter says:

    Great little quick technique, thanks for sharing Jeff!

  32. Cony Shown says:

    so you created an article to just show $(‘h2′).css(‘color’, $(this).css(‘backgroundColor’)); one line of code?

    i was expecting something serous ..

  33. Evan-XG says:

    nice stuff , and well done .
    there is something nice to easly add though, by storring the theme in cookie as a setting for the visitor .

  34. Woooow !

    So cool !

    Thanks !

  35. Stan says:

    Can you use this also with background-images?

  36. v-mann says:

    hmmm great Tut, but the Demo doesn’t Work in IE8 for me :-/
    FF und Crome do it just fine… grrr…

  37. Jack says:

    Quicktips, I think this is the start of a beautiful friendship.

  38. Simon says:

    Thank you for this great idea Jeffrey! Will use it on my items :)

  39. Jared says:

    One of the best tuts. Love it, thanks.

  40. Maxi says:

    can make the images mean to be an image in many colors. eg a car

  41. kate says:

    thanks again!

  42. DaveC says:

    Thanks Jeffrey. I didn’t know you could do $(el).append( [el1, el2] )

    Unfortunately it doesn’t work on IE8 for me either.

    I might be looking into this waaay too deeply, but I keep hearing about accessibility stuff and everything being usable via the keyboard. Would there be an easy way to achieve this here?

    Would it be more efficient (and perhaps readable) to assign the event handler to the divs in #colorContainer in a seperate block? I.e.:

    //other code as before

    $(‘#header’).hover(function() {
    colorsContainer.fadeIn(200);
    }, function() {
    colorsContainer.fadeOut(200);
    });

    colorsContainer
    .children(‘div’)
    .hover(function() {
    $(‘h2′).css(‘color’, $(this).css(‘backgroundColor’));
    });

    Just realised this makes it work in IE8 too!

  43. Vincent says:

    Thanks for the great tut. I have a question though.

    For colorOptions, what’s wrong with doing it the regular javascript array way?

    eg:

    var colorOptions = ['black','blue','orange','red','green'];

  44. r6media says:

    Thanks for this fantastic tutorial!

  45. Farzad says:

    Hi and thanks for this useful tutorial, but there is a problem with this!

    Basically when you select a colour that’s fine, even when hovering over the colours the page text colours changes instantly, but when you attempt to refresh the page the colour goes back to its default colour “black” and that’s what I’m having problem with!

    I think the colours should be stored in somewhere, so when refreshing the page or even jumping from one to another page the select colour would stay the same!

    So could somebody help with this please!
    Thanks in advance :)

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.