首页 > 解决方案 > 如何使用滑动检测构建触摸手势?

问题描述

我想为我的 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() 放入 {},但这并没有改变任何东西。

标签: javascriptnpmelectron

解决方案


推荐阅读