2012-12-07 34 views

回答

2

事件冒泡,所以它後面的對象仍然會收到點擊事件。只要忽略前面物體上的事件。但是,如果「在前面」的對象不是「在後面」的對象的孩子,將不起作用。就像拉帕爾所做的那樣,絕對定位元素就是這種情況。你唯一的解決辦法是使用http://robertnyman.com/css3/pointer-events/pointer-events.html

pointer-events: none; 

但是,這僅適用於最新的FF和Chrome

或者使用黑客像蘭迪·霍爾建議,你躲在被點擊的元素分派事件你的自我並使用elementFromPoint()

1

我有一個解決方案,只是建立了類似的東西。你需要做的是在可見的對象後面觸發一個事件。你需要在畫布後面的dom觸發事件,基本上?你需要爲跨瀏覽器制定出具體細節,但是這應該讓你開始

element.onclick = function (e){ //or however you're binding it 
    // create some function so that canvas.style = display none, so that it's not in the way 
    // then get the element at the click coordinates 
    var ghost = document.elementFromPoint(e.pageX, e.pageY); 
    //create an event 
    var evt = document.createEvent('Event'); 
    evt.initEvent(e.type, true, true); 
    //set x and y on evt to match the x and y that was used in the original click 
    evt.pageX = e.pageX; 
    evt.pageY = e.pageY; 
    //do the event on the element 
    ghost.dispatchEvent(evt); 
    // create some function to reshow the canvas. 
} 

或者,你可以隱藏特定的元素,如果你想點擊它背後的畫布上的東西。或者,如果在事件發生後不需要它,則可以完全摧毀畫布而不是隱藏畫布。

相關問題