您想将 mousemove 事件从非固定元素“冒泡”到具有不同父元素的固定元素吗?我认为您必须自己检查和触发:
为非固定元素构建一个包装器。这也获得了一个 mousemove 事件侦听器。如果 mousemove 在固定元素上(检查 clientX 和 clientY),则在固定元素上触发 mousemove 事件。
例如用 Firefox 测试:
function onCanvasMouseMove(oEvent) {
console.log(oEvent);
}
// wrapper mousemove handler:
// if the mouse is over the canvas, trigger mousemove event on it.
function onWrapperMouseMove(oEvent) {
if (
oEvent.clientX <= oCanvas.offsetWidth
&& oEvent.clientY <= oCanvas.offsetHeight
) {
oEvent.stopPropagation();
var oNewEvent = document.createEvent("MouseEvents");
oNewEvent.initMouseEvent("mousemove", true, true, window, 0, oEvent.screenX, oEvent.screenY, oEvent.clientX, oEvent.clientY, false, false, false, false, 0, null);
oCanvas.dispatchEvent(oNewEvent);
}
}
var oCanvas;
window.onload = function() {
oCanvas = document.getElementById('canvas');
oCanvas.addEventListener('mousemove', onCanvasMouseMove, false);
// add mousemove listener to none-fixed wrapper
var oWrapper = document.getElementById('wrapper');
oWrapper.addEventListener('mousemove', onWrapperMouseMove, false);
};
另请参阅此示例。
Ps:冒泡不是正确的词,它通常意味着将事件冒泡到父元素。