简体   繁体   中英

JavaScript not working for sliding the pages

I am following the jsfiddle . The code sample is working fine on jsfiddle, However I am unable to make it to work in my cordova mobile app.

I have placed the javascript code in home.js in js folder.

home.js:

$(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;
});

home.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 type="text/javascript" src="js/home.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>

I can't swipe to change the pages. Could someone point me in right direction ?

似乎您缺少HTML中的“ ui-page”类。

<div class="ui-page" data-role="page" id="article1">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM