服务器之家:专注于服务器技术及软件下载分享
分类导航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服务器之家 - 编程语言 - JavaScript - 使用jQueryMobile实现滑动翻页效果的方法

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

2021-08-05 15:52yongh701 JavaScript

这篇文章主要介绍了使用jQueryMobile实现滑动翻页效果的方法,较为详细的分析了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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://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滑动手势控制不到,不起作用 --> 
<div data-role="page" data-position="fixed" data-fullscreen="true" id="mypage"> 
  <div data-role="header" data-theme="b" data-tap-toggle = "false"> 
    <h1>Title</h1> 
 
  </div> 
<!-- html部分很简单,就在content中布局4个图层,其中div1一开始显示,其余隐藏即好,之所以把“你好”二字设置得大大的,是由于jquerymobile的滑动必须滑到图层内的非空白部分,即使你设置了width:100%; height:100% --> 
  <div data-role="content"> 
      <div id="div1"> 
        <h1>你好1</h1> 
      </div> 
      <div id="div2" style="display:none;"> 
        <h1>你好2</h1> 
      </div> 
      <div id="div3" style="display:none;"> 
        <h1>你好3</h1> 
      </div> 
      <div id="div4" style="display:none;"> 
        <h1>你好4</h1> 
      </div> 
      <!-- 此乃记录翻到那一页的图层,有一个名叫divnumber的行内文本 --> 
      <div> 
        <span id="divnumber"></span><span>/4</span> 
      </div> 
  </div> 
 
  <div data-role="footer" data-position="fixed" data-fullscreen="true"  data-theme="b" data-tap-toggle = "false"> 
      <div 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> 
    </div> 
     
  </div>  
</div>  

 

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

 

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

希望本文所述对大家的jQueryMobile程序设计有所帮助。

延伸 · 阅读

精彩推荐