Could we help you? Please click the banners. We are young and desperately need the money
We are using one large image containing all the images for a website and then access each and every element with the CSS-Property "background:url('image.jpg') 0 0 no-repeat" where we overgive the background-image the needed X- and Y coordinates for accessing the proper icon on the image (sprite).
While this enhances loading times, there are different problems:
That's why we decided to solve this using jQuery. But jQuery some flaws as well:
We've created a simple jQuery script that takes care of this and allows you to animate E V E R Y T H I N G you like to be animated - even background-images.
It works like this:
<a id="nav_1">MyLink</a>
Please take note of the "hoverfade" class that's being assigned to this element. We're gonna need this class so that our jQuery script knows which element(s) need to be animated. The jQuery script will simply select all elements that have this class. But yes, of course, you can change the jQuery code to your needs and e.g. adress all a-tags instead.
#nav_1 {position: relative; display: block; width:214px; height:39px; background:url('sprite.jpg') 0 -199px no-repeat;}
#nav_1 .hoverfade {position: relative; display: block; width:214px; height:39px; background:url('sprite.jpg') 0 -444px no-repeat;}
Watch out for a space between the ID of the element and the ".hoverfade" class description. It might seem wrong but it's correct like this because if you check the jQuery script below you will see that it will first select the a-tag with the class 'hoverfade', then append a span-tag right after the a-tag which also has the class 'hoverfade' assigned. The jQuery script then sets the opacity of the a-tag to zero (0=invisible) and does an animated fadeIn to 100% opacity of the span-tag.
function hoverFading() { jq(".hoverfade").each(function() { var appendstring = ""; jq(this).append(appendstring); jq(this).find(".hoverfade").css("opacity", 0); jq(this).hover( /* define mouseenter() event */ function() { jq(this).children(".hoverfade").stop().fadeTo(250, 1.00); }, /* define mouseleave() event */ function() { jq(this).children(".hoverfade").stop().fadeTo(250, 0.00); } ); }); }
$(document).ready(function() { hoverFading(); });