CSS controlled images & buttons - revisited

Published July 29, 2006
Advertisement
In my last article on the subject I ended up with something I liked. Now I've got something I like even more. See it in action! Now, code, reveal yourself!

// HTML header"text/javascript"</span>><br><span class="cpp-comment">/* Convenience function for the multiple class system in CSS to switch button images. */</span><br>function setBtnEvent(btn, event)<br>{<br>var classes = btn.className.split(' ');<br>classes[classes.length-<span class="cpp-number">1</span>] = event; <br>btn.className = classes.join(' ');<br>} <br>// HTML body "image"

title="A little button!"
class="btnBase btnDerived idle"
onmouseover="javascript: setBtnEvent(this, 'over');"
onmouseout="javascript: setBtnEvent(this, 'idle');"
onmousedown="javascript: setBtnEvent(this, 'click');"
onclick="document.location.href='.'; return false;"
src="transp_pixel.gif" />


// CSS

.btnBase
{
cursor: pointer;
border: none;
}

.btnDerived
{
background: url(idle.gif) center center;
width: 91px;
height: 40px;
}
.btnDerived.over
{
background: url(over.gif) center center;
}
.btnDerived.click
{
background: url(click.gif) center center;
}




I implemented an inheritance like behaviour for classes by using multiple classes and multiple selectors to create a btnBase class for all buttons. I then made a small &#106avascript function that takes care of the class changing for me, since I want to keep the base class and the real class but change the behaviour class (the last one). This makes a strong coupling between the markup and stylesheet, but since it's part of a system then I think it's ok. I guess you could implement functions per event so you don't get typographical errors like "ovre" instead of "over".<br><br>Of course, this does <em>not</em> work in IE if you have more than one different button because of a CSS parsing bug. It just uses the last multiple selector definition...<br><br>Another flaw is that it doesn't preload images which causes flickering. This can be solved with <a href="http://www.alistapart.com/articles/sprites">sprites</a>. If someone knows of a better way, let me know.<div> </div>
Previous Entry Conditional Statements
0 likes 2 comments

Comments

Rob Loach
*cough* Drupal *cough*......... What I meant was that you should install Drupal on seriema.net .
July 29, 2006 10:50 AM
Seriema
lol... I have Drupal on seriema.net! :P Currently I'm using my personal space to play around with it (you need to know the secret link!). But the code in the post I developed for work.
July 31, 2006 01:34 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement