使用滑动手势来更改多页jQuery移动应用程序中的页面

时间:2014-05-22 03:24:21

标签: javascript jquery jquery-mobile swipe

有没有办法在jQuery Mobile多页面模板移动网站/应用程序上实现滑动手势导航?

我可以按如下方式组合一个简单的简单构造:

$("body").bind("swipeleft", function(e) {
$.mobile.changePage( 'about.html', { transition: "slide" });

但是当我开始使用锚标签(多页JQM样式)时,该事件不起作用:

$("body").bind("swipeleft", function(e) {
$.mobile.changePage( '#points2', { transition: "slide" });

是否有合适的解决方法或我是否必须放弃多页设置才能使其正常工作?

1 个答案:

答案 0 :(得分:20)

工作示例:http://jsfiddle.net/Gajotres/JB22E/3/

HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Share QR</title>
    <meta name="viewport" content="width=device-width,height=device-height,minimum-scale=1,maximum-scale=1"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" /> 
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</head>

<body>
  <div data-role="page" id="article1">
    <div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
      <h1>Articles</h1>
    </div>
    <div data-role="content">
      <p>Article 1</p>
    </div>
  </div>

  <div data-role="page" id="article2">
    <div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
      <a href="#article1" data-icon="home" data-iconpos="notext">Home</a>
      <h1>Articles</h1>
    </div>
    <div data-role="content">
      <p>Article 2</p>
    </div>
  </div>

  <div data-role="page" id="article3">
    <div data-role="header" data-theme="b" data-position="fixed" data-id="footer">
      <a href="#article1" data-icon="home" data-iconpos="notext">Home</a>
      <h1>Articles</h1>
    </div>
    <div data-role="content">
      <p>Article 3</p>
    </div>
    </div>

</body>
</html>

的JavaScript:

$(document).on('swipeleft', '.ui-page', function(event){    
    if(event.handled !== true) // This will prevent event triggering more then once
    {    
        var nextpage = $.mobile.activePage.next('[data-role="page"]');
        // swipe using id of next page if exists
        if (nextpage.length > 0) {
            $.mobile.changePage(nextpage, {transition: "slide", reverse: false}, true, true);
        }
        event.handled = true;
    }
    return false;         
});

$(document).on('swiperight', '.ui-page', function(event){     
    if(event.handled !== true) // This will prevent event triggering more then once
    {      
        var prevpage = $(this).prev('[data-role="page"]');
        if (prevpage.length > 0) {
            $.mobile.changePage(prevpage, {transition: "slide", reverse: true}, true, true);
        }
        event.handled = true;
    }
    return false;            
});