CSS sprite support in jQuery user interface

I am studying the use of CSS sprites, but would not like to reinvent the wheel. Is there any support in jQuery or jQuery UI? Or alternatively, a well-debugged jQuery plugin

+5
source share
4 answers

The use of sprites depends on the amount of displacement to that part of the position you want - javascript cannot access the image data, since there will be such a thing?

There are several tools to help you create sprites and provide you with basic CSS. Here is my favorite:

http://csssprites.com/

+4
source

jquery-tool, , . . tab anchor demo, stylesheet .

@Mark: -

+2

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;
}

: http://css-tricks.com/css-sprites/

+1

, , OP, .

jQuery - , jQuery , . attr():

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

For example, this is a super simple image gallery using 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('http://i.stack.imgur.com/vPDBk.gif') 0 0; }

.next {
width:43px;
height:44px;
background:url('http://i.stack.imgur.com/vPDBk.gif') -47px 0; }

.prev {
width:43px;
height:44px;
background:url('http://i.stack.imgur.com/vPDBk.gif') -91px 0; }
+1
source

All Articles