官术网_书友最值得收藏!

  • jQuery UI Cookbook
  • Adam Boduch
  • 500字
  • 2021-08-13 16:40:52

Using effects with the button hover state

The button widget utilizes the various states found in the jQuery UI theme framework. For example, when the user hovers over a button widget, this event triggers a handler inside the button widget that applies the ui-state-hover class to the element, changing its appearance. Likewise, when the mouse leaves the widget, a different handler removes that class.

This default functionality of the button widget works fine—it just uses the addClass() and removeClass() jQuery functions to apply the hover class. As the user moves around and considers what he/she wants to do next, the mouse is likely to move in and out of button widgets; this is where we tweak the experience by providing the user with some subtle effects.

Getting ready

For this example, we'll just create three simple button elements that will serve as the button widgets. This way, we can experiment with moving the mouse pointer over several buttons.

<div>
    <button>Button 1</button>
    <button>Button 2</button>
    <button>Button 3</button>
</div>

How to do it...

Let's extend the capabilities of the default button widget to include a new option called animateHover that when true, animates the addition and removal of the ui-state-hover class.

(function( $, undefined ) {

$.widget( "ab.button", $.ui.button, {

    options: {
        animateHover: false 
    },

    _create: function() {

        this._super( "create" );

        if ( !this.options.animateHover ) {
            return;
        }

        this._off( this.element, "mouseenter mouseleave" );

        this._on({
            mouseenter: "_mouseenter",
            mouseleave: "_mouseleave"
        });

    },

    _mouseenter: function( e ) { 
        this.element.stop( true, true )
                    .addClass( "ui-state-hover", 200 );
    },

    _mouseleave: function( e ) {
        this.element.stop( true, true )
                    .removeClass( "ui-state-hover", 100 );
    }

});

})( jQuery );

$(function() {
    $( "button" ).button( { animateHover: true } );
});

How it works...

We've added a new option to the button widget called animateHover. When true, buttons will animate the addition or removal of the CSS properties found in ui-state-hover class. This is all done by overriding the _create() method, called when the button widget is first instantiated. Here, we're checking if the animateHover option is false, after we call the original _create() method that performs routine button initialization tasks.

If the option is set, the first job is unbinding the original mouseenter and mouseleave event handlers from the button. These handlers are what, by default, simply add or remove the hover class. This is exactly what we want to change, so once the original handlers are removed, we're free to register our own using _on(). This is where we use the stop(), addClass(), and removeClass() functions. The jQuery UI effects extensions apply to the addClass() and removeClass() functions if a duration is given after the class name, which we've done here. We want the adding of the ui-state-hover class to take 200 milliseconds and the removal of the class to take 100 milliseconds since the initial hover is more noticeable by the user. Finally, the stop( true, true ) call is a common technique in jQuery for ensuring animations don't overlap and cause jittery behavior from the user perspective.

主站蜘蛛池模板: 无为县| 麻城市| 中山市| 安泽县| 东山县| 印江| 斗六市| 贡山| 红桥区| 临安市| 苏州市| 许昌市| 大竹县| 隆子县| 陆良县| 临澧县| 鹤岗市| 棋牌| 昭通市| 卫辉市| 昔阳县| 米林县| 襄城县| 万全县| 荔浦县| 大城县| 上犹县| 屯留县| 寻乌县| 酒泉市| 金山区| 吉首市| 前郭尔| 宜黄县| 丹寨县| 盐亭县| 博野县| 夹江县| 格尔木市| 仪征市| 林芝县|