Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On hover change jQuery UI's button icon - broken on IE and Chrome

I've created a jQuery UI button, and would like to add a 'X' secondary icon on mouseenter event, and remove the 'X' on mouseleave event. Sounds easy.

$("#hover").button({
icons: {
    primary: "ui-icon-tag"
}
}).hover(function (event) {
    // hover in           
    $(this).button("option", "icons", { primary: "ui-icon-tag", secondary: "ui-icon-close" });
}, function (event) {
    // hover out
    $(this).button("option", "icons", { primary: "ui-icon-tag" });
});

The hover event is triggered multiple times if you move the mouse cursor within the button.

It does work in Firefox, however, failed with IE and Chrome.

It's hard to explain the quirky effect in words but please see the jsFiddle here:

http://jsfiddle.net/27SSr/

My goal is to achieve consistent effect across all browsers. Thanks =)

EDIT***

Now i have some clue as to why the hover is broken in IE and Chrome. Hovering the UI button a few times and inspect the button element in Firebug, turns out the html tags are flawed by jQuery UI widget..

<button id="hover" class="ui-button ui-widget ui-state-default ui-corner-all ui-state-hover ui-button-text-icons" role="button">
<span class="ui-button-icon-primary ui-icon ui-icon-tag"/>
<span class="ui-button-text">Hover me!</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-close"/>
</button>
<span class="ui-button-icon-primary ui-icon ui-icon-tag"/>
<span class="ui-button-text">Hover me!</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-close"/>
</button>

Note the extra SPANs and the BUTTON closing tag now? Is this behavior a jQuery UI's bug?

You can try the example here:

http://jsfiddle.net/B5sAk/1/

like image 780
Kagawa Avatar asked Nov 15 '11 10:11

Kagawa


2 Answers

Okay, after fiddling for awhile, finally a workaround that works.. at least that seems decent to me.

$("button").button({
    icons: {
        primary: "ui-icon-tag",
        secondary: "ui-icon-blank"
    }
}).hover(function () {
    // hover in            
    $(".ui-button-icon-secondary", this).toggleClass("ui-icon-close ui-icon-blank");
}, function () {
    // hover out
    $(".ui-button-icon-secondary", this).toggleClass("ui-icon-close ui-icon-blank");
});

And add one line to CSS:

.ui-icon-blank { background-position: -240px -224px; } /* point to blank area of sprite */

See here for a working example: http://jsfiddle.net/GCme2/4/

like image 177
Kagawa Avatar answered Oct 26 '22 22:10

Kagawa


Why use javascript? Is it a requirement? Can you use css :hover psuedoclass?

.ui-button-text .myicon{background-image: url('/path/reg.jpg')}
.ui-button-text .myicon:hover{background-image: url('/path/to.jpg')}

If you must use jquery, then I would say you need to do e.stop() after each

    ...hover(function (event) {
        // hover in           
        $(this).button("option", "icons", { primary: "ui-icon-tag", secondary: "ui-icon-close" });
        event.stop();
    }, function (event) {
        // hover out
        $(this).button("option", "icons", { primary: "ui-icon-tag" });
    event.stop();

});
like image 38
Todd Baur Avatar answered Oct 26 '22 23:10

Todd Baur