var clickarray=[];function getcoo(thatdiv){thatdiv.find(".link").each(function(){var offset=$(this).offset();clickarray.unshift([(offset.left),(offset.top),(offset.left+$(this).width()),(offset.top+$(this).height()),($(this).attr('name')),1]);});}
$("body").click(function(event){event.preventDefault();//if it is a a-tagvar x=event.pageX;var y=event.pageY;var job="";for(var i in clickarray){if(x>=clickarray[i][0] && x<=clickarray[i][2] && y>=clickarray[i][1] && y<=clickarray[i][3] && clickarray[i][5]==1){job=clickarray[i][4];clickarray[i][5]=0;//set to allready clickedbreak;}}if(job.length>0){// --do some thing with the job --}});
$('.overlay').click(function(e){var left = $(window).scrollLeft();var top = $(window).scrollTop();
//hide the overlay for now so the document can find the underlying elements$(this).css('display','none');//use the current scroll position to deduct from the click position$(document.elementFromPoint(e.pageX-left, e.pageY-top)).click();//show the overlay again$(this).css('display','block');});
<div id="outerElement"><div id="canvas-wrapper"><canvas id="overlay"></canvas></div><!-- Omitted: element(s) behind canvas that should still be selectable --></div>
(outerElement、canvas-wrapper和canvas元素具有相同的大小。)
为了使画布后面的元素正常工作(例如可选择、可编辑),我使用了以下代码:
canvasWrapper.style.pointerEvents = 'none';
outerElement.addEventListener('mousedown', event => {const clickedOnElementInCanvas = yourCheck // TODO: check if the event *would* click a canvas element.if (!clickedOnElementInCanvas) {
// if necessary, add logic to deselect your canvas elements ...
wrapper.style.pointerEvents = 'none';return true;}
// Check if we emitted the event ourselves (avoid endless loop)if (event.isTrusted) {// Manually forward element to the canvasconst mouseEvent = new MouseEvent(event.type, event);canvas.dispatchEvent(mouseEvent);mouseEvent.stopPropagation();}return true;});
onCanvasModified(canvas, () => {const inputFieldInCanvasActive = // TODO: Check if an input field of the canvas is active.wrapper.style.pointerEvents = inputFieldInCanvasActive ? 'auto' : 'none';});