JQuery Hover Fading on Mouse Over and Mouse Out Without Images

I've been playing with the excellent JQuery JavaScript library for a while now. Recently I came across a technique for fading in and out two images based on mouse over and mouse out; and I wanted to see if I could create a fade-in and fade-out hover effect without the images.

It's a testament to the robustness and flexibility of JQuery that I could do this in so little code, with so little knowledge. Admittedly my first go can be CPU-intensive, however the code is short and sweet - include JQuery and JQuery UI, mark your elements with a CSS class called "fadeThis", and put the snippet of script below into your document's HEAD:

  1.   <script type="text/javascript">
  2.     // adapted from http://www.incg.nl/blog/2008/hover-block-jquery
  3.     // and http://greg-j.com/static-content/hover-fade-redux.html
  4.     $(document).ready(function() {
  5.       $('.fadeThis').hover(
  6.         function () {
  7.           $(this).animate({backgroundColor:'Yellow', color:'Red'}, {queue:false,duration:500});
  8.         }, function () {
  9.           $(this).animate({backgroundColor:'#ececed', color:'#777777'}, {queue:false,duration:500});
  10.         });
  11.     });
  12.   </script>

The one-line functions starting with $(this).animate... fade the background and text colors on mouse over and mouse out for a subtle effect. One limitation is that I haven't found a good way to fade the border color, yet (and keep in mind I'm using Internet Explorer, so YMMV).

If you're into low-impact progressive enhancement, you can also reference the needed libraries from central Google hosting like so:

  1.   <!-- include Google's AJAX API loader -->
  2.   <script src="http://www.google.com/jsapi"></script>
  3.   <!-- load JQuery and UI from Google (need to use UI to animate colors) -->
  4.   <script type="text/javascript">
  5.     google.load("jquery", "1.2.6");
  6.     google.load("jqueryui", "1.5.2");
  7.   </script>

I hope this little snippet can form the basis of something useful for someone. It's a nice effect that fades in and out, all without the use of images.

Tags: , , ,

«November»
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456