676
技術社區[雲棲]
百度有啊前端框架分析(瀏覽器內置事件)
事件是JavaScript中非常重要的一個內容,在百度有啊的前端框架中主要對事件分成了瀏覽器內置事件和自定義事件兩部分。
BBEvent下主要對瀏覽器內置事件進行了標準化。
target :事件目標對象
BBEvent.target = function(A) {
A = A || window.event;
return A.target || A.srcElement;
};
isLeftClick :判斷是否為鼠標左鍵點擊
BBEvent.isLeftClick = function(A) {
A = A || window.event;
return (((A.which) && (A.which == 1)) || ((A.button) && (A.button == 1)));
};
pageX :鼠標相對於整個頁麵的X軸的坐標
BBEvent.pageX = function(A) {
A = A || window.event;
return A.pageX || (A.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
};
pageY :鼠標相對於整個頁麵的y軸的坐標
BBEvent.pageY = function(A) {
A = A || window.event;
return A.pageY || (A.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
};
pagePosition :鼠標相對整個頁麵的坐標
BBEvent.pagePosition = function(A) {
A = A || window.event;
return {
x: BBEvent.pageX(A),
y: BBEvent.pageY(A)
};
};
layerX 鼠標相對於當前元素的X坐標(當前元素要position:absolute,否則會跟pageX相同)
BBEvent.layerX = function(A) {
A = A || window.event;
return A.layerX || A.offsetX;
};
layerY鼠標相對於當前元素的Y坐標(當前元素要position:absolute)
BBEvent.layerY = function(A) {
A = A || window.event;
return A.layerY || A.offsetY;
};
layerPosition 鼠標相對於當前元素的坐標(當前元素要position:absolute)
BBEvent.layerPosition = function(A) {
A = A || window.event;
return {
x: BBEvent.layerX(A),
y: BBEvent.layerY(A)
};
};
preventDefault 阻止瀏覽器默認動作的發生
BBEvent.preventDefault = function(A) {
A = A || window.event;
if (A.preventDefault) {
A.preventDefault();
} else {
A.returnValue = false;
}
};
stopPropagation 阻止事件冒泡
BBEvent.stopPropagation = function(A) {
A = A || window.event;
if (A.stopPropagation) {
A.stopPropagation();
} else {
A.cancelBubble = true;
}
};
observe 添加事件監聽器
stopObserving 刪除事件監聽器
fireEvent 觸發事件
CustEvent是對自定義事件服務的。
observe
stopObserving
fireEvent
最後更新:2017-04-02 04:01:42