自己的js工具 Event封裝
更新時間:2009年08月21日 01:46:18 作者:
說到瀏覽器中的event,相信不少人都很頭疼,ie的event大部分時候都可以獲取到
因為ie的event是全局的而firefox的event是局部的,用起來不太方便,這個時候我們就要自己組裝一下常用的event操作了,封裝成類便于重用
/**
類 Event
用法:
Event.getEvent();獲取 ie,firefox的event
Event.getTarget();獲取ie的srcElement或firefox的target
Event.isIe();是否為ie
Event.clientX(); 獲取ie,fox的鼠標x坐標
Event.clientY();獲取 ie,fox的鼠標y坐標
*/
var Event=new function(){
this.toString=function(){
return this.getEvent();
}
//獲取 事件
this.getEvent=function(){
var ev=window.event;
if(!ev){
var c=this.getEvent.caller;
while(c){
ev=c.arguments[0];
if(ev && Event ==ev.constructor)
break;
c=c.caller;
}
}
return ev;
};
//獲取 事件源
this.getTarget=function(){
var ev=this.getEvent();
return this.isIe()?ev.srcElement:ev.target;
}
//是否為ie
this.isIe=function(){
return document.all?true:false;
}
//鼠標x坐標
this.clientX=function(){
var ev=this.getEvent();
var x=this.isIe()?ev.clientX:ev.pageX;
return x;
}
//鼠標y坐標
this.clientY=function(){
var ev=this.getEvent();
var y=this.isIe()?ev.clientY:ev.pageY;
return y;
}
/**增加事件(對象,事件類型,函數(shù)指針 )
obj: html對象
sEvent: 事件名稱
spNotify: 事件執(zhí)行的方法
isCapture:是否允許全屏捕捉
*/
this.addEvent=function(obj,sEvent,fpNotify,isCapture){
sEvent=sEvent.indexOf("on")!=-1?sEvent:"on"+sEvent;
if(obj.addEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2);
obj.addEventListener(sEvent,fpNotify,isCapture);
}else{ //ie
if(isCapture)
obj.setCapture(isCapture);
obj.attachEvent(sEvent,fpNotify);
}
}
//移除事件
this.removeEvent=function(obj,sEvent,fpNotify){
if(obj.removeEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2)
obj.removeEventListener(sEvent,fpNotify,false);
}else{
obj.detachEvent(sEvent,fpNotify);
}
}
//獲取鼠標按鍵,left=1,middle=2,right=3
this.button=function(){
var ev=this.getEvent();
if(!ev.which&&ev.button){//ie
return ev.button&1?1:(ev.button&2?3:(ev.button&4?2:0))
}
return ev.which;
};
//阻止事件冒泡傳遞
this.stopPropagation=function(){
var ev=this.getEvent();
if(this.isIe)
ev.cancelBubble=true;
else
ev.stopPropagation();
}
//阻止默認事件返回
this.preventDefault=function(){
var ev=this.getEvent();
if(this.isIe)
ev.returnValue=false;
else
ev.preventDefault();
}
}
復制代碼 代碼如下:
/**
類 Event
用法:
Event.getEvent();獲取 ie,firefox的event
Event.getTarget();獲取ie的srcElement或firefox的target
Event.isIe();是否為ie
Event.clientX(); 獲取ie,fox的鼠標x坐標
Event.clientY();獲取 ie,fox的鼠標y坐標
*/
var Event=new function(){
this.toString=function(){
return this.getEvent();
}
//獲取 事件
this.getEvent=function(){
var ev=window.event;
if(!ev){
var c=this.getEvent.caller;
while(c){
ev=c.arguments[0];
if(ev && Event ==ev.constructor)
break;
c=c.caller;
}
}
return ev;
};
//獲取 事件源
this.getTarget=function(){
var ev=this.getEvent();
return this.isIe()?ev.srcElement:ev.target;
}
//是否為ie
this.isIe=function(){
return document.all?true:false;
}
//鼠標x坐標
this.clientX=function(){
var ev=this.getEvent();
var x=this.isIe()?ev.clientX:ev.pageX;
return x;
}
//鼠標y坐標
this.clientY=function(){
var ev=this.getEvent();
var y=this.isIe()?ev.clientY:ev.pageY;
return y;
}
/**增加事件(對象,事件類型,函數(shù)指針 )
obj: html對象
sEvent: 事件名稱
spNotify: 事件執(zhí)行的方法
isCapture:是否允許全屏捕捉
*/
this.addEvent=function(obj,sEvent,fpNotify,isCapture){
sEvent=sEvent.indexOf("on")!=-1?sEvent:"on"+sEvent;
if(obj.addEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2);
obj.addEventListener(sEvent,fpNotify,isCapture);
}else{ //ie
if(isCapture)
obj.setCapture(isCapture);
obj.attachEvent(sEvent,fpNotify);
}
}
//移除事件
this.removeEvent=function(obj,sEvent,fpNotify){
if(obj.removeEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2)
obj.removeEventListener(sEvent,fpNotify,false);
}else{
obj.detachEvent(sEvent,fpNotify);
}
}
//獲取鼠標按鍵,left=1,middle=2,right=3
this.button=function(){
var ev=this.getEvent();
if(!ev.which&&ev.button){//ie
return ev.button&1?1:(ev.button&2?3:(ev.button&4?2:0))
}
return ev.which;
};
//阻止事件冒泡傳遞
this.stopPropagation=function(){
var ev=this.getEvent();
if(this.isIe)
ev.cancelBubble=true;
else
ev.stopPropagation();
}
//阻止默認事件返回
this.preventDefault=function(){
var ev=this.getEvent();
if(this.isIe)
ev.returnValue=false;
else
ev.preventDefault();
}
}
相關文章
Javascript attachEvent傳遞參數(shù)的辦法
找了半天找到的解決辦法,看介紹說是javascript的閉包問題,導致得不能直接讀取外部的那個函數(shù),不然就所有傳遞的參數(shù)都變?yōu)樽詈笠粋€了。2009-12-12微信小程序利用swiper+css實現(xiàn)購物車商品刪除功能
這篇文章主要介紹了微信小程序利用swiper+css實現(xiàn)購物車商品刪除功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03JavaScript與Image加載事件(onload)、加載狀態(tài)(complete)
以前寫過一個圖片等比縮放的Js函數(shù),缺陷是要等到所有圖片都加載完畢了,才能進行等比縮放。2011-02-02