Hammer.JS桌面文本选择和滑动冲突

Hammer.JS desktop text selection and swipe conflict

我有一个 Hammer.js 2.06 的工作实现,可以使用水平滑动在页面之间跳转。但是我在桌面上发现有冲突。当用户使用鼠标选择一段文本时,该操作还会触发 Hammer 的滑动事件。

我想允许文本选择的默认浏览器行为,但也想使用 Hammer 的滑动手势。有没有办法让两者一起工作?或者我应该禁用鼠标手势支持,因为文本选择功能更重要,我该怎么做?

delete Hammer.defaults.cssProps.userSelect // as suggested in tips 'n tricks
var myElement = document.getElementById("gestureCanvas"),
  nextp = document.getElementById("GotoNextPage"),
  prevp = document.getElementById("GotoPrevPage"),
  mc = new Hammer(myElement, {
    doubletap: false,
    pan: false,
    press: false,
    pinch: false,
    rotate: false,
    tap: false
  });
// gesture event listeners
mc.on("swipeleft swiperight", function(ev) {
  if (ev.type === "swipeleft") {
    if (Boolean(nextp.disabled) === false) {
      nextp.click();
    }
  } else if (ev.type === "swiperight") {
    if (Boolean(prevp.disabled) === false) {
      prevp.click();
    }
  }
});

默认情况下,Hammer 管理器处理触摸和鼠标输入。您可以通过设置 inputClass 选项指示它仅处理触摸输入:

mc = new Hammer(element, { inputClass: Hammer.TouchInput });

这样它仍然会检测到滑动手势,但不会影响文本选择等鼠标操作。

接受的解决方案对我不起作用,所以我用谷歌搜索,这就是我找到的。

“I can’t select my text anymore!”

Hammer is setting a property to improve the UX of the panning on desktop. Regularly, the desktop browser would select the text while you drag over the page. The user-select css property disables this.

If you care about the text-selection and not so much about the desktop experience, you can simply remove this option from the defaults. Make sure you do this before creating an instance.

delete Hammer.defaults.cssProps.userSelect;

http://hammerjs.github.io/tips/