使用滑动手势在多页 jQuery 移动应用程序中更改页面 [英] Using swipe gestures to change pages in multi-page jQuery mobile application

查看:19
本文介绍了使用滑动手势在多页 jQuery 移动应用程序中更改页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 jQuery Mobile 多页模板移动网站/应用程序上实现滑动手势导航?

我可以按如下方式组合一个死区简单的结构:

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

但是当我开始使用锚标记(多页 JQM 样式)时,事件不起作用:

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

是否有合适的解决方法,或者我是否必须放弃多页设置才能使其正常工作?

解决方案

工作示例:http://jsfiddle.net/Gajotres/JB22E/3/

HTML:

<头><title>分享二维码</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><身体><div data-role="page" id="article1"><div data-role="header" data-theme="b" data-position="fixed" data-id="footer"><h1>文章</h1>

<div data-role="内容"><p>第1条</p>

<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">首页</a><h1>文章</h1>

<div data-role="内容"><p>第2条</p>

<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">首页</a><h1>文章</h1>

<div data-role="内容"><p>第3条</p>

JavaScript:

$(document).on('swipeleft', '.ui-page', function(event){if(event.handled !== true)//这将防止事件触发多次{var nextpage = $.mobile.activePage.next('[data-role="page"]');//如果存在,则使用下一页的 id 滑动如果 (nextpage.length > 0) {$.mobile.changePage(nextpage, {transition: "slide", reverse: false}, true, true);}event.handled = true;}返回假;});$(document).on('swiperight', '.ui-page', function(event){if(event.handled !== true)//这将防止事件触发多次{var prevpage = $(this).prev('[data-role="page"]');如果(prevpage.length > 0){$.mobile.changePage(prevpage, {transition: "slide", reverse: true}, true, true);}event.handled = true;}返回假;});

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?

解决方案

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

这篇关于使用滑动手势在多页 jQuery 移动应用程序中更改页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆