A Crazy Simple jQuery Hack for Creating Rollovers


Mouse Cursor

This is one of my favorite “quick-n-dirty” jQuery tricks. If you’re working on a Web site and you need to add rollover effects to links or other images, you can do it pretty simply with this chunk of JavaScript code and some intelligently named image files. First, make sure you include jQuery if you haven’t already:

http://gist.github.com/456725

Now, include this jQuery code somewhere on your site:

http://gist.github.com/455216

Here’s how it works: each image you have that needs to do something when you mouse over it should have two states, “up” (mouse is off of the image) and “over” (mouse is over the image). Each of these stators will be represented by a specific image, i.e., “foo_up.jpg” and “foo_over.jpg”. So, all you need to do is name each of your following that pattern and add a single CSS class to the image itself within your markup:

http://gist.github.com/456727

And you rollover effects will work automatically. Easy as pie.

Clearly, you can change the naming convention to your liking if my implementation doesn’t suit you, but you’ll need to adjust the JavaScript as well.

The principle at work here is one I recall from The Art of Unix Programming by Eric S. Raymond:

Fold knowledge into data so program logic can be stupid and robust.

This approach is perfect for situations like this because you can tell almost immediately if there’s something wrong with your setup: either the image won’t load or the rollover effect doesn’t work. Either way, it’s pretty damn simple to find out where the bump in the road is and fix it.

I know this is a little inside baseball, but I’ve seen countless web pages where this same effect was achieved by doing this kind of crap:

http://gist.github.com/456812

I find that to be a little gross (and extremely obtrusive in terms of markup).

The swapping technique listed above is something I came up with when I did something that I think everybody (myself more than anybody) should do a little more often – think about the problem you’re trying to solve. Once you’ve done that, think about how you might avoid having to solve the same problem again in the future. Not that there’s a whole lot of genius wrapped up in my 10 lines of JavaScript, but I’ve used it at least 20 times in the last year and it’s served me very well.

What parts of your workflow could benefit from a little bit of extra consideration? 

Photo by Darren Hester

Trackbacks

  1. go. you could create a sign-up page…

    that contains basic instructions on using the email sign-up form on the right, along with pointing out the sharing buttons and links. this page would be basically for people who are new to blogging. most experienced users will have already spotted…

  2. your time and remember that patience is…

    key to writing a good articles. write when you have free time, maybe when you come home from work or are felling well enough to write an article usually there is a time of day when you just feel like writing…

  3. click here says:

    of plugins as much as most plugins…

    are free, this does not mean you can use all. excessive use of unnecessary plugins can result to slow loading of your page and this can really make visitors leave your site immediately.make use of a few plugins that are vital…

  4. for profit is a great way to…

    bring in extra cash. it has a lot of potential and the amount of cash you can earn from blogging will vary greatly. it depends on how much time and effort you put into it. one reason that blogging for profit…

  5. expert, you have to sound like one….

    write smart! it is perfectly acceptable to use an informal style in your article writing. this has become the norm for articles that are not highly technical. provide your readers with a good experience.#3 keep it simplefinally, keep your articles shor…

  6. click here says:

    a good option that brings in some…

    extra cash. sell advertising space- if you get a lot of traffic to your site you have the potential to earn cash by selling space to advertisers. you will need to have a good study flow of traffic to your site…

  7. my website says:

    your customers is by blogging. and for…

    this reason many businesses whether small or big have recognized the powerful impact a blog can have on business achievement, which is important. but one major challenge is that their blogs are going nowhere which gets them discouraged at the lack…

  8. of information to enhance the reader in…

    some way. if you are there to help others, they will be more likely to do business with you. when your only motivation is money, it will be difficult to write quality material.5.you don’t always have to be an expert. staying…

  9. your competition isn’t worried about you. so,…

    find the keywords that your target audience would use to do a search on google. then, make sure to include them throughout your blogs content. be humble with the amount of keywords you use. include them in the title, blog post…