【问题标题】:Wheel event not propagating车轮事件未传播
【发布时间】:2019-05-15 18:06:51
【问题描述】:

我正在编写一个 Chrome 扩展程序,以便在用户查看 Google 地图中的全景图像时执行,并且我在将车轮运动传播到 Google 地图时遇到问题。我希望它能够传播,以便用户可以在我的扩展程序中缩放图像。

我试图在我的注入脚本中完全忽略该事件,但这不起作用。然后我尝试捕获该事件,然后通过 JavaScript 将其传递,但​​这也不起作用。 事件总是被捕获,但传递它不起作用。

当光标离开我的扩展程序的范围时(因为鼠标快速移动到我的“div”之外),滚轮事件正在正确地传递给谷歌地图。

我已经尝试过捕获

  expandingBox = document.createElement('div');
  expandingBox.addEventListener('wheel', mouseWheel, false);

和

  expandingBox = document.createElement('div');
  expandingBox.addEventListener('wheel', mouseWheel, true);

和

  $(document).addEventListener('wheel', mouseWheel, true);

和

  $(document).addEventListener('wheel', mouseWheel, false);

捕捉功能很简单:

  function mouseWheel( e ) {

       // alert("Wheel was moved");
        $(document).trigger("wheel", e);

        return true;

      }

我还尝试在函数 mouseWheel 中使用“return false”、“$(window)”和“$(window.parent)”。一切都无济于事。

有没有可能做我想做的事?

测试此功能的最小实现是:

manifest.json

{
  "name": "Wheel Test",
  "version": "0.3",
  "description": "See if I can read and pass a Wheel event",
  "background": {
    "persistent": false,
    "scripts": ["background.js", "jquery-3.3.1.min.js" ]
  },

  "default_locale": "en",

  "permissions": [
    "activeTab",
    "notifications",
    "<all_urls>"
  ],

  "browser_action":{
    "default_icon": {
      "20": "data/icons/QRC-Ninja-20.png"
    }
  },

  "manifest_version": 2
}

background.js

"use strict";

function injectCode( tab ){

  chrome.tabs.insertCSS(tab.id, { file: 'data/inject/inject.css' } );
  chrome.tabs.executeScript(tab.id , { file: 'jquery-3.3.1.min.js'});
  chrome.tabs.executeScript(tab.id , { file: 'data/inject/inject.js'});

}
// Listen for a click on the icon. On that click, inject the necessary code and get going.
chrome.browserAction.onClicked.addListener( injectCode );

注入.css

.expanding-box {
  box-sizing: border-box;
  position: fixed;
  z-index: 2147483645;
  border: 0px solid;
  width: 0;
  height: 0;
}

注入.js

'use strict';

var guide = window.guide;
var expandingBox = null;

try {

  guide.remove();

} catch (e) {}

guide = (function () {

  function stop(){

    guide.remove();

  }

  function mouseWheel( e ) {

//        alert("Wheel was moved");

        var result = expandingBox.dispatchEvent( e );

        return true;

  }

  return {
    install: function () {  
      expandingBox = document.createElement('div');
      expandingBox.setAttribute('class', 'expanding-box');
      expandingBox.setAttribute('id', 'expandingBox'); 
      document.body.appendChild(expandingBox);
      //
      // Initial position is the top left of the window
      //
      expandingBox.style.borderWidth =  '4px';
      expandingBox.style.borderColor = 'red';
      expandingBox.style.width = '200px';
      expandingBox.style.height = '200px';

      expandingBox.addEventListener('wheel', mouseWheel, false);
      document.addEventListener('mouseup', stop, false);
    },
    remove: function () {
      expandingBox.removeEventListener('wheel', mouseWheel, false);
      document.removeEventListener('mouseup', stop, false);
      if (expandingBox && expandingBox.parentNode) {
        expandingBox.parentNode.removeChild(expandingBox);
      }

    }
  };
})();

guide.install();

关于“目的是什么?”的问题代码的目的是分析用户指向的感兴趣区域。

提前感谢您的帮助!

与此同时,我发现 fotijr 提供了一个有效的答案。

【问题讨论】:

  • 您的代码并没有告诉我们太多信息。例如,您没有显示 div 被添加到任何内容,因此立即响应应该是“您尚未将 div 添加到任何内容”。 (我怀疑你有)。请创建一个 minimal reproducible example 来证明您的问题。
  • 目的是什么?捕获一个事件,然后在处理程序中再次触发相同的事件没有多大意义..?对我来说看起来像是一个无限的事件循环,但这可能是由于提供的代码的 sn-ps 太短。 wheel event 是怎么回事,它在冒泡,但你是否真的将新创建的 div 附加到 DOM 中?
  • 尝试使用标准的dispatchEvent() 而不是jQuery。
  • 很难从您的描述和代码示例中看出,但这可能是样式问题。如果您试图忽略一个元素上的鼠标事件并让它们“传递”到元素后面/下方的元素,请将元素的样式设置为 pointer-events: none;。
  • fotijr - 谢谢!!!这正是问题所在!

标签: javascript google-maps events google-chrome-extension propagation


【解决方案1】:

来自 fotijr 的关于使用 CSS 属性“pointer-events: none;”的评论让事件通过。不幸的是,现在我需要的鼠标跟踪丢失了....

我认为不可能做我正在尝试的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多