That approach seems a little heavy to me, honestly.
You'll already have five images, so why not just create a sprite containing the pressed state for each individual button as well as a no-pressed state, and then slide using the CSS "background-position" property?
Your CSS would look something like:
div.buttons {
background-image: url(images/myimage.png);
background-position: 0 0;
position:relative;
width:64px;
}
div.buttons.dog{
background-position: 0 32px;
}
div.buttons.cat{
background-position: 0 64px;
}
div.buttons div.dog{
position:absolute;
left: 0;
top: 0;
width: 32px;
height: 32px;
border: transparent 1px solid;
}
div.buttons div.cat{
position:absolute;
left: 32px;
top: 0;
width: 32px;
height: 32px;
border: transparent 1px solid;
}
With your HTML as:
<div class="buttons">
<div class="dog"></div>
<div class="cat"></div>
</div>
And then something simple for jQuery such as:
$('div.buttons').children().each(function(){
$(this).click(function() { $(this).parent().addClass($(this).attr('class')).removeClass($(this).siblings().attr('class'));
});
UPDATE
Per the asker's request, here's how you would implement a div overlay on the button layer.
First, start with the div.buttons
CSS block up top, only instead of a sprite use your image for the buttons in their default "off" position.
Then, create four containers as such (I've used div
s here, but you could conceivably use any element that gives the appropriate semantic context for a container).
CSS
div.buttons{
background-image: url(images/myimage.png);
background-position: 0 0;
position:relative;
width:136px; /* whatever your width for the image is */
}
div.fish, div.rabbit, div.dog, div.cat {
position: absolute;
top: 0;
background-size: cover;
height: 24px; /* Whatever the height of your buttonbar is */
}
div.cat{
width: 32px; /* Width of your button */
left: 0;
background-image: none;
}
div.on.cat {
background-image: url(path/to/catImage.png);
}
div.dog {
left: 32px; /* The width of the preceding button(s), if they exist */
width: 40px; /* Width of your button */
background-image: none;
}
div.on.dog {
background-image: url(path/to/dogImage.png);
}
div.rabbit {
left: 72px; /* The width of the preceding button(s), if they exist */
width: 32px; /* Width of your button */
background-image: none;
}
div.on.rabbit {
background-image: url(path/to/rabbitImage.png);
}
div.fish {
left: 104px; /* The width of the preceding button(s), if they exist */
width: 40px; /* Width of your button */
background-image: none;
}
div.on.fish {
background-image: url(path/to/fishImage.png);
}
HTML
<div class="buttons">
<div class="cat"></div>
<div class="dog"></div>
<div class="rabbit"></div>
<div class="fish"></div>
</div>
This assumes that your div "wrapping" the button slice has a class matching its animal, but this could obviously be whatever you'd like it to be. Then, on the click event, you'd simply apply the class that you need to the pressed button. The jQuery you'd use would look like this:
jQuery
$('.buttons > div').on('click',function(){
var t = $(this);
t.siblings.removeClass('on');
t.addClass('on');
//Whatever your button is supposed to do normally here
});