HammerJS事件属性未定义。

7

我正在开发这个小网站:Website;并且我正在使用HammerJS作为触摸支持库。

它似乎对事件做出了响应,并且识别event.type属性,但是当我尝试获取drag事件的event.direction或其他相关属性时,控制台没有输出任何结果(我在控制台中记录结果)。

这是我监听drag事件的方式:

Application.ApplicationController.prototype.Drag = function(selector, delay, callback) {
    return $(selector).on('drag', _.debounce(function(event){
        event.preventDefault();
        return (typeof callback === 'function' && callback !== undefined) ? callback.apply( event, [ event ] ) : 'Argument : Invalid [ Function Required ]';
    }, delay)); 
};

我将其称为类似于:

this.Drag(selector, delay, function(event) {
    console.log(event.type, event.direction);
});

请问有人能告诉我我做错了什么或者是否遗漏了什么吗?

编辑:我刚刚用旧版本的jquery.hammer.js替换了jQuery库:jquery.specialevents.hammer.js,现在似乎对所有事件都有响应,并获得了所有应该获得的属性。但我仍想知道为什么我尝试使用的那个不起作用?

编辑:我已经找到了问题的根本原因,我的代码依赖一些库,我正在使用Yepnope脚本加载器异步加载这些库,所以在某个地方,它没有加载所有库(包括hammer.js的jQuery插件),其中一些库丢失了 :) 我已经解决了这个问题,现在事件具有它们应该具有的属性。


1
我得到了event.type的记录值,但是event.direction的值未定义。 - Asad Saeeduddin
这正是我得到的...这也是我在这里提问的原因 :) - Roland
所有这对我来说意味着你的代码很好,事件对象只是没有方向属性。 - Asad Saeeduddin
你尝试在使用.on之前在方法链中添加.hammer()了吗?就像这样 $(selector).hammer().on(... - Asad Saeeduddin
让我们在聊天中继续这个讨论。 (http://chat.stackoverflow.com/rooms/22464/hammerjs-event-properties-are-undefined) - Asad Saeeduddin
显示剩余3条评论
2个回答

9

我仍然想知道为什么我尝试使用的那个不起作用?

了解 jquery.specialevent.hammer.jsjquery.hammer.js 之间的区别应该有助于理解问题。创建 jquery.specialevent.hammer.js 的 Damien 解释了原因

However Eight Media decided to create their own namespace in jQuery to activate the events.

$("#element").hammer({ /* options */ }).on("tap", function(ev) { 
    console.log(ev); 
}); 

So in the end they are not using the default jQuery eventing system. That means my existing source code which used jQuery mobile events has to be change. That’s why I decided to implement the use of Hammer.JS with the jQuery special eventing API.

$("#element").on("tap", { /* options */ }, function(ev) {
    console.log(ev); 
}); 

I put jquery.specialevent.hammer.js onto my Github where you can also find a demo. Maybe Eight Media accepts my pull request and it will be part of Hammer.JS.


如问题描述中所述,我已找到导致所述行为的问题。另外,我也阅读了引入jquery.specialevent.hammer.js到项目中的原因 :) 无论如何,感谢您的答案 :) - Roland

5

event.gesture.direction 应该能够得到您想要的结果。


3
虽然这可能有助于他人-我花了几个小时搜索event.direction问题。 - Darren Cooney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接