简体   繁体   English

删除指针事件:none属性。 不工作

[英]Removing pointer-events : none property . Not working

I want to remove the pointer-events :none property when I right click . 我想在右键单击时删除指针事件:none属性。 that's what I actually tried . 那就是我真正尝试过的。

HTML 的HTML

<div class="wrapper">
<br />                         
  <div id="viewer2" class="viewer" style="width:800px; height:300px; position: absolute; z-index : 0;></div>
  <canvas id="paper" width="800" height="300" style="border:1px solid #ccc;position: absolute; z-index : 1;"></canvas>           
  <br />
</div>

Javascript Java脚本

$('.viewer').bind('contextmenu', function(e) {
   return false;
});
$('#paper').bind('contextmenu', function(e) {
   return false;
});
document.getElementById('paper').onmousedown =FuncOnClick;

function FuncOnClick(event) {
   console.log(event.which);
   switch (event.which) {
      case 1:
         document.getElementById("paper").style.pointerEvents = "none";
         alert('Left');
         break;
      case 3:
         document.getElementById("paper").style.pointerEvents = "";
         alert('Right ');
         break;
   }
}

What I'm trying is to check if it's right or left click and if it's right it click on the canvas and it's left it click on the div. 我正在尝试的是检查它是右键单击还是左键单击,如果正确,则单击画布,然后单击div。 But when I Right click the pointer-events is staying with 'none' value . 但是,当我右键单击时,指针事件保持为'none'值。

What can I do to easily remove the pointer-events attribute and do it before the click is fired (like I did in the code the click is going to click before the pointer-events is edited ?) 我该如何做才能轻松删除指标事件属性,并在触发点击之前执行该操作(就像我在代码中所做的那样,在指针事件编辑之前,点击将要单击?)

EDIT : Added Z-index to make it a bit clearlier 编辑:添加了Z索引,使其更加清晰

EDIT #2 编辑#2

My DIV contain an image that i can move with right click and zoom with mousewheel . 我的DIV包含一个我可以右键单击并使用鼠标滚轮缩放的图像。 My canvas is here because i want to draw some rectangle (region of interest) . 我的画布在这里是因为我想绘制一些矩形(感兴趣的区域)。 I want to draw these rectangle with right click . 我想用右键单击绘制这些矩形。 When i left click i can drag my image (div) When i right click i want to draw rectangle on (canvas) 当我单击鼠标左键时,我可以拖动图像(div)当我单击鼠标左键时,我想在(画布)上绘制矩形

So what i need is to be able to pass trough the canvas when i right click . 所以我需要的是当我右键单击时能够通过画布。

Depending on how you're planning on using this, you could try adding return false to the left click event. 根据您打算如何使用它,您可以尝试将return false添加到左击事件。 So the JavaScript would look like this: 因此,JavaScript如下所示:

$('.viewer').bind('contextmenu', function(e) {
   return false;
});
$('#paper').bind('contextmenu', function(e) {
   return false;
});

document.getElementById('paper').onmousedown = FuncOnClick;

function FuncOnClick(event) {
   console.log(event.which);
   switch (event.which) {
      case 1:
         document.getElementById("paper").style.pointerEvents = "none";

         setTimeout(function(){
            document.getElementById("paper").style.pointerEvents = "all";
         }, 500)

         alert('Left');

         break;
      case 3:
         document.getElementById("paper").style.pointerEvents = "all";
         alert('Right ');
         break;
   }
}

The left click event.which is still being console logged, but the alert won't run because of the return false . 左键单击event.whichevent.which仍在控制台中记录,但是由于return false ,因此警报不会运行。 Here's a JS Fiddle example. 这是一个JS Fiddle示例。

Answer updated following comments. 回答以下评论更新。

Try to document.getElementById('wrapper').onmousedown = FuncOnClick; 尝试document.getElementById('wrapper').onmousedown = FuncOnClick; set the listener on to the wrapper. 将监听器设置为包装器。

(don't forget to make an unique id instead of "wrapper") (不要忘记制作一个唯一的ID而不是“包装器”)

"paper" is initialized with pointer-events: none; “ paper”用指针事件初始化:none; so its not emitting any events. 因此它不会发出任何事件。

Here you go with a solution https://jsfiddle.net/usqmkbg2/ 在这里您可以找到解决方案https://jsfiddle.net/usqmkbg2/

 $('.viewer').bind('contextmenu', function(e) { return false; }); $('#paper').bind('contextmenu', function(e) { return false; }); document.getElementById('paper').onmousedown =FuncOnClick; document.getElementById('viewer2').onmousedown =FuncOnClick; function FuncOnClick(event) { console.log(event.which); switch (event.which) { case 1: document.getElementById("paper").style.pointerEvents = "none"; alert(event.target.id + ' Left'); $('#viewer2').css('z-index', '1'); $('#paper').css('z-index', '-1'); break; case 3: document.getElementById("paper").style.pointerEvents = ""; alert(event.target.id + ' Right '); break; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <br /> <div id="viewer2" class="viewer" style="width:800px; height:300px; position: absolute; z-index : 0;"></div> <canvas id="paper" width="800" height="300" style="border:1px solid #ccc;position: absolute; z-index : 1;"></canvas> <br /> </div> 

Hope this will help you. 希望这会帮助你。

Try this: 尝试这个:

HTML: HTML:

    <div id="wrapper">
<br />                         
  <div id="viewer2" class="viewer" style="width:800px; height:300px; position: absolute; z-index : 0;></div>
  <canvas id="paper" width="800" height="300" style="border:1px solid #ccc;position: absolute; z-index : 1;"></canvas>           
  <br />
</div>

JavaScript: JavaScript:

document.getElementById('wrapper').onmousedown =FuncOnClick;

function FuncOnClick(event, clickNeeded) {
   switch (event.which) {
     case 1:
        document.getElementById('viewer').onmousedown();
        break;
    case 3:
        document.getElementById('paper').onmousedown();
        break;
    }
}

And make the wrapper an transaparent overlay for both of the div. 并使包装器成为两个div的透明覆盖层。

Hope it is what you meant. 希望这就是你的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM