5

I'm looking into using CSS sprites, but wouldn't like to invent the wheel. Is there existing support in jQuery or jQuery UI? Or as an alternative, a well debugged jQuery plugin

3
  • What specific tasks do you want done? Commented Sep 16, 2010 at 19:03
  • Just to turn some of the images on a web application to sprites to make the page load more efficient Commented Sep 16, 2010 at 21:10
  • see also stackoverflow.com/questions/527336/tools-to-make-css-sprites Commented Sep 16, 2010 at 22:06

4 Answers 4

4

Using sprites depends on the amount of offset to the part of the position you want -- javascript can't access image data so how would there be such a thing?

There are some tools to help you make sprites and provide you with the base CSS however. Here's my favorite:

http://csssprites.com/

Sign up to request clarification or add additional context in comments.

Comments

2

There are some good jquery-tool demos you can copy and then modify. They have good practices. I would start with the tab anchor demo, their stylesheet is well written.

@Mark: The tabs demo uses one image

6 Comments

How is a tabs demo page relevant to CSS sprites?
There is an image used to draw and swap the tab navigation
Is it one image or multiple images? If its multiple images its unrelated to CSS Sprites.
Thats why I was asking. That is sprites.
|
1

Depending on the specific tasks you want to accomplish, which you do not specify in the OP, you might no need a plugin at all.

A possible way to use sprites with jQuery is to create a separate class for each sprite state, and then use jQuery to change the class attribute of the element showing the sprite with .attr():

  // Change the sprite state of an element
$(elementSelector).attr("class", spriteClassOfChoie);

For example here is a super simple image gallery making use of sprites and jQuery:

jsFiddle example

Script:

$(function() {

      // The sprite classes
    var sprites = ["home", "next", "prev"];

      // Which image is showing
    var showing = 0;

      // Show the first image
    $("#gallery").addClass(sprites[showing]);

      // Add a click handler to change sprite states
    $("input").click(function() { 

          // Cycle through images by making use of sprites
        $("#gallery").attr("class", sprites[(++showing % sprites.length)]);
    });
});​

HTML:

<input type="button" value="Show Next Image" />
<br/><br/>
<div id="gallery"></div>

CSS:

.home {
width:46px;
height:44px;
background:url('https://i.sstatic.net/vPDBk.gif') 0 0; }

.next {
width:43px;
height:44px;
background:url('https://i.sstatic.net/vPDBk.gif') -47px 0; }

.prev {
width:43px;
height:44px;
background:url('https://i.sstatic.net/vPDBk.gif') -91px 0; }

Comments

1

Why not do it all in CSS? A la:

btn
{
width:50px;
height:50px;
background:url(images/btnspite.png) -30px 100px;
}
btn:hover
{
background-position:-30px -150px;
}
btn:active
{
background-position:-30px -200px;
}

This'll get you started on actually implementing it: http://css-tricks.com/css-sprites/

2 Comments

4 spaces before a line will format it as code. Highlight a section and hit ctr-k to do this quickly.
Thanks Peter...it was pretty ugly-lookin' :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.