【发布时间】:2017-05-14 07:12:43
【问题描述】:
我正在开发一个Cordova 应用程序(使用backbone.js 和requirejs)并且我正在使用toe.js js 库(https://github.com/dantipa/toe.js)来扩展jQuery 和backbone.js 事件,以便有一些移动手势,如swipe 和tap。
tap 和 swipe 手势效果很好,但我需要 drag 事件(pan 手势),所以我尝试像这样编辑库:
(function ($, touch, window, undefined) {
var namespace = 'drag';
var cfg = {
distance: 5,
direction: 'all'
};
touch.track(namespace, {
touchstart: function (event, state, start) {
state[namespace] = {
finger: start.point.length
};
},
touchmove: function (event, state, move) {
// if another finger was used then increment the amount of fingers used
state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger;
},
touchend: function (event, state, end) {
var opt = $.extend(cfg, event.data),
duration,
distance;
// calc
duration = touch.calc.getDuration(state.start, end);
distance = touch.calc.getDistance(state.start.point[0], end.point[0]);
// check if the swipe was valid
if (distance > opt.distance) {
state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]);
state[namespace].direction = touch.calc.getDirection(state[namespace].angle);
state[namespace].distance = distance;
// fire if the amount of fingers match
if (opt.direction === 'all' || state[namespace].direction === opt.direction) {
$(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace])));
}
}
}
});
}(jQuery, jQuery.toe, this));
我不确定这是重现 drag 事件的好方法,所以你能帮我解决这个问题吗?
我需要那个事件来重现移动侧边栏菜单滑动效果:
我该怎么做?
附:我尝试将toe.js 替换为具有所有手势事件的hammer.js (https://hammerjs.github.io/),但在使用requirejs 将其与backbone.js 事件集成时遇到了问题。
谢谢
【问题讨论】:
标签: javascript jquery backbone.js requirejs gesture