0
這裏是我的下拉菜單類。而在隱藏方法中,Fininsh 之後的回調函數會丟失該類的範圍。我不想爲此宣佈一個全球性的可變因素。有沒有更好的解決方案?Javascript範圍丟失問題
var DropMenu = Class.create({
// Class properties.
speed: 0.4,
delay: 0,
active_submenu: null,
initialize: function() {
$$("li.drop_down_element").invoke('observe', 'mouseenter', this.mouseOver.bind(this));
$$("li.drop_down_element").invoke('observe', 'mouseleave', this.mouseOut.bind(this));
},
show: function(elem, speed) {
new Effect.BlindDown(elem, {duration: speed, queue: {position: 'end', scope: elem.identify(), limit:13}});
},
hide: function(elem, speed) {
if(elem.visible())
{
new Effect.BlindUp(elem, {duration: speed, queue: {position: 'end', scope: elem.identify(), limit: 13}, afterFinish: function(scope) { console.info(scope)}(this)});
}
},
mouseOver: function(event) {
var element = event.element(),
submenu = element.getElementsByClassName('submenu_element')[0],
width = element.getWidth();
if(submenu)
{
submenu.setStyle({minWidth: width + 'px'})
this.active_submenu = submenu;
this.show(submenu, this.speed);
}
if(!this.iefix && Prototype.Browser.IE)
{
new Insertion.After(submenu,
'<iframe id="dropdown_iefix" '+
'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
this.iefix = $(submenu.id+'_iefix');
}
//if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
},
mouseOut: function(event) {
var submenu = this.active_submenu;
if (submenu)
{
this.hide.delay(this.delay, submenu, this.speed);
}
},
fixIEOverlapping: function() {
Position.clone(this.active_submenu, this.iefix, {setTop:(!this.active_submenu.style.height)});
this.iefix.style.zIndex = 700;
this.iefix.style.border = '2px solid #000';
this.active_submenu.style.zIndex = 999;
Element.show(this.iefix);
}
});
document.observe('dom:loaded', function() { new DropMenu(); });