事件是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