简体   繁体   English

mouseWheel 事件不起作用 Safari 画布

[英]mouseWheel event not working Safari canvas

I am trying to handle mouseWheel event on Safari.我正在尝试在 Safari 上处理mouseWheel事件。 Below you can see my full functional example hmtl5/js code, to test the mouseWheel event.您可以在下面看到我的完整功能示例 hmtl5/js 代码,以测试mouseWheel事件。

My code trace a rectangle at each mouseWheel event and the wheel event is caught to define the size of the rectangle.我的代码在每个mouseWheel事件处跟踪一个矩形,并捕获滚轮事件以定义矩形的大小。

My problem is my code works perfectly on Chrome and Firefox browser.我的问题是我的代码在 Chrome 和 Firefox 浏览器上完美运行。 But when it is tested on Safari browser event of wheel are not linear caught.但是当它在 Safari 浏览器上测试时,轮子的事件不是线性的。 In fact on Safari the event is called one time and after that there are no other wheel event sent.事实上,在 Safari 上,该事件被调用一次,之后没有其他轮事件发送。

 var c = document.getElementById("mon_canvas"); var ctx = c.getContext("2d"); ctx.canvas.width = window.innerWidth; ctx.canvas.height = window.innerHeight; var width = c.width; var height = c.height; var sizeRect = 100.0; drawRect(width/2 - sizeRect/2,height/2 - sizeRect/2,sizeRect ,sizeRect,'red'); c.addEventListener('mousewheel',function(event){ zoom(event); }); c.addEventListener('Wheel',function(event){ zoom(event); }); function zoom(event){ if( event.deltaY > 0 ) sizeRect -= 10; if( event.deltaY <= 0 )sizeRect += 10; drawRect(0,0,width,height,'white'); drawRect(width/2 - sizeRect/2,height/2 - sizeRect/2,sizeRect ,sizeRect,'red'); } function drawRect(px,py,sizeX,sizeY,color){ ctx.beginPath(); ctx.rect(px,py ,sizeX,sizeY); ctx.fillStyle = color; ctx.fill(); }
 * { margin:0; padding:0; }
 <canvas id="mon_canvas" ></canvas>

您可以使用此方法删除浏览器上的轮子并始终捕获事件。

window.onwheel = function(){ return false; }

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

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