开发者

What's the best way to do this: Show/hide menu on mouseenter/mouseleave, with a button to toggle visibility?

开发者 https://www.devze.com 2023-03-25 07:53 出处:网络
http://jsfiddle.net/nicktheandroid/ppNaX/ This is somewhat theoretical. The menu shows/hides on mouseenter/mouseleave, with a toggle button to remove this functionality should a user find it annoying

http://jsfiddle.net/nicktheandroid/ppNaX/

This is somewhat theoretical. The menu shows/hides on mouseenter/mouseleave, with a toggle button to remove this functionality should a user find it annoying. In my example i have the element that triggers the toggle just showing it and not actually toggling it, but what I really want to know is, what's the best way to do something like this?

Is the开发者_如何学Gore a better way to do it? Custom jQuery events? Using .data()? Could it be more advanced(silly?)? More simple? Benefits, pitfalls?


Conceptually all correct. Details can be improved:

var menuWrapper = $('#menuWrapper'),
    menu = $('#menu');

var toggleFade = function (toggle) {
    menu.stop(true, true);
    toggle ? menu.fadeIn() : menu.fadeOut();
};

menuWrapper.bind('mouseenter', function () {
    toggleFade(true);
}).bind('mouseleave', function () {
    toggleFade(false);
});


$('#showMenu').click(function () {
    toggleFade(true);
    menuWrapper.unbind('mouseenter').unbind('mouseleave');
});

Most of all — you need to stop the animation before running another one, because the user can do a mouseover while the menu is animated, leading to a mess.

0

精彩评论

暂无评论...
验证码 换一张
取 消