javascript - Mootools:如何使用 'mouseenter' 和 'mouseleave' 停止延迟事件

标签 javascript events mootools delay mouseenter

我是 javascript 和 mootols 的新手,我有一个关于使用 Mootools 进行延迟和事件处理的问题。

我的目标:菜单出现在主要位置,然后在一定时间后移动到次要位置并切换不透明度,然后在 mouseenter 上它以完全不透明的方式重新出现在主要位置,在 mouseleave 上它回到次要位置.

我已经解决这个问题一段时间了,我已经阅读了很多演示和问题,但我无法切入正题......我在 mouseenter mouseleave 上遇到了问题(有延迟,因此,如果我将鼠标放在刚刚加载的菜单上一段时间后,菜单就会转到它的次要位置)或者如果我只是将鼠标移入和移出菜单,就会产生困惑。

代码如下:

window.addEvent('domready', function(){
var el = $('menu'),
    color = el.getStyle('backgroundColor');


        // Morphs to secondary style
    var menuHide = function(){
    this.morph({'background-color': '#000000',
   'opacity': 0.6,'margin-top': -43}) };
    menuHide.delay(5000, el);



    $('menu').set('duration', 100).addEvents({

        mouseenter: function(){ 
            // Morphs back to the primary style
                this.morph({
                'opacity': 1.0,
                'background-color': '#B51000',
                'margin-top': 0
            });
        },
        mouseleave: function(){

            // Morphs back to the secondary style
            this.store("timer", (function(){
                            this.morph({
                            'opacity': 0.6,
                            'background-color': '#000000',
                            'margin-top': -43
                            });
            }).delay(2000,this));
        }

    });

你能帮帮我吗?我很绝望!

最佳答案

如果我没理解错的话,您缺少的是计时器 ID,然后在开始新的转换之前将其清除。

像这样的东西可能会成功(未经测试):

var Menu = new Class({
    initialize: function(el) {
        this.element = el;
        this.timer = null;

        this.element.set('duration', 100);
        this.element.addEvents({
            mouseenter: this.enter.bind(this),
            mouselave: this.leave.bind(this)
        });

    },
    enter: function() {
        this.stopTimer();
        this.element.morph({
            'opacity': 1.0,
            'background-color': '#B51000',
            'margin-top': 0
        });
    },
    leave: function() {
        this.stopTimer();
        this.timer = this.element.morph({
            'opacity': 0.6,
            'background-color': '#000000',
            'margin-top': -43
        }).delay(2000));
    },
    stopTimer: function() {
        if (this.timer != null) {
            clearTimeout(this.timer);
            this.timer = null;
        }
    }
});

var menu;
window.addEvent('domready', function() {
    menu = new Menu($('menu'));
});

关于javascript - Mootools:如何使用 'mouseenter' 和 'mouseleave' 停止延迟事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9469838/

相关文章:

javascript - npm:缺少 node_modules 中的传递依赖项

JavaScript 构建系统在 Sublime Text 2 中使用 Node.js 错误

javascript - AngularJs ng-重复多个 orderBy 和条件

javascript - 获取被点击的列表项的位置

javascript - 祖先的事件处理程序可以停止其子元素的事件之一的传播吗?

javascript - 每 2 行更改一次,而不是每个表行更改一次

javascript - 我如何获得焦点窗口标题 - Mootools Mocha UI

javascript - Node js(getConnection)

events - Mootools:如何在补间或变形 FX 或其他事件结束后触发一个函数(或多个函数)?

javascript - onHashChange running onLoad...尴尬