【问题标题】:Using swipe gestures to change pages in multi-page jQuery mobile application使用滑动手势在多页 jQuery 移动应用程序中更改页面
【发布时间】:2014-07-10 22:19:56
【问题描述】:

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

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

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

但是当我开始使用锚标签(多页JQM风格)的那一刻,事件不起作用:

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

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

【问题讨论】:

    标签: javascript jquery jquery-mobile swipe


    【解决方案1】:

    工作示例: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;            
    });
    

    【讨论】:

    • 爱它!很棒的解决方案。但是有没有办法选择哪些页面可以进行滑动导航?我希望该操作仅适用于某些页面子集。
    • 改变这个: $(document).on('swipeleft', '.ui-page', function(event){ 例如 $(document).on('swipeleft', ' #page1, #page3, #page5 ....', function(event){ 或者只为可扫描页面添加一个特殊的类名
    • 它是否可以与 Todd Thompson 的子页面插件一起使用? github.com/ToddThomson/jQuery-Mobile-Subpage-Widget 在代码审查之后,我的 DOM 似乎太臃肿了....
    • 对我不起作用。一开始就输出3页
    • 嗨@Gajotres 感谢您的回答!我想知道,您如何使第二页成为默认起始页?这样当您打开应用程序时,您既可以向左滑动到第 1 页,也可以向右滑动到第 3 页?再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多