>我在单个页面中创建了多个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);
}
});
});
您对两个面板使用相同的 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.changePage
都已弃用,它们的替换分别是pagecreate
和$.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 */ });
}
});
});
演示