, , OP, .
jQuery - , jQuery , . attr():
$(elementSelector).attr("class", spriteClassOfChoie);
For example, this is a super simple image gallery using sprites and jQuery:
Script:
$(function() {
var sprites = ["home", "next", "prev"];
var showing = 0;
$("#gallery").addClass(sprites[showing]);
$("input").click(function() {
$("#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; }
source
share