Using swipe gestures to change pages in multi-page jQuery mobile application
Asked Answered
C

1

8

Is there a way to implement swipe gesture navigation on a jQuery Mobile multi-page template mobile website/application?

I can put together a deadset easy construct as follows:

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

But the moment I start using anchor tags (multi-page JQM style), the event does not work:

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

Is there a suitable workaround for this or would I have to abandon the multi-page setup to get this to work?

Chilpancingo answered 22/5, 2014 at 3:24 Comment(0)
P
23

Working example: 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;            
});
Pash answered 22/5, 2014 at 6:38 Comment(6)
Love it! Fantastic solution. But is there a way to be selective about which pages can do swipe navigation? I want the action to only work on certain subsets of pages.Chilpancingo
Change this: $(document).on('swipeleft', '.ui-page', function(event){ to this for example $(document).on('swipeleft', '#page1, #page3, #page5 ....', function(event){ or add a special class name only to sweepable pagesPash
Any chance that it works with Todd Thompson's sub page plugin? github.com/ToddThomson/jQuery-Mobile-Subpage-Widget After code review it would seem my DOM is too bloated....Chilpancingo
Does not work for me. All 3 pages are outputted at the beginPostilion
Hi @Pash thanks for this answer! I was wondering, how do you make the second page the default starting page? So that when you open the app you can either swipe left to page 1, or swipe right to page 3? Thanks again.Odysseus
@Gajotres: short, clear and briefly! Amazing. QWorks also with jQm 1.4.5. Salute those from Croatia.Unesco

© 2022 - 2024 — McMap. All rights reserved.