Hammer.js - Panstart事件不止一次发射

时间:2015-07-31 07:19:42

标签: hammer.js

为什么 panstart 事件的触发频率与 pan 一样频繁?是不是 panstart 应该每个手势只开一次?



var myElement = document.getElementById('myElement');
var myResult = document.getElementById('myResult');

// create a simple instance
var mc = new Hammer(myElement);

// setup pan gesture support
mc.get('pan').set({ event: 'panstart', direction: Hammer.DIRECTION_ALL });

// listen to events...
var timesFired = 0;
mc.on("panstart", function(ev) {
  timesFired++;
  myElement.textContent = ev.type;
    myResult.textContent = '# events fired: ' + timesFired;
});

#myElement {
  background: silver;
  height: 100px;
  text-align: center;
  font: 50px/100px Helvetica, Arial, sans-serif;
}

#myResult {
  text-align: center;
  font: 50px/70px Helvetica, Arial, sans-serif;
}

<script src="https://hammerjs.github.io/dist/hammer.js"></script>

<div id="myElement">(pan here)</div>
<div id="myResult"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

当你写这个

mc.get('pan').set({ event: 'panstart', direction: Hammer.DIRECTION_ALL });

你是说当检测到pan时应该触发名为panstart的事件。这就是为什么你会有这么多的暴徒。

修饰:

mc.get('pan').set({ **enable: true**, direction: Hammer.DIRECTION_ALL });

// listen to events...
var timesFired = 0;
mc.on("panstart pan", function(ev) {
  timesFired++;
  myElement.textContent = ev.type;
    myResult.textContent = '# events fired: ' + timesFired;
});