我是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));
}
});
你能帮帮我吗?我很绝望!
答案 0 :(得分:1)
如果我理解正确,那么您缺少的是计时器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'));
});
答案 1 :(得分:1)
我设法通过混合我的一些代码来修复它,我知道我需要一个计时器......
window.addEvent('domready', function() {
var myMorph = new Fx.Morph($('menu'), {
duration: 1000,
link: 'cancel'
});
var timer;
$('menu').addEvents({
'domready': function() {
var myDelay1 = function() {
myMorph.start({
'opacity': 0.6,
'background-color': '#000000',
'margin-top': -43
});
};
timer = myDelay1.delay(5000);
},
mouseenter: function() {
clearTimeout(timer);
myMorph.start({
'opacity': 1.0,
'background-color': '#B51000',
'margin-top': 0
});
},
mouseleave: function() {
var myDelay1 = function() {
myMorph.start({
'opacity': 0.6,
'background-color': '#000000',
'margin-top': -43
});
};
timer = myDelay1.delay(2000);
}
});});