富贵资源网 Design By www.hznty.com
jQuery的事件模块严重依赖于其数据储存(jQuery.data),你会发现我的代码中的dataManager对象对应它。
这里只提供bind和unbind方法。暂不包含
1, 事件命名空间(event namespace)
2, 事件代理(event delegation)
3, 特殊事件如dom ready
接口如下:
复制代码 代码如下:
E.bind(el, 'click', fn);
E.bind(el, 'click', fn, data);
E.unbind(el, 'click', fn);
E.unbind(el, 'click');
E.unbind(el);
复制代码 代码如下:
/**
* Event from jQuery
* 2011-06-20 snandy
*
* A number of helper functions used for managing events.
* Many of the ideas behind this code originated from jQuery library (1.6.2).
*
* example
*
* E.bind(el, 'click', fn);
*
* E.bind(el, 'click', fn, data);
*
* E.unbind(el, 'click', fn);
*
* E.unbind(el, 'click');
*
* E.unbind(el);
*
*/
E = function( window ) {
var uuid = 0,
globalCache = {},
doc = window.document,
w3c = !!doc.addEventListener,
expando = 'snandy' + (''+Math.random()).replace(/\D/g, ''),
addListener = w3c ?
function(el, type, fn) { el.addEventListener(type, fn, false); } :
function(el, type, fn) { el.attachEvent('on' + type, fn); },
removeListener = w3c ?
function(el, type, fn) { el.removeEventListener(type, fn, false); } :
function(el, type, fn) { el.detachEvent('on' + type, fn); };
dispatch = w3c ?
function( el, type ){
try{
var evt = doc.createEvent('Event');
evt.initEvent( type, true, true );
el.dispatchEvent( evt );
}catch( e ){ alert( e ) };
} :
function( el, type ){
try{
el.fireEvent( 'on' + type );
}catch( e ){ alert( e ); }
},
dataManager = {
data : function ( elem, name, data ) {
var getByName = typeof name === "string",
thisCache,
isNode = elem.nodeType,
cache = isNode ? globalCache : elem,
id = isNode ? elem[ expando ] : elem[ expando ] && expando;
if(!id && isNode) {
elem[expando] = id = ++uuid;
}
if(!cache[id]){
cache[id] = {};
}
thisCache = cache[id];
if(data !== undefined) {
thisCache[name] = data;
}
return getByName ? thisCache[name] : thisCache;
},
removeData : function ( elem, name ) {
var id = elem[expando],
thisCache = globalCache[id];
if(!id || !thisCache){
return;
}
if(typeof name === 'string') {
delete thisCache[name];
}else{
delete globalCache[id];
}
}
};
function returnFalse() {
return false;
}
function returnTrue() {
return true;
}
function now() {
return (new Date).getTime();
}
function isEmptyObject( obj ){
for( var i in obj ){
return false;
}
return true;
}
function addEvent (elem, types, handler, data) {
if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
return;
}
if ( handler === false ) {
handler = returnFalse;
} else if ( !handler ) {
return;
}
var elemData = dataManager.data( elem ),
events = elemData.events,
eventHandle = elemData.handle,
types = types.split(" ");
if ( !events ) {
elemData.events = events = {};
}
if ( !eventHandle ) {
elemData.handle = eventHandle = function ( e ) {
return evtHandle.call( eventHandle.elem, e );
};
}
eventHandle.elem = elem;
var type, i = 0;
while ( type = types[i++] ) {
var handleObj = {handler : handler, data : data},
handlers = events[type];
if ( !handlers ) {
handlers = events[type] = [];
addListener( elem, type, eventHandle );
}
handlers.push( handleObj );
}
elem = null;
}
function evtHandle ( event ) {
event = fixEvent( event || window.event );
var handlers = ((dataManager.data(this, "events") || {})[event.type] || []).slice(0);
event.currentTarget = this;
for( var j = 0, l = handlers.length; j < l; j++ ) {
var handleObj = handlers[j];
event.handler = handleObj.handler;
event.data = handleObj.data;
event.handleObj = handleObj;
var ret = handleObj.handler.call( this, event );
if( ret !== undefined ) {
if( ret === false ) {
event.preventDefault();
event.stopPropagation();
}
}
if( event.isImmediatePropagationStopped() ) {
break;
}
}
}
function removeEvent( elem, types, handler ) {
// don't do events on text and comment nodes
if( elem.nodeType === 3 || elem.nodeType === 8 ) {
return;
}
if( handler === false ) {
handler = returnFalse;
}
var type, origType, i = 0, j,
elemData = dataManager.data( elem ),
events = elemData && elemData.events;
if( !elemData || !events ) {
return;
}
// Unbind all events for the element
if( !types ) {
types = types || "";
for ( type in events ) {
removeEvent( elem, type );
}
return;
}
// Handle multiple events separated by a space
// jQuery(...).unbind("mouseover mouseout", fn);
types = types.split(" ");
while( (type = types[ i++ ]) ) {
origType = type;
handleObj = null;
eventType = events[ type ];
if( !eventType ) {
continue;
}
if( !handler ) {
for ( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];
removeEvent( elem, origType, handleObj.handler );
eventType.splice( j--, 1 );
}
continue;
}
for( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];
if( handler === handleObj.handler ) {
// remove the given handler for the given type
eventType.splice( j--, 1 );
}
}
}
// remove generic event handler if no more handlers exist
if ( eventType.length === 0 ) {
delete events[ origType ];
}
// Remove the expando if it's no longer used
if ( isEmptyObject( events ) ) {
var handle = elemData.handle;
if ( handle ) {
handle.elem = null;
}
delete elemData.events;
delete elemData.handle;
if ( isEmptyObject( elemData ) ) {
dataManager.removeData( elem, 'events' );
}
}
}
function Event( src ) {
this.originalEvent = src;
this.type = src.type;
this.timeStamp = now();
}
Event.prototype = {
preventDefault: function() {
this.isDefaultPrevented = returnTrue;
var e = this.originalEvent;
if( e.preventDefault ) {
e.preventDefault();
}
e.returnValue = false;
},
stopPropagation: function() {
this.isPropagationStopped = returnTrue;
var e = this.originalEvent;
if( e.stopPropagation ) {
e.stopPropagation();
}
e.cancelBubble = true;
},
stopImmediatePropagation: function() {
this.isImmediatePropagationStopped = returnTrue;
this.stopPropagation();
},
isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse
};
function fixEvent( evt ) {
var props = "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),
len = props.length;
var originalEvent = evt;
evt = new Event(originalEvent);
for(var i = len, prop; i;) {
prop = props[ --i ];
evt[ prop ] = originalEvent[ prop ];
}
if(!evt.target) {
evt.target = evt.srcElement || document;
}
if( evt.target.nodeType === 3 ) {
evt.target = evt.target.parentNode;
}
if( !evt.relatedTarget && evt.fromElement ) {
evt.relatedTarget = evt.fromElement === evt.target ? evt.toElement : evt.fromElement;
}
if( evt.pageX == null && evt.clientX != null ) {
var doc = document.documentElement, body = document.body;
evt.pageX = evt.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
evt.pageY = evt.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
}
if( !evt.which && ((evt.charCode || evt.charCode === 0) ? evt.charCode : evt.keyCode) ) {
evt.which = evt.charCode || evt.keyCode;
}
if( !evt.metaKey && evt.ctrlKey ) {
evt.metaKey = evt.ctrlKey;
}
if( !evt.which && evt.button !== undefined ) {
evt.which = (evt.button & 1 ? 1 : ( evt.button & 2 ? 3 : ( evt.button & 4 ? 2 : 0 ) ));
}
return evt;
}
function bind ( el, type, fn, data ) {
var handler;
if( typeof type === "object" ) {
for( var key in type ) {
bind(el, key, type[key], data);
}
return;
}
handler = fn;
addEvent( el, type, handler, data );
}
function unbind ( el, type, fn ) {
if( typeof type === "object" ) {
for ( var key in type ) {
unbind( el, key, type[key] );
}
}else {
removeEvent( el, type, fn );
}
}
return {
data : dataManager.data,
removeData : dataManager.removeData,
bind : bind,
unbind : unbind
};
}(this);
这里只提供bind和unbind方法。暂不包含
1, 事件命名空间(event namespace)
2, 事件代理(event delegation)
3, 特殊事件如dom ready
接口如下:
复制代码 代码如下:
E.bind(el, 'click', fn);
E.bind(el, 'click', fn, data);
E.unbind(el, 'click', fn);
E.unbind(el, 'click');
E.unbind(el);
复制代码 代码如下:
/**
* Event from jQuery
* 2011-06-20 snandy
*
* A number of helper functions used for managing events.
* Many of the ideas behind this code originated from jQuery library (1.6.2).
*
* example
*
* E.bind(el, 'click', fn);
*
* E.bind(el, 'click', fn, data);
*
* E.unbind(el, 'click', fn);
*
* E.unbind(el, 'click');
*
* E.unbind(el);
*
*/
E = function( window ) {
var uuid = 0,
globalCache = {},
doc = window.document,
w3c = !!doc.addEventListener,
expando = 'snandy' + (''+Math.random()).replace(/\D/g, ''),
addListener = w3c ?
function(el, type, fn) { el.addEventListener(type, fn, false); } :
function(el, type, fn) { el.attachEvent('on' + type, fn); },
removeListener = w3c ?
function(el, type, fn) { el.removeEventListener(type, fn, false); } :
function(el, type, fn) { el.detachEvent('on' + type, fn); };
dispatch = w3c ?
function( el, type ){
try{
var evt = doc.createEvent('Event');
evt.initEvent( type, true, true );
el.dispatchEvent( evt );
}catch( e ){ alert( e ) };
} :
function( el, type ){
try{
el.fireEvent( 'on' + type );
}catch( e ){ alert( e ); }
},
dataManager = {
data : function ( elem, name, data ) {
var getByName = typeof name === "string",
thisCache,
isNode = elem.nodeType,
cache = isNode ? globalCache : elem,
id = isNode ? elem[ expando ] : elem[ expando ] && expando;
if(!id && isNode) {
elem[expando] = id = ++uuid;
}
if(!cache[id]){
cache[id] = {};
}
thisCache = cache[id];
if(data !== undefined) {
thisCache[name] = data;
}
return getByName ? thisCache[name] : thisCache;
},
removeData : function ( elem, name ) {
var id = elem[expando],
thisCache = globalCache[id];
if(!id || !thisCache){
return;
}
if(typeof name === 'string') {
delete thisCache[name];
}else{
delete globalCache[id];
}
}
};
function returnFalse() {
return false;
}
function returnTrue() {
return true;
}
function now() {
return (new Date).getTime();
}
function isEmptyObject( obj ){
for( var i in obj ){
return false;
}
return true;
}
function addEvent (elem, types, handler, data) {
if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
return;
}
if ( handler === false ) {
handler = returnFalse;
} else if ( !handler ) {
return;
}
var elemData = dataManager.data( elem ),
events = elemData.events,
eventHandle = elemData.handle,
types = types.split(" ");
if ( !events ) {
elemData.events = events = {};
}
if ( !eventHandle ) {
elemData.handle = eventHandle = function ( e ) {
return evtHandle.call( eventHandle.elem, e );
};
}
eventHandle.elem = elem;
var type, i = 0;
while ( type = types[i++] ) {
var handleObj = {handler : handler, data : data},
handlers = events[type];
if ( !handlers ) {
handlers = events[type] = [];
addListener( elem, type, eventHandle );
}
handlers.push( handleObj );
}
elem = null;
}
function evtHandle ( event ) {
event = fixEvent( event || window.event );
var handlers = ((dataManager.data(this, "events") || {})[event.type] || []).slice(0);
event.currentTarget = this;
for( var j = 0, l = handlers.length; j < l; j++ ) {
var handleObj = handlers[j];
event.handler = handleObj.handler;
event.data = handleObj.data;
event.handleObj = handleObj;
var ret = handleObj.handler.call( this, event );
if( ret !== undefined ) {
if( ret === false ) {
event.preventDefault();
event.stopPropagation();
}
}
if( event.isImmediatePropagationStopped() ) {
break;
}
}
}
function removeEvent( elem, types, handler ) {
// don't do events on text and comment nodes
if( elem.nodeType === 3 || elem.nodeType === 8 ) {
return;
}
if( handler === false ) {
handler = returnFalse;
}
var type, origType, i = 0, j,
elemData = dataManager.data( elem ),
events = elemData && elemData.events;
if( !elemData || !events ) {
return;
}
// Unbind all events for the element
if( !types ) {
types = types || "";
for ( type in events ) {
removeEvent( elem, type );
}
return;
}
// Handle multiple events separated by a space
// jQuery(...).unbind("mouseover mouseout", fn);
types = types.split(" ");
while( (type = types[ i++ ]) ) {
origType = type;
handleObj = null;
eventType = events[ type ];
if( !eventType ) {
continue;
}
if( !handler ) {
for ( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];
removeEvent( elem, origType, handleObj.handler );
eventType.splice( j--, 1 );
}
continue;
}
for( j = 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ];
if( handler === handleObj.handler ) {
// remove the given handler for the given type
eventType.splice( j--, 1 );
}
}
}
// remove generic event handler if no more handlers exist
if ( eventType.length === 0 ) {
delete events[ origType ];
}
// Remove the expando if it's no longer used
if ( isEmptyObject( events ) ) {
var handle = elemData.handle;
if ( handle ) {
handle.elem = null;
}
delete elemData.events;
delete elemData.handle;
if ( isEmptyObject( elemData ) ) {
dataManager.removeData( elem, 'events' );
}
}
}
function Event( src ) {
this.originalEvent = src;
this.type = src.type;
this.timeStamp = now();
}
Event.prototype = {
preventDefault: function() {
this.isDefaultPrevented = returnTrue;
var e = this.originalEvent;
if( e.preventDefault ) {
e.preventDefault();
}
e.returnValue = false;
},
stopPropagation: function() {
this.isPropagationStopped = returnTrue;
var e = this.originalEvent;
if( e.stopPropagation ) {
e.stopPropagation();
}
e.cancelBubble = true;
},
stopImmediatePropagation: function() {
this.isImmediatePropagationStopped = returnTrue;
this.stopPropagation();
},
isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse
};
function fixEvent( evt ) {
var props = "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),
len = props.length;
var originalEvent = evt;
evt = new Event(originalEvent);
for(var i = len, prop; i;) {
prop = props[ --i ];
evt[ prop ] = originalEvent[ prop ];
}
if(!evt.target) {
evt.target = evt.srcElement || document;
}
if( evt.target.nodeType === 3 ) {
evt.target = evt.target.parentNode;
}
if( !evt.relatedTarget && evt.fromElement ) {
evt.relatedTarget = evt.fromElement === evt.target ? evt.toElement : evt.fromElement;
}
if( evt.pageX == null && evt.clientX != null ) {
var doc = document.documentElement, body = document.body;
evt.pageX = evt.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
evt.pageY = evt.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
}
if( !evt.which && ((evt.charCode || evt.charCode === 0) ? evt.charCode : evt.keyCode) ) {
evt.which = evt.charCode || evt.keyCode;
}
if( !evt.metaKey && evt.ctrlKey ) {
evt.metaKey = evt.ctrlKey;
}
if( !evt.which && evt.button !== undefined ) {
evt.which = (evt.button & 1 ? 1 : ( evt.button & 2 ? 3 : ( evt.button & 4 ? 2 : 0 ) ));
}
return evt;
}
function bind ( el, type, fn, data ) {
var handler;
if( typeof type === "object" ) {
for( var key in type ) {
bind(el, key, type[key], data);
}
return;
}
handler = fn;
addEvent( el, type, handler, data );
}
function unbind ( el, type, fn ) {
if( typeof type === "object" ) {
for ( var key in type ) {
unbind( el, key, type[key] );
}
}else {
removeEvent( el, type, fn );
}
}
return {
data : dataManager.data,
removeData : dataManager.removeData,
bind : bind,
unbind : unbind
};
}(this);
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。