3

我在单个页面中创建了多个 div 滑动,以便页面保持不变但 div 被更改。使用 jquery 移动页面滑动的帮助并进行了一些自定义。div 滑动工作完全正常,但我创建了一个面板,每当我从第一个 div 转到第二个 div 并再次回到第一个 div 时,面板不起作用。请帮我处理面板。这是我的代码:-

<!DOCTYPE html> 
<html>
<head>
    <title>Index</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="jquery/jquery.mobile-1.4.5.min.css" />
    <link rel="stylesheet" href="css/style.css" />
    <script src="jquery/jquery.min.js"></script>
    <script src="js/swipe-page.js"></script>
    <script src="jquery/jquery.mobile-1.4.5.min.js"></script>
</head>

<body>
        <div id="header" data-role="header" data-id="header" data-position="fixed" data-tap-toggle="false">
            <h1>Swipe left or right</h1>
            <a href="#mypanel" data-icon="bars" data-iconpos="notext" data-shadow="false" data-iconshadow="false" class="ui-nodisc-icon">Open left panel</a>
            <!-- <div data-role="navbar">
                <ul>
                    <li><a href="index.html" data-icon="grid" class="ui-btn-active">News</a></li>
                    <li><a href="market.html" data-icon="star">Market</a></li>
                    <li><a href="#" data-icon="gear">Announcement</a></li>
                    <li><a href="#" data-icon="comment">Portfolio</a></li>
                </ul>
            </div> --><!-- /navbar -->
        </div><!-- /header -->

        <div data-role="page" id="index" class="demo-page" data-title="Index" data-dom-cache="true" data-theme="a" data-next="market">
            <div role="main" class="ui-content">
                <h1>Div 1</h1>
            </div><!-- /content -->
            <div data-role="panel" id="mypanel" data-theme="a" data-display="overlay" data-position="left" >
                <ul data-role="listview">
                    <li data-icon="delete"><a href="#" data-rel="close">Close menu</a></li>
                    <li><a href="#panel-fixed-page2">Accordion</a></li>
                    <li><a href="#panel-fixed-page2">Ajax Navigation</a></li>
                    <li><a href="#panel-fixed-page2">Autocomplete</a></li>
                    <li><a href="#panel-fixed-page2">Buttons</a></li>
                    <li><a href="#panel-fixed-page2">Checkboxes</a></li>
                    <li><a href="#panel-fixed-page2">Collapsibles</a></li>
                    <li><a href="#panel-fixed-page2">Controlgroup</a></li>
                </ul>
            </div><!-- /panel -->
        </div><!-- /page -->

        <div  data-role="page" id="market" class="demo-page" data-title="Market" data-dom-cache="true" data-theme="a" data-prev="index">
            <div role="main" class="ui-content">
                <h1>Div 2</h1>
            </div><!-- /content -->
            <div data-role="panel" id="mypanel" data-theme="a" data-display="overlay" data-position="left">
                <ul data-role="listview">
                    <li data-icon="delete"><a href="#" data-rel="close">Close menu</a></li>
                    <li><a href="#panel-fixed-page2">Accordion</a></li>
                    <li><a href="#panel-fixed-page2">Ajax Navigation</a></li>
                    <li><a href="#panel-fixed-page2">Autocomplete</a></li>
                    <li><a href="#panel-fixed-page2">Buttons</a></li>
                    <li><a href="#panel-fixed-page2">Checkboxes</a></li>
                    <li><a href="#panel-fixed-page2">Collapsibles</a></li>
                    <li><a href="#panel-fixed-page2">Controlgroup</a></li>
                </ul>
            </div><!-- /panel -->
        </div><!-- /page -->

        <div id="footer" data-id="footer" data-position="fixed" data-tap-toggle="false">
            <div data-role="navbar">
                <ul>
                    <li><a href="#">Latest News</a></li>
                    <li><a href="#">Featured News</a></li>
                    <li><a href="#">Most Read</a></li>
                </ul>
            </div><!-- /navbar -->
        </div><!-- /footer -->
</body>
</html>

用于 div 滑动的 javascript 是:-

$(document).on('pageinit', function(event){
    $('div.ui-page').on("swipeleft", function () {
        var nextpage = $(this).next('div[data-role="page"]');
        if (nextpage.length > 0) {
            $.mobile.changePage(nextpage, "slide", false, true);
        }
    });

    $('div.ui-page').on("swiperight", function () {
        var prevpage = $(this).prev('div[data-role="page"]');
        if (prevpage.length > 0) {
          $.mobile.changePage(prevpage, { transition: "slide", reverse: true }, true, true);
        }
    });
});
4

1 回答 1

2

您对两个面板使用相同的 ID,这是错误的。为每个面板指定不同的 ID 或使用外部面板,如工具栏(页眉/页脚)。

将面板 div 放在页面 div 之外,然后手动对其进行初始化。

<!-- panel -->
<div data-role="panel" id="mypanel" data-theme="a" data-display="overlay" data-position="left">
  <ul data-role="listview">
     <li data-icon="delete"><a href="#" data-rel="close">Close menu</a></li>
     <li><a href="#panel-fixed-page2">Accordion</a></li>
     <li><a href="#panel-fixed-page2">Ajax Navigation</a></li>
     <li><a href="#panel-fixed-page2">Autocomplete</a></li>
     <li><a href="#panel-fixed-page2">Buttons</a></li>
     <li><a href="#panel-fixed-page2">Checkboxes</a></li>
     <li><a href="#panel-fixed-page2">Collapsibles</a></li>
     <li><a href="#panel-fixed-page2">Controlgroup</a></li>
  </ul>
</div><!-- /panel -->

<!-- pages -->
<div data-role="page" id="page1">
</div>

<div data-role="page" id="page1">
</div>

初始化外部面板

$(function () {
  $("#mypanel").panel().enhanceWithin();
});

.enhanceWithin()用于初始化面板内的小部件。


关于您的刷卡代码,它需要调整。您在每个pageinit. 即,如果您有五页,则在第五页上,滑动将触发五次。

请注意,从jQM 1.4 开始,两者都已弃用,它们的替代品分别pageinit是和。$.mobile.changePagepagecreate$.mobile.pageContainer.pagecontainer("change", target)

$(document).on('pagecreate', function (event) {
    var currentPage = $(event.target);

    currentPage.on("swipeleft", function () {
        var nextpage = currentPage.next('[data-role="page"]');
        if (nextpage.length > 0) {
            $.mobile.pageContainer.pagecontainer("change", nextpage, { /* options */ });
        }
    });

    currentPage.on("swiperight", function () {
        var prevpage = currentPage.prev('[data-role="page"]');
        if (prevpage.length > 0) {
            $.mobile.pageContainer.pagecontainer("change", prevpage, { /* options */ });
        }
    });
});

演示

于 2014-12-08T17:10:16.040 回答