Friday, April 30, 2010

jQueryUI Hovering

The jQueryUI user interface library offers developers building interactive web applications not only a set of widgets to use, but also, a set of effects. An effect, in this context, can be thought of as a transition from one state to another by a user interface component. As an example, imagine you have a button element that is in a hidden state. Showing that button would be considered an effect, albeit, a very basic one. A jQueryUI effect adds some appeal to these transitions. In essence, jQueryUI effects can be thought of as user interface component state transitions that enhance the overall user experience.

Another nice feature in the jQueryUI repertoire is the CSS framework used for changing the look and feel of the widget set. This framework allows developers to choose an existing theme or to build their own. CSS classes within the framework are flexible enough that they aren't restricted to the jQueryUI widgets. Your user interface isn't going to be composed entirely of widgets. You can use these CSS classes to help give the non-widget elements of your application more of a theme.

An example use of applying the jQueryUI CSS framework to a non-widget element would be using the ui-state-hover class. Lets say we have a list of li elements. The idea is to add the ui-state-hover CSS class to each li element when the user hovers over it. This is fairly straightforward to do with the jQuery addClass() and removeClass() methods. You call these methods inside a hover in and a hover out callback respectively. Example callback functions are shown below.
function hoverin(element){
jQuery(element).addClass("ui-state-hover");
}

function hoverout(element){
jQuery(element).removeClass("ui-state-hover");
}
jQueryUI allows us to add an interaction to the addClass() and removeClass() methods. Typically, we would only want to add an effect to the addClass() for hover in events. Otherwise, the user interface tends to look too busy. Here is the same example modified to introduce an effect when adding the class. The the visual changes made to the element as a result of adding the class will take place over a duration of 150 milliseconds rather than instantaneously.
function hoverin(element){
jQuery(element).addClass("ui-state-hover", 150);
}

function hoverout(element){
jQuery(element).removeClass("ui-state-hover");
}
There is, however, one problem with this approach. There is a chance that the ui-state-hover class will not be removed from the element by hoverout(). This is because the user can, and most definitely will at some point, hover out of the element during the 150 millisecond effect duration. Keep in mind, the element doesn't actually have the ui-state-hover class until the 150 millisecond effect duration is up. When hoverout() is called during the effect, no class is removed because it hasn't been added yet. Below is a work-around for this issue that involves using a secondary state class.
function hoverin(element){
jQuery(element).addClass("app-state-hover");
jQuery(element).addClass("ui-state-hover", 150, function(){
if(!jQuery(element).hasClass("app-state-hover")){
jQuery(element).removeClass("ui-state-hover");
}
jQuery(query).removeClass("app-state-hover");
});
}

function hoverout(element){
jQuery(element).removeClass("ui-state-hover app-state-hover");
}
Here we introduce a new class called app-hover-state. This class will be used to track the hover state of the element since we are now using an effect. The hoverin() function will now add this class to the element before starting the visual effect. The hoverout() function will now remove both the ui-state-hover and the app-state-hover classes. This is important because even if the effect hasn't yet finished when the user hovers out of the element, the app-state-hover class will be removed from the element. The visual effect in hoverin() now has a callback function that is executed when the duration of the effect has completed. This callback will remove the ui-state-hover class if the app-state-hover class has been removed. In this case, if app-state-hover has been removed can only mean one thing; a hoverout() call was made during the duration of the effect and the ui-state-hover class should also be removed.

1 comment :

  1. This is a nice article, thanks! It pointed me in the right direction but didn't quite do what I was looking for and while digging around I managed to find a little gem. I believe if you were to call...


    function hoverin(element){
    jQuery(element).stop(true, true).addClass("ui-state-hover", 150);
    }

    function hoverout(element){
    jQuery(element).stop(true, true).removeClass("ui-state-hover");
    }

    ...it would render the same result. Calling stop(true, true) will clear the animation queue and finish the currently running effect. This way you are guaranteed to always have the class applied before it is removed. http://api.jquery.com/stop/

    Tom

    ReplyDelete