【问题标题】:Mobile gestures in backbone.jsbone.js 中的移动手势
【发布时间】:2012-04-21 19:20:37
【问题描述】:

我可以在 Backbone.js 视图事件中使用滑动、点击、捏合等移动手势吗?更具体地说,以下是我的代码。

Backbone.View.extend({
     initialize:function(){
        //initialization 
     },
     Events:{
          "swipe-left #homeBtn":"homeSwipe"
     },
     homeSwipe:function(){
        alert("Event Swipe left triggered!");
     }
});

我可以使用滑动、向左/向右滑动、捏合、点击等移动手势来使用backbone.js吗?

【问题讨论】:

    标签: events jquery-mobile backbone.js


    【解决方案1】:

    下载并包含Hammer.js,然后像平常一样使用 Backbone 视图事件!

    events:{
        'swipe': 'onSwipe'
    },
    
    initialize: function(){
        // I think you can get away doing this here once, but I have not tested.
        // If not, just move it to the `render` method
        new Hammer(this.el);
    },
    
    onSwipe: function(e){
        console.log(e.direction); // left or right
    }
    

    另外,你可以看看我的简单Backbone view Gist

    更新

    根据反馈,看起来必须在主干视图上调用 new Hammer(this.el) 才能使其正常工作。我已更新示例以反映这一点。

    【讨论】:

    • 这就像一个魅力。唯一需要添加的是 - 应添加hammer.js 和jquery 之间的“适配器”以使此代码正常工作。可以在这里找到:raw.github.com/EightMedia/hammer.js/master/…
    • 为此,我必须在我的视图的render() 方法中像this.$el.hammer() 一样在我的视图的$el 上调用hammer() 函数。我从this answer 得到这个,虽然我确实需要the jquery plugin 才能工作
    • @AlexandreBourlier - 我更新了我的答案以显示您所说的要求。我想我已经提供了一个不需要 jquery 插件的示例。
    【解决方案2】:

    Backbone relies on jQuery.bind 来管理 DOM 事件。

    所以问题是如果 jQuery 支持这些事件并且看起来像 jQuery Mobile does,那么现在你必须检查 how to integrate jQuery Mobile and Backbone

    【讨论】:

    • Que hondas @gguillen,老实说我不这么认为。滑动效果似乎可以直接针对el,但没有一个子div
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    相关资源
    最近更新 更多