开发者

Mootools: how to stop event, when other event happens

开发者 https://www.devze.com 2023-02-22 07:12 出处:网络
i´ve got a question about mootools eventhandling. I wanna delay a mouseenter event for a dropdown navigation. After 1 second the drowdown list will be shown by \"setStyle(\'display\', \'block\')...

i´ve got a question about mootools eventhandling.

I wanna delay a mouseenter event for a dropdown navigation. After 1 second the drowdown list will be shown by "setStyle('display', 'block')...this is what i´ve got so far, and it´s working:

$('main-nav').getElements('li.level-1 ul.quick-nav').setStyle('display', 'none');

                            $('main-nav').getElements('li.level-1').each(function(elem){
                               var list = elem.getElement('.quick-nav');

                                elem.addEvents({
                                    'mouseenter' : function(event){                                                                                     
                                        (function() {
                                            elem.getElement('.quick-nav').setStyle('display', 'block');
                                        }).delay(1000)},
                                    'mouseleave' : function(event){                                               
                                            elem.getElement('.quick-nav').setStyle('display', 'none');
                                        }
                                });
                            });

I´ve delayed the mouseenter event with the delay function...the problem i got and still can´t solve is that the mouseenter event will although happen when i already left my navigation item. I enter the item, leave the item immediately, and after one second the subitem still appears. I therefore need some kind of check withi开发者_如何学Gon the mouseleave event, wheather my menuitem is already displayed or not. Then i could stop the mouseenter event, if the menuitem is still not visible... I don´t know how i can respond the mousenter event from the function of the mouseleave event...hope anybody understood this...

Thanks in advance.


use a timer and clearTimeout on mouseleave (also $clear(timer) in older versions of mootools).

$('main-nav').getElements('li.level-1 ul.quick-nav').setStyle('display', 'none');

$('main-nav').getElements('li.level-1').each(function(elem) {
    var list = elem.getElement('.quick-nav');
    var timer;
    elem.addEvents({
        'mouseenter': function(event) {
            timer = (function() {
                elem.getElement('.quick-nav').setStyle('display', 'block');
            }).delay(1000)
        },
        'mouseleave': function(event) {
            clearTimeout(timer);
            elem.getElement('.quick-nav').setStyle('display', 'none');
        }
    });
});
0

精彩评论

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