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

Like this Post? Tell Your Pals!
Get More Great Tips

Comments

  1. The only problem with your hack is that it loads the images on demand - so won’t be as snappy as it could if you pre-loaded them (but then that would require some code up-front to pre-load them, which kind of kills it being a quick hack :) )

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…

  10. much easier to enlist the services of…

    a professional promotions and advertising service.the benefits of professional promotions helpthere are a large number of reasons why entrepreneurs, organization leaders, or business owners would opt to choose a professional promotions company as oppos…

  11. want to have the distinction of being…

    tagged with the great honor of having the best blog design? not only does it become a great source for immense pride but it also will catapult your blog site to high page ranks and open up a lot of channels…

  12. Links says:

    you want to harvest some of their…

    time, you need to be thinking here. indeed i hope you will consider all this, and think on it. there is an extensive variety of avenues to make passive income on the internet. many people have started by making a small…

  13. better are: start your blog with the…

    keyword or question that you are trying to answer. if you are promoting a product or idea you need to say that at the beginning. it is imperative that you are clear from the start because you have to grab their…

  14. writing, then learn to like it first….

    forcing yourself to do something you do not like would not give you good outputs. when you force yourself t write, it will radiate into your writing, your readers will feel and notice it, and most probably, you will not be…

  15. available on the web, the range of…

    prices needs to be checked and compared to find out the best deal among them. after all; a provider that bleeds you financially white is never the best choice. lets go a couple of years into the past. back when i…

  16. visit us says:

    confidence in your ability to produce the…

    type of content that is demanded.learn these seo writing tipssearch engine optimization is a vast subject, and one that is not limited to writing, but you can get started with these few tips. first, you need to understand how to use…

  17. link building outreach campaigns.the main difference between…

    social media link building and traditional link building is the focus. in traditional link building, you are focusing on anchor texts in your backlinks from specific websites. when you are using social networking, you are focusing on the person behind …

  18. financial experts would suggest you not to…

    put any daily living expenses on your credit for many reasons. typically, this habit becomes problematic for people that tend to under budget and overspend. but when you are trying to re-establish good credit, making a large purchase is never a…

  19. that their students are poor writers. freshmen…

    entering college have the worst time in their writing classes, especially, but also in many others courses. at some schools, over half of new freshman cannot get though their english composition courses seamlessly enough. it seems clear that these new …

  20. berkins bag says:

    welcome to check out what you know…

    and what you have to offer. because you own it, it can never be shutdown, you don’t have to conform to any third party guidelines and rules so you never run the risk of waking up in the morning and finding…

  21. click here says:

    your blog. you can get a blog…

    within the next 10 minutes by going to a site called “blogger”, and get your very own site up and running in no time at all. once you’ve created your blog, it’s up to you to start promoting it everywhere on…

  22. tablet says:

    some of your materials are sourced. or…

    how you’ve been recycling since 1967. or a string of ideas for creative and unusual ways to make use of your products.product literature, sales brochures, industry news, quarterly and annual reports… these can all be sources of potentially good mate…

  23. Macbook says:

    about it somewhere online that will help…

    you out.blogging is awesome, and once you finally understand what to do, you will find that it’s easy as pie to start a blog and start earning money from it. there are a lot of books on blogging, and most of…

  24. being unpredictable. being unpredictable helps you to…

    remain in control while giving you an opportunity to exceed the expectations of your Titanic audience. furthermore, you will gain the respect required to achieve a more superior position and status. being smart helps you to become famous without having…

  25. sites, read your titles and summaries and…

    then click through to your blog. so not only are you getting the word out there, getting some good seo benefit, you’re also getting real readers to your blog, and you’re using a tool provided by your blog platform for free,…

  26. Chanel says:

    Visitor recommendations…

    [...]one of our visitors recently recommended the following website[...]……

  27. side effects of low vitamin d…

    I should say also believe that mesothelioma is a uncommon form of cancers that is usually found in all those previously subjected to asbestos. Cancerous cells form from the mesothelium, which is a safety lining that covers the majority of the body’s b…