當使用者向左滑動的時候,菜單應該關閉在官網看到了一個簡單的樣本,然而並不是用於這個菜單,等到我完成之後我才知道:為什麼不用於菜單?
找到了這樣一個符合功能的庫,雖然知道要寫這個功能也不難。相比於自己寫這個庫,還不如用別人維護了一些時候的庫來得簡單、穩定。
jQuery Plugin to obtain touch gestures from iPhone, iPod Touch and iPad, should also work with Android mobile phones (not tested yet!)
然而,它並不會其他一些裝置上工作。
添加jQuery Touchwipe添加到requirejs的配置中:
require.config({ baseUrl: 'lib/', paths: { jquery: 'jquery-2.1.1.min', router: '../router', touchwipe: 'jquery.touchwipe.min' }, shim: { touchwipe: [jquery], underscore: { exports: '_' } }});require(['../app'], function(App){ App.initialize();});
(註:上面的代碼中暫時去掉了一部分無關本文的,為了簡單描述。)
接著,添加下面的代碼添加到app.js的初始化方法中
$(window).touchwipe({ wipeLeft: function() { $.sidr('close'); }, wipeRight: function() { $.sidr('open'); }, preventDefaultEvents: false });
就變成了我們需要的代碼。。
define([ 'jquery', 'underscore', 'backbone', 'router', 'jquerySidr', 'touchwipe'], function($, _, Backbone, Router){ var initialize = function(){ $(window).touchwipe({ wipeLeft: function() { $.sidr('close'); }, wipeRight: function() { $.sidr('open'); }, preventDefaultEvents: false }); $(document).ready(function() { $('#sidr').show(); $('#menu').sidr(); $(#sidr li a ).bind('touchstart click', function() { if(null != Backbone.history.fragment){ _.each($(#sidr li),function(li){ $(li).removeClass() }); $('a[href$=#/'+Backbone.history.fragment+']').parent().addClass(active); $.sidr('close'); window.scrollTo(0,0); } }); }); Router.initialize(); }; return { initialize: initialize };});
便可以實現我們需要的