javascript - 如何使用滑动检测构建触摸手势?
问题描述
我想为我的 Electron 应用程序创建触摸手势,例如在 Chromium 中。它应该只做两件简单的事情:当我向左滑动时加载下一页,当我向右滑动时加载上一页。
我找到了这个 npm 包https://www.npmjs.com/package/swipe-detect,它似乎可以做到。它需要一个 TARGET_DOMAIN_NODE,我猜它是屏幕,一个 CALLBACK,我猜它是在滑动时调用的函数和滑动长度的阈值。
我想用
import swipeDetect from 'swipe-detect';
在 main.js 中,但我收到了一个意外的标识符错误。所以我想我必须写
const swipeDetect = require ('swipe-detect');
至于其他包。
swipeDetect 输出左、右、上或下,所以我写了这个简单的函数并将它放在 main.js 的顶部
function swipeHandler (direction) {
if(direction == 'left') {
parent.document.getElementById(1).contentWindow.history.forward();
}
if(direction == 'right') {
parent.document.getElementById(1).contentWindow.history.back();
}
}
然后对于 swipeDetect 我把
swipeDetect(electron.screen.getPrimaryDisplay(), swipeHandler(), 150);
但我越来越
export default function(target, callback, threshold=150) {
^^^^^^
SyntaxError: Unexpected token export
at Module._compile (internal/modules/cjs/loader.js:752:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)
at Module.load (internal/modules/cjs/loader.js:677:32)
at tryModuleLoad (internal/modules/cjs/loader.js:609:12)
at Function.Module._load (internal/modules/cjs/loader.js:601:3)
at Module.require (internal/modules/cjs/loader.js:715:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object.<anonymous> (C:\folder\main.js:17:21)
at Module._compile (internal/modules/cjs/loader.js:808:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)
估计格式不对。已经尝试将 electron.screen.getPrimaryDisplay() 放入 {},但这并没有改变任何东西。
解决方案
推荐阅读
- c - 如果字符串包含所有数字,则 C 代码输出 yes,如果它包含除数字以外的其他内容,则输出 no
- android - RecyclerView - 自定义 DefaultItemAnimator 问题
- flutter - 带有可点击卡片 back4app 的 Flutter Future Builder
- python - X 和 Y 轴上的 2 个彩条
- text - QNX 屏幕渲染文字
- list - 为什么 list 应该先转换为 RDD 再转换为 Dataframe?有什么方法可以将列表转换为数据框?
- algorithm - 为上述场景确定最简单的解决方案
- reactjs - 在打字稿中为 MuiPickersUtilsProvider 组件创建反应容器组件
- javascript - 为什么第一行 `import jQuery from "../core.js";` 包含在 `load.js` 中,即使它已经包含在 `jquery.js` 中
- amazon-web-services - AWS Amplify 无法从函数指令恢复为连接指令