欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用jQueryMobile实现滑动翻页效果的方法教程

程序员文章站 2022-06-07 14:50:23
本文实例讲述了使用jquerymobile实现滑动翻页效果的方法。分享给大家供大家参考。具体分析如下: 滑动手势在移动设备是很流行的,在移动设备中滑动翻页中很常见 虽然这个功能...

本文实例讲述了使用jquerymobile实现滑动翻页效果的方法。分享给大家供大家参考。具体分析如下:

滑动手势在移动设备是很流行的,在移动设备中滑动翻页中很常见

虽然这个功能可以在jquerymobile中实现,但是个人与之前一篇【jquery手机中拖拽动作的艰难性分析】中的观点一致,由于这是在手机浏览器中浏览,而不是安卓的一个独立app,所以不要经常除点击以外的移动设备手势,以免跟手机浏览器与手机本身的手势发生冲突。

那么,使用jquerymobile实现滑动翻页的效果到底怎么做呢?

一、基本目标

在手机浏览器中的jquerymobile框架页中现实滑动手势翻页的功能,如下图:

使用jQueryMobile实现滑动翻页效果的方法教程

并且记录当前页的页数,随用户滑动而自动增加与减少。

二、制作过程

关于jquerymobile的界面怎么布置,不再细说,详情请翻阅之前一篇【jquerymobile之helloworld与页面切换的方法】

如下的代码注释,主要是叙述如何通过对jquerymobile封装好的滑动手势jquery mobile swipeleft与jquery mobile swiperight处理,来实现上面的页面,w3c《jquery mobile touch 事件》一文中对此的叙述是有问题的,实验代码与给出的代码并不一致:

 

代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="https://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>a</title> 
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"> 
<!-- 需要的文件不再多嘴 --> 
<link rel="stylesheet" href="jqmobile/jquery.mobile-1.4.5.css"> 
<script src="jqmobile/jquery-1.11.1.js"></script> 
<script src="jqmobile/jquery.mobile-1.4.5.js"></script> 
 
</head> 
 
<body> 
<!-- 必须此页命名,否则下面的jquerymobile滑动手势控制不到,不起作用 --> 
<p data-role="page" data-position="fixed" data-fullscreen="true" id="mypage"> 
  <p data-role="header" data-theme="b" data-tap-toggle = "false"> 
    <h1>title</h1> 
 
  </p> 
<!-- html部分很简单,就在content中布局4个图层,其中p1一开始显示,其余隐藏即好,之所以把“你好”二字设置得大大的,是由于jquerymobile的滑动必须滑到图层内的非空白部分,即使你设置了width:100%; height:100% --> 
  <p data-role="content"> 
      <p id="p1"> 
        <h1>你好1</h1> 
      </p> 
      <p id="p2" style="display:none;"> 
        <h1>你好2</h1> 
      </p> 
      <p id="p3" style="display:none;"> 
        <h1>你好3</h1> 
      </p> 
      <p id="p4" style="display:none;"> 
        <h1>你好4</h1> 
      </p> 
      <!-- 此乃记录翻到那一页的图层,有一个名叫pnumber的行内文本 --> 
      <p> 
        <span id="pnumber"></span><span>/4</span> 
      </p> 
  </p> 
 
  <p data-role="footer" data-position="fixed" data-fullscreen="true"  data-theme="b" data-tap-toggle = "false"> 
      <p data-role="navbar"> 
      <ul> 
        <li><a href="#" class="ui-btn-active ui-state-persist" data-icon="info">a</a></li> 
        <li><a href="#" target="_self" data-icon="grid">b</a></li> 
        <li><a href="#" target="_self" data-icon="star">c</a></li> 
      </ul> 
    </p> 
     
  </p>  
</p>  

 

</body> 
</html> 
<script> 
    /* jquery部分,先定义一个记录翻到多少页的变量 */ 
    var pnum=1; 
    /* 相当于.innerhtml=""; jquery设置一个节点的值是需要这样设定的 */ 
    $("#pnumber").text(pnum) 
    /* 在#mypage页面开启触控 */ 
    $(document).on("pageinit","#mypage",function(){ 
        /* 如果对p1的非空白部分向左滑,那么p1就隐藏,p2就显示,同时页面计数器+1,并更新pnumber这个行内文本 */ 
        $("#p1").on("swipeleft",function(){ 
             $("#p1").hide("fast"); 
             $("#p2").show("fast"); 
             pnum=pnum+1; 
             $("#pnumber").text(pnum) 
        }); 
        /* 如果对p2的非空白部分向右滑,那么p1就显示,p2就隐藏,同时页面计数器-1,并更新pnumber这个行内文本 */ 
         $("#p2").on("swiperight",function(){ 
             $("#p1").show("fast"); 
             $("#p2").hide("fast"); 
             pnum=pnum-1; 
             $("#pnumber").text(pnum) 
        }); 
        /* 如果对p2的非空白部分向左滑,那么p2就隐藏,p3就显示,同时页面计数器+1,并更新pnumber这个行内文本,下面如此类推 */ 
        $("#p2").on("swipeleft",function(){ 
             $("#p2").hide("fast"); 
             $("#p3").show("fast"); 
             pnum=pnum+1; 
             $("#pnumber").text(pnum) 
        }); 
        $("#p3").on("swiperight",function(){ 
             $("#p2").show("fast"); 
             $("#p3").hide("fast"); 
             pnum=pnum-1; 
             $("#pnumber").text(pnum) 
        }); 
        $("#p3").on("swipeleft",function(){ 
             $("#p3").hide("fast"); 
             $("#p4").show("fast"); 
             pnum=pnum+1; 
             $("#pnumber").text(pnum) 
        }); 
         $("#p4").on("swiperight",function(){ 
             $("#p3").show("fast"); 
             $("#p4").hide("fast"); 
             pnum=pnum-1; 
             $("#pnumber").text(pnum) 
        });                           
    }); 
</script>

请注意,p1没有向右滑的手势,因为这是第一页,p4没有向左滑的手势,因为这是最后一页。