Code
Type.registerNamespace("Demo");
// Constructor
Demo.HoverButton = function(element) {
Demo.HoverButton.initializeBase(this, [element]);
this._clickDelegate = null;
this._hoverDelegate = null;
this._unhoverDelegate = null;
}
Demo.HoverButton.prototype = {
// text property accessors.
get_text: function() {
return this.get_element().innerHTML;
},
set_text: function(value) {
this.get_element().innerHTML = value;
},
// Bind and unbind to click event.
add_click: function(handler) {
this.get_events().addHandler('click', handler);
},
remove_click: function(handler) {
this.get_events().removeHandler('click', handler);
},
// Bind and unbind to hover event.
add_hover: function(handler) {
this.get_events().addHandler('hover', handler);
},
remove_hover: function(handler) {
this.get_events().removeHandler('hover', handler);
},
// Bind and unbind to unhover event.
add_unhover: function(handler) {
this.get_events().addHandler('unhover', handler);
},
remove_unhover: function(handler) {
this.get_events().removeHandler('unhover', handler);
},
// Release resources before control is disposed.
dispose: function() {
var element = this.get_element();
if (this._clickDelegate) {
Sys.UI.DomEvent.removeHandler(element, 'click', this._clickDelegate);
delete this._clickDelegate;
}
if (this._hoverDelegate) {
Sys.UI.DomEvent.removeHandler(element, 'focus', this._hoverDelegate);
Sys.UI.DomEvent.removeHandler(element, 'mouseover', this._hoverDelegate);
delete this._hoverDelegate;
}
if (this._unhoverDelegate) {
Sys.UI.DomEvent.removeHandler(element, 'blur', this._unhoverDelegate);
Sys.UI.DomEvent.removeHandler(element, 'mouseout', this._unhoverDelegate);
delete this._unhoverDelegate;
}
Demo.HoverButton.callBaseMethod(this, 'dispose');
},
initialize: function() {
var element = this.get_element();
if (!element.tabIndex) element.tabIndex = 0;
if (this._clickDelegate === null) {
this._clickDelegate = Function.createDelegate(this, this._clickHandler);
}
Sys.UI.DomEvent.addHandler(element, 'click', this._clickDelegate);
if (this._hoverDelegate === null) {
this._hoverDelegate = Function.createDelegate(this, this._hoverHandler);
}
Sys.UI.DomEvent.addHandler(element, 'mouseover', this._hoverDelegate);
Sys.UI.DomEvent.addHandler(element, 'focus', this._hoverDelegate);
if (this._unhoverDelegate === null) {
this._unhoverDelegate = Function.createDelegate(this, this._unhoverHandler);
}
Sys.UI.DomEvent.addHandler(element, 'mouseout', this._unhoverDelegate);
Sys.UI.DomEvent.addHandler(element, 'blur', this._unhoverDelegate);
Demo.HoverButton.callBaseMethod(this, 'initialize');
},
_clickHandler: function(event) {
var h = this.get_events().getHandler('click');
if (h) h(this, Sys.EventArgs.Empty);
},
_hoverHandler: function(event) {
var h = this.get_events().getHandler('hover');
if (h) h(this, Sys.EventArgs.Empty);
},
_unhoverHandler: function(event) {
var h = this.get_events().getHandler('unhover');
if (h) h(this, Sys.EventArgs.Empty);
}
}
Demo.HoverButton.registerClass('Demo.HoverButton', Sys.UI.Control);
// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
/*在所有基于文件的脚本(.js 文件)中调用 notifyScriptLoaded 方法可以通知 ScriptManager 对象引用的脚本已完成加载。脚本管理器按顺序将脚本元素注入 DOM 中,注入时等待每个脚本完成加载,然后继续下一个脚本的加载。对于执行异步回发的页面,必须在页面的每个基于文件的脚本中包含对 notifyScriptLoaded 方法的调用。要在所有浏览器类型中提供关于脚本已完成加载的通知,对 notifyScriptLoaded 方法的调用是唯一可靠的方式。
调用 notifyScriptLoaded 方法时,建议检查是否存在 Microsoft AJAX Library 以避免库不可用时发生错误。*/