Nikola
Nikola

Reputation: 15038

jQuery Mobile changePage with swipe transition

I can't seem to make the "reverse" slide effect while handling the "swiperight" event. So, the code below works fine but I would like when I make the "swiperight" that the next page would slide in from the left side and not right hand side. I did search the documentation and added the reverse: true as as it recomends in to the "swiperight":

$.mobile.changePage("#page"+nextPage, {transition : "slide", reverse:true});

but this does not provide the wanted effect. Can you point out where am I doing it wrong?

I have the following code on jsFiddle:

html:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery Mobile Application</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>    
</head>

<body>
    <section id="page1" data-role="page">
        <header data-role="header"><h1>jQuery Mobile</h1></header>

        <div data-role="content" class="content">
            <p>First page!</p>
        </div>
        <footer data-role="footer"><h1>O'Reilly</h1></footer>
    </section>

    <section id="page2" data-role="page">
        <header data-role="header"><h1>jQuery Mobile</h1></header>

        <div data-role="content" class="content">
            <p>Second page!</p>
        </div>
        <footer data-role="footer"r><h1>O'Reilly</h1></footer>
    </section>

    <section id="page3" data-role="page">
        <header data-role="header"><h1>jQuery Mobile</h1></header>

        <div data-role="content" class="content">
            <p>Third page!</p>
        </div>
        <footer data-role="footer"><h1>O'Reilly</h1></footer>
    </section>    
</body>
</html>​

jQuery:

(function($) {
    var methods = {
        init : function(options) {
            var settings = {
                callback: function() {}
            };

            if ( options ) {
                $.extend( settings, options );
                }

            $(":jqmData(role='page')").each(function() {
                $(this).bind("swiperight", function() {
                    var nextPage = parseInt($(this).attr("id").split("page")[1]) - 1;
                    if (nextPage === 0) 
                        nextPage = 3;

                    $.mobile.changePage("#page"+nextPage, "slide");
                    });                        

                $(this).bind("swipeleft", function() {
                    var nextPage = parseInt($(this).attr("id").split("page")[1]) +1;
                    if (nextPage === 4) 
                        nextPage = 1;

                    $.mobile.changePage("#page"+nextPage, "slide");
                });
            })
        }
        }

    $.fn.initApp = function(method) {
        if ( methods[method] ) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } 
        else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } 
        else {
            $.error( 'Method ' + method + ' does not exist' );
        }
    }
    })(jQuery);

$(document).ready(function(){
    $().initApp();
});
​

Upvotes: 1

Views: 14292

Answers (1)

Phill Pafford
Phill Pafford

Reputation: 85308

OK first off you're using a Alpha version of jQM and the docs you are referring to a for jQM 1.1.1. I've updated your jsfiddle to use the latest jQM 1.2

And I've added the correct syntax for the reverse swipe transition

$.mobile.changePage("#page"+nextPage, {
        transition: "slide",
        reverse: false
    });
});

and the reverse transition

$.mobile.changePage("#page"+nextPage, {
        transition: "slide",
        reverse: true
    });
}); 

Upvotes: 2

Related Questions