As long as we're at it, here is a *much* better version of the jQuery script
from my earlier message.

Instead of this:

<script type="text/javascript">

    $(function() {
        $('input.button')
            .mousedown( function() {
                $(this).addClass( 'buttonActive' );
            })
            .mouseup( function() {
                $(this).removeClass( 'buttonActive' );
            });
    });

</script>

Let's make it more general:

<script type="text/javascript">

    // Toggle a classname in response to a pair of events.
    // The event names can be in any format that $().bind() accepts,
    // including single events, multiple events separated by spaces,
    // and namespaced events for easy removal.
    $.fn.classToggler = function( className, onEvent, offEvent ) {
        this
            .bind( onEvent, function() {
                $(this).addClass( className );
            })
            .bind( offEvent, function() {
                $(this).removeClass( className );
            });
    };

    $(function() {
        $('input.button').classToggler( 'buttonActive', 'mousedown',
'mouseup' );
    });

</script>

What we've done here is made a mini jQuery plugin - we've added a
$(...).classToggler() method to jQuery and then we call that method.

Why is that better - even though it's a few lines longer? Well, adding and
removing a class in response to a pair of events is a pretty common
operation. In fact, the next thing you'll want to do is a hover effect,
right?

With my first version of the code, you'd have to duplicate the entire block,
changing the mousedown and mouseup to mouseover and mouseout and using a
different classname.

With the new version, you only have to add one line of code. Simply change
the last block to:

    $(function() {
        $('input.button')
            .classToggler( 'buttonHover', 'mouseover', 'mouseout' )
            .classToggler( 'buttonActive', 'mousedown', 'mouseup' );
    });

Add the corresponding CSS rule:

input.button:hover, input.buttonHover { ... }

and you're all set.

-Mike
______________________________________________________________________
css-discuss [[email protected]]
http://www.css-discuss.org/mailman/listinfo/css-d
List wiki/FAQ -- http://css-discuss.incutio.com/
List policies -- http://css-discuss.org/policies.html
Supported by evolt.org -- http://www.evolt.org/help_support_evolt/

Reply via email to