JqueryMobile入门基础附源码下载

最近要作一个手机版的网站,因此就了解了一点JqueryMObile,下面是我整理的笔记,如今分享给你们,但愿朋友们喜欢,先给个首页看看吧!!!javascript

1、JqueryMobile基本页面结构css

<!DOCTYPE html> 
<html> 
     <head>
      <title>jQuery Mobile基本页面结构</title>   
      <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" /> 
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script> 
      <script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>  
     </head>   
     <body> 
             <div data-role="page" id="home">    
                      <div data-role="header"> <h1>Header</h1> </div>    
                      <div data-role="content">  
                          <p>Content goes here</p>  
                      </div>           
                      <div data-role="footer">   
                          <h4>Footer</h4>   
                      </div>       
            </div>
    </body> 
</html>
View Code

代码说明:html

要使用 jQuery Mobile,首先须要在开发的界面中包含以下3个内容:java

  1. CSS文件jquery.mobile-1.0a3.min.css
  2. jQuery类库文件 jquery-1.5.min.js
  3. jQuery Mobile文件 jquery.mobile-1.0a3.min.js

在上面的页面基本模板中,引入这三个元素采用的是jQuery CDN方式,开发人员也能够下载这些文件及主题到你的服务器上。jquery

咱们能够看到页面中的内容都是包装在div标签中并在标签中加入data-role=”page”属性。 这样jQuery Mobile就会知道哪些内容须要处理。web

说明:data-属性是HTML5新推出的颇有趣的一个特性,它可让开发人员添加任意属性到html标签中,只要添加的属性名有“data-”前缀。 在”page”div中,还能够包含”header”, ”content”, ”footer”的div元素,这些元素都是可选的,但至少要包含一个 “content”div。 windows

2、jQuery Mobile 入门:api

使用jQuery Mobile的第一步,先建立一个html页面,并在head标签中加入如下内容:浏览器

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css"> <script src="http://code.jquery.com/jquery-1.5.min.js"></script>缓存

<script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>

正如你在代码中看到的,jQuery Mobile是jQuery的一个扩展。目前来讲,压缩后的jQuery Mobile 仅12Kb。

在建立第一个jQuery Mobile页面时你须要建立三块基本内容,下面的推荐模版展现了这一过程,你能够在将来的项目中使用它:

1.在 head 标签内填写:

<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1"> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css"> 
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>

2.在body标签内填写:

<div data-role="page">
 <div data-role="header"><h1>Page Title</h1></div><!-- /header --> <div data-role="content"><p>Page content goes here.</p></div><!-- /content --> 
<div data-role="footer"><h4>Page Footer</h4></div><!-- /footer --> </div><!-- /page -->

在模版中有些地方值得咱们注意。首先是DIV元素的使用,咱们知道,既然HTML5在移动设备中如此流行,为何不使用HTML5的 header, article, section, footer 标签元素呢?这是由于较老的智能手机浏览器没法明白新的HTML5元素。在某些状况下,例如windows phone上老版本的IE会出现一个bug使得没法加载页面的css。而DIV元素却被普遍支持。此时你已经能够保存你的网页并在浏览器中查看了,这些代码一样能够在桌面浏览器中正常工做。我推荐你使用Chrome来进行本地测试。要在真实环境测试,你就须要相应移动设备了。

 

3.使用超连接:

普通网页和移动网页的一个巨大的不一样即是屏幕中呈现内容的数量多寡上。虽然你能够在你的iPhone上加载纽约时报的主页,但你须要缩放它才能顺利阅读上面的内容。这样的体验并很差,而更好的解决方案是减小那些杂乱的内容,只在屏幕上显示你须要显示的内容。

若是是传统的网站,你可能会建立一些包含少许内容的子页面,而当你使用jQuery Mobile时,你最好在页面中包含“微量”的内容,这样才会更有效率。

在上面例子中你已经看到了如何利用模版来建立一个页面。如今让咱们更进一步,来建立内容的“page”。jQuery Mobile中的一个“page”结构通常使用一个DIV来组织。如今你可使用上面的模板来建立一个包含四个跳转到其余页面的连接的导航页面:

<!-- Start of first page --> 
<div data-role="page" id="menu">   
 <div data-role="header"><h1>Menu</h1></div><!-- /header -->    <div data-role="content">    
     <p>What vehicles do you like?</p> 
     <p><a href="#Cars">Cars</a></p>  
     <p><a href="#Trains">Trains</a></p>    
   <p><a href="#Planes">Planes</a></p>    
</div><!-- /content -->   
 <div data-role="footer"><h4>Page Footer</h4></div><!-- /footer --> </div><!-- /page -->

上面这段代码中第一个div很是重要,它包含有一个id和一个data-role属性:

data-role="page" id="menu"

data-role定义了这个div是一个“page”,page这个术语稍微有点让人误解,“page”这里其实指的是一个可视面或者在屏幕里未隐藏的HTML代码部分,而不是指的一个单独的页面(或者说单独的HTML文件)。data-role="page" 意味着jQueryMobile会根据div元素在屏幕中构建可视内容。而id属性容许你经过a标签连接到该page,或者其余page。

上面建立的导航页是咱们在浏览器看到的第一个页面,接下来咱们再添加三个“page”,他们有不一样的id:Cars, Planes,Trains。

<div data-role="page" id="Cars">    
<div data-role="header">    
   <h1>Cars</h1>    
</div><!-- /header -->   
<div data-role="content">    
     <p>We can add a list of cars</p>  
 </div><!-- /content --> 
 <div data-role="footer">   
    <h4>Page Footer</h4>   
 </div><!-- /footer --> 
</div><!-- /page --> 
<!-- Start of third page --> 
<div data-role="page" id="Trains">    
    <div data-role="header">    
        <h1>Trains</h1>   
     </div><!-- /header -->  
 <div data-role="content">  
       <p>We can add a list of trains</p> 
 </div><!-- /content -->   
 <div data-role="footer">    
   <h4>Page Footer</h4>    
   </div><!-- /footer --> 
</div><!-- /page --> 
<!-- Start of fourth page --> 
<div data-role="page" id="Planes">   
     <div data-role="header">      
        <h1>Planes</h1> 
    </div><!-- /header --> 
   <div data-role="content">    
     <p>We can add a list of planes</p>   
 </div><!-- /content -->  
  <div data-role="footer">    <h4>Page Footer</h4> </div><!-- /footer --> </div><!-- /page -->

如今,在你的Android或者IOS设备里测试一下,当你加载好页面后你会发现这三件事情:

 导航页出如今屏幕中(你能够上下滚动一下,并无别的东西出现)

 当你点击一个连接时,会动画切换到另外一个页面。

其实这些div元素预先会加载并缓存到你的浏览器中,所以“页面”间的切换会很是流畅。

在同一个HTML页面建立多个在屏幕上显示的“页面”使得你能够大大减小页面加载的次数,但同时也会致使许多移动设备运行缓慢。jQueryMobile将页面全部的连接跳转都视做Ajax调用,这样能够充分利用CSS的过渡效果,当你想要连接到你本身的web程序以外的某些连接时,你能够这样编写你的连接代码:

<a href="http://www.lampweb.org" rel="external">lampweb.org</a>

如上所示,仅须要为a标签添加 rel="external"属性便可。然而jQueryMobile对于(同域的)外部连接并非简单地跳转完事儿,相比于其余移动框架它更进了一步,由于他对(同域的)全部连接都采用Ajax调用方式,从而实现漂亮的转场效果。基于此你能够将你的网页内容分离到许多页面来建立更大型的项目。 

4.使用组件

连接和页面只是移动网页设计中一个很小的部分,APP程序(好比采用Object-C,C#,java等在Android或者IOS设备上建立的本地应用程序)快速增加的同时产生了丰富的控件和组件(例如菜单栏,列表等控件)使得开发者能够很方便地建立复杂的应用程序,这正是许多移动网页开发者第二个巨大的挑战——由于在原生的HTML里面并无这些控件或者组件。针对这一问题,jQueryMobile当前正在建立一组很是有用的组件。如下是已经发布的组件:

 Pages(页面)

 Dialog boxes(对话框)

 Toolbars(工具栏)

 Buttons(按钮)

 Content formatting(内容格式化)

 Form elements(表单)

 List views(列表)

只要你懂的html基础知识,你会发现添加这些组件并不困难,接下来咱们看几个例子。

 

1>>为页面添加header和footer

上面的模版已经为你展现了如何轻松地建立工具栏(header,footer)。咱们知道,在不一样尺寸的屏幕上建立自适应工具栏一般都是很是难的活儿。而如今,jQueryMobile让你可以很是容易地建立一个带有一个居中标题和两个按钮而且自适应任何屏幕尺寸的header:

<div data-role="header" data-position="inline"> 
   <a href="cancel.html" data-icon="delete">Cancel</a>   
 <h1>Edit Contact</h1>   
 <a href="save.html" data-icon="check">Save</a> 
</div>

代码中a标签的顺序决定了按钮显示的位置。以上代码在几乎全部的设备中都能取得一致的显示效果。

header和footer一样能够被自定义为你想要的样式,好比改造一个导航条:你能够添加一些按钮在footer里面,从而导航到某一页面的不一样部分:

<div data-role="navbar">
    <ul>   
        <li><a href="#nav1" class="ui-btn-active">One</a></li> 
         <li><a href="#nav2">Two</a></li>   
   </ul> 
</div><!-- /navbar -->

从这些代码你能够发现jQueryMobile不过是一些简单的HTML代码,导航条依然是div,而诸如ui-btn-active这个class可使得你的按钮显示为被选择状态。

 当你点击一个按钮并导航到该页面的另外一个部分时,jQueryMobile会自动加上一个back按钮,如下的代码结构能够清晰地展示这一过程:

 

建立位置固定的header和footer

工具条固定在屏幕上方或者下方是一个很常见的界面设计,你能够为footer或者header添加 data-position="fixed"来实现这一点。

如下代码会强制footer/header固定在下方/上方:

<div data-role="header" data-position="fixed">  

     <h1>Navigation</h1>  

  </div><!-- /header -->    

<div data-role="content" >   

    <ul data-role="listview" data-dividertheme="d" style="margin-top: 0;">   

         <li data-role="list-divider">Royal Family</li>     

       <li><a href="#nav1">Henry VIII</a></li>     

       <li><a href="#nav1">George V</a></li>     

       <li><a href="#nav1">Prince of Wales</a></li>        

       <li><a href="#nav1">Elizabeth I</a></li>    

       <li><a href="#nav1">Elizabeth II</a></li>        

       <li data-role="list-divider">Prime Miniseters</li>         

       <li><a href="#nav2">Winston Churchill</a></li>       

      <li><a href="#nav2">Tony Blare</a></li>       

      <li><a href="#nav2">David Cameron</a></li>    

   </ul>    

</div><!-- /content -->

  <div data-role="footer" data-position="fixed">  

      <div data-role="navbar">  

         <ul>        

        <li><a href="#nav1" class="ui-btn-active">Royals</a></li>  

            <li><a href="#nav2">Leaders</a></li>      

        </ul>    

         </div><!-- /navbar --> 

    </div><!-- /footer --> 

 </div><!-- /page -->

<div data-role="page" id="nav1" data-position="fixed">  

 <div data-role="header">   

    <h1>Royal Family</h1> 

 </div><!-- /header --> 

<div data-role="content">  
      <p>Members and relatives of the British Royal Family historically represented the monarch in various places throughout the British Empire, sometimes for         extended periods as viceroys, or for specific ceremonies or events. Today, they often perform ceremonial and social duties throughout the United Kingdom and abroad on behalf of the UK, but, aside from the monarch, have no constitutional role in the affairs of government. This is the same for the other realms of the Commonwealth though the family there acts on behalf of, is funded by, and represents the sovereign of that particular state, and not the United Kingdom.</P> 
</div><!-- /content -->  
<div data-role="footer" data-position="fixed">  
     <h4>Royal Family</h4>  
</div><!-- /header --> 
</div><!-- /page --> 
<div data-role="page" id="nav2" data-position="fixed">   
 <div data-role="header">     
   <h1>Prime Ministers</h1>   
 </div><!-- /header --> 
 <div data-role="content"> The Prime Minister of the United Kingdom of Great Britain and Northern Ireland is the Head of Her Majesty's Government in the United Kingdom. The Prime Minister and Cabinet (consisting of all the most senior ministers, who are government department heads) are collectively accountable for their policies and actions to the Sovereign, to Parliament, to their political party and ultimately to the electorate. The current Prime Minister, David Cameron, was appointed on 11 May 2010.</div><!-- /content --> 
 <div data-role="footer" data-position="fixed">  
   <h4>Prime Minister</h4>      
 </div><!-- /header -->  
</div><!-- /page -->
View Code

看,如今不使用Objective-C ,你也能够建立一个易用的界面,是否是很不错?

 

2>>对话框

使用data-rel属性能够方便地建立弹出式对话框,如下代码将会以对话框形式加载dialog.html页面:

<a href="dialog.html" data-role="dialog">Open dialog</a>

因为对话框在jQueryMobile里面其实和其余page没有任何区别,所以你能够在对话框里添加任何类型的HTML代码,固然你也能够在同一个html文件里建立两个page,一个是主界面:

<div data-role="page"> 
   <div data-role="header">    
   <h1>Dialog Box</h1>   
 </div><!-- /header -->
 <div data-role="content">   
   <a href="#dialogPopUp" data-rel="dialog" data-role="button">Open dialog</a>  
    </div><!-- /content -->
     <div data-role="footer">  
         <h4>Page Footer</h4> 
     </div><!-- /footer --> 
</div><!-- /page -->

  


另外一个是对话框(上面的HREF属性便指向这个对话框page):

<div data-role="page" id="dialogPopUp"> 

   <div data-role="header">     

  <h1>Dialog Title</h1>   

 </div><!-- /header -->   

 <div data-role="content"> This is a dialog box </div><!-- /content --> 

 <div data-role="footer">

  <h4>Additional Footer information</h4>

  </div><!-- /footer -->

</div><!-- /page -->

要注意以上代码中footer是可选的,可是你必须包括header,由于若是没有header,将没法显示自动生成的对话框关闭按钮。

3>>列表

当你有大量的数据条目须要显示时,列表是一个不错的选择。下面是使用ul时的HTML代码:

<ul> 

   <li><a href="#nav1">Henry VIII</a></li>  

  <li><a href="#nav1">George V</a></li>  

  <li><a href="#nav1">Prince of Wales</a></li>   

 <li><a href="#nav1">Elizabeth I</a></li>   

 <li><a href="#nav1">Elizabeth II</a></li> 

</ul>

在jQueryMobile你能够经过data-role="listview"来将一个普通的列表转换成一个很是华丽如同本地APP通常的列表样式:

<ul data-role="listview">  

  <li><a href="#nav1">Henry VIII</a></li>   

 <li><a href="#nav1">George V</a></li>   

 <li><a href="#nav1">Prince of Wales</a></li>   

 <li><a href="#nav1">Elizabeth I</a></li> 

   <li><a href="#nav1">Elizabeth II</a></li> 

</ul>

就是这样,仅须要20个字母,你就搞定了这一切。

除此以外,jQueryMobile还为列表提供了一些加强的选项,如下代码将为列表添加分隔符(divider):

<ul data-role="listview" data-dividertheme="d">    

<li data-role="list-divider">Royal Family</li>  

  <li><a href="#home">Henry VIII</a></li> 

   <li><a href="#home">George V</a></li>   

 <li><a href="#home">Prince of Wales</a></li>  

  <li><a href="#home">Elizabeth I</a></li>  

  <li><a href="#home">Elizabeth II</a></li> 

  <li data-role="list-divider">Prime Ministers</li>    

 <li><a href="#home">Winston Churchill</a></li>  

 <li><a href="#home">Tony Blare</a></li> 

<li><a href="#home">David Cameron</a></li>

</ul>

  

如上面的代码所示,为li元素添加data-role="list-divider"便可实现分隔符的效果。

你还能够为列表添加小气泡来展现一些附加数据(本例展现了英国皇室的统治时间):

<ul data-role="listview" style="margin-top: 0;">  

  <li><a href="#nav1">Henry VIII <span class="ui-li-count">Reign 37 Years</span></a></li>   

 <li><a href="#nav1">George V <span class="ui-li-count">Reign 25 Years</span></a></li>   

 <li><a href="#nav1">Prince of Wales <span class="ui-li-count">N/A</span></a></li>   

 <li><a href="#nav1">Elizabeth I <span class="ui-li-count">Reign 44 Years</span></a></li>   

 <li><a href="#nav1">Elizabeth II<span class="ui-li-count">Reign since 1952</span></a></li>

</ul>

下面是一个更加复杂的例子,列表中包含了连接,图片,文本:

<ul data-role="listview" style="margin-top: 0;"> 
   <li>
    <img src= "http://img.freebase.com/api/trans/image_thumb/en/henry_viii_of_england?pad=1&errorid=%2Ffreebase%2Fno_image_png&maxheight=64&mode=fillcropmid&maxwidth=64" />       <h3><a href="index.html">Henry VIII</a></h3>  
         <p>Reign 37 Years</p>   
             <a href="#home">Details</a>  
   </li>   
   <li>    
      <img src="http://www.iwise.com/authorIcons/15/King_George%20V_64x64.png" />   
          <h3><a href="index.html">George V</a></h3> <p>Reign 25 Years</p> 
                <a href="#home">Details</a>  
   </li>   
   <li>    
      <img src="http://img.freebase.com/api/trans/image_thumb/en/prince_of_wales_secondary_school?pad=1&errorid=%2Ffreebase%2Fno_image_png&maxheight=64&mode=fillcropmid&maxwidth=64" />  
       <h3><a href="index.html">Prince of Wales</a></h3> 
       <p>Reign N/A</p>  
       <a href="#home">Details</a>  
   </li>  
   <li>  
     <img src="http://www.iwise.com/authorIcons/13846/Elizabeth%20I%20of%20England_64x64.png" />   
         <h3><a href="index.html">Elizabeth I</a></h3>    
            <p>Reign 44 Years</p>    
               <a href="#home">Details</a>  
   </li>   
   <li>  
        <img src="http://www.iwise.com/authorIcons/9098/Elizabeth%20II_64x64.png" />  
        <h3><a href="index.html">Elizabeth II</a></h3>   
        <p>Reign Since 1952</p> 
        <a href="#home">Details</a>  
   </li> 
 </ul>
View Code

在你须要展示富文本时这种列表尤其有用,好比显示一个包含照片,名字,平均分等信息的学生名单。

最后:部署你的jQueryMobile站点

当你完成了开发,最后一步固然是展现你的成果啦!

到现目前为止,jQueryMobile都只是包含了一些HTML,CSS,javascript文件罢了,部署方式与通常的HTML站点无异。用FTP(或者其余你喜欢的方式)上传到你的web服务器就能够了,固然,你要确保你上传了全部用到的文件。

此时已经大功告成,你就能够用你的移动设备访问站点啦!

使用jQueryMobile的目标群是移动设备用户,所以你能够考虑为你的网站建立两个版本,一个为桌面用户准备,另外一个则为移动用户(准备好比主站为http://www.zhuzhan.com,移动站位 :http://www.yidong.com)。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

尚未结束:下面呢是个人笔记,分享给你们,固然是JqueryMobile的啦。一些经常使用的组件和属性,以实例和代码的形式展现给你们,但愿对想了解JqueryMobile的朋友有帮助。很少说了,先给个图看一下:

 还犹豫什么,想学习就点击这里:http://files.cnblogs.com/itxiaoyan2013/JqueryMObile.zip这是个人劳动成果,免费分享给你们。。。

相关文章
相关标签/搜索