自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用jQuery Mobile框架將博客快速轉(zhuǎn)化為Mobile網(wǎng)站

移動開發(fā) 移動應(yīng)用
jQuery Mobile是一個非常不錯的移動端網(wǎng)站應(yīng)用的解決方案,很多網(wǎng)站都使用jQuery Mobile來生成Mobile手機端的移動網(wǎng)站應(yīng)用,在過去的GBin1博客文章中,我們曾經(jīng)使用jQuery Mobile利用RSS訂閱生成過一個簡單的RSS閱讀手機端,在今天的教程中,我們將講解如何將GBin1網(wǎng)站完整的轉(zhuǎn)化為一個全功能的移動端網(wǎng)站,主要的技術(shù)使用jQuery Mobile和Java的SiteMesh布局類庫

使用jQuery Mobile移動開發(fā)框架將博客網(wǎng)站快速轉(zhuǎn)化為Mobile網(wǎng)站

在線演示

jQuery Mobile是一個非常不錯的移動端網(wǎng)站應(yīng)用的解決方案,很多網(wǎng)站都使用jQuery Mobile來生成Mobile手機端的移動網(wǎng)站應(yīng)用,在過去的GBin1博客文章中,我們曾經(jīng)使用jQuery Mobile利用RSS訂閱生成過一個簡單的RSS閱讀手機端,在今天的教程中,我們將講解如何將GBin1網(wǎng)站完整的轉(zhuǎn)化為一個全功能的移動端網(wǎng)站,主要的技術(shù)使用jQuery Mobile和Java的SiteMesh布局類庫

系統(tǒng)設(shè)計分析

在我們正式的開始開發(fā)之前,我們需要大概的分析一下如何設(shè)計Mobile網(wǎng)站,很多網(wǎng)站的移動端應(yīng)用和網(wǎng)站的內(nèi)容是分離的,也就是說,桌面訪問網(wǎng)站和移動設(shè)備訪問網(wǎng)站的路徑是不同的。

  • 好處是可以分別開發(fā)獨立的功能,我們可以針對移動端的用戶設(shè)計獨立的UI,展現(xiàn)方式,或者使用方式,往往對于比較大型的網(wǎng)站或者已經(jīng)上線很久的網(wǎng)站這樣設(shè)計成本比較低。
  • 壞處是你需要獨立的維護兩套代碼,無形的增加了開發(fā)成本和維護成本 

在我們GBin1的移動設(shè)備網(wǎng)站開發(fā)中,我們本著盡量維護同一套代碼的原則,希望能夠直接重用網(wǎng)站的頁面內(nèi)容和框架。值得一提的是,GBin1架 構(gòu)使用Sitemesh的模板布局功能來生成頁面的不同樣式和布局,而相關(guān)的頁面內(nèi)容及其附屬功能,例如,留言等等都是由靜態(tài)頁面或者功能模塊生成,這樣 的設(shè)計使得我們可以利用已經(jīng)存在的頁面內(nèi)容花最小的代價來創(chuàng)建新的移動設(shè)備模板,因而可以快速的生成新的移動端web應(yīng)用,而且整體結(jié)構(gòu)及其閱讀使用方式 將會和網(wǎng)站本身的使用方式類似,降低了用戶的學(xué)習(xí)成本。

如何實現(xiàn)?

為了讓系統(tǒng)能夠自動處理應(yīng)用不同的模板,這里我們使用了一個小技巧:

我們創(chuàng)建一個2級域名, 例如, http://m.gbin1.com,將它通過域名綁定了我們的服務(wù)器。然后通過后臺的應(yīng)用來判斷當前的域名訪問,如果用戶通過http://www.gbin1.com來訪問的話,我們將應(yīng)用網(wǎng)站模板,如果用戶嘗試使用http://m.gbin1.com來訪問gbin1的時候,我們將使用移動設(shè)備模板來展示網(wǎng)站內(nèi)容。通過這種方法,系統(tǒng)能夠自動的應(yīng)用不同的模板生成不同的web應(yīng)用界面。

當然,我們也可以做的更好,自動判斷客戶端的代理類型,如果是移動設(shè)備,自動重定向到http://m.gbin1.com,否則重定向到http://www.gbin1.com。

代碼實現(xiàn)

設(shè)計和實現(xiàn)基本完成以后,我們正式開始開發(fā)網(wǎng)站的移動設(shè)備模板,使用jQuery Mobile框架。使用jQuery Mobile框架的好處在于,考慮了不同手機的兼容性并且封裝了現(xiàn)成的UI組件,我們只需要簡單的添加幾個html標簽即可生成對應(yīng)的頁面,如下:

  1. <div data-role="page"> 
  2.     <div data-role="header"> 
  3.         <a href="/bloghome.html" data-icon="help">首頁</a> 
  4.         <h1 id="logo"></h1> 
  5.         <a href="/portfolio/" data-icon="info">關(guān)于</a> 
  6.         <div data-role="navbar" data-iconpos="top" data-theme="a"> 
  7.             <nav> 
  8.  
  9.             </nav> 
  10.         </div><!-- /navbar --> 
  11.     </div><!-- /header --> 
  12.  
  13.     <div data-role="content">    
  14.  
  15.     </div><!-- /content --> 
  16.  
  17.     <div data-role="footer">  
  18.         <h4>&copy; Copywrite by gbin1.com, all rights reserved.</h4>  
  19.     </div>  
  20. </div><!-- /page --> 

以上代碼我們定義了,頁頭,內(nèi)容和頁腳。

在頁頭中我們添加了兩個按鈕, 首頁和關(guān)于

頁腳中我們添加了版權(quán)相關(guān)信息。

生成頁面內(nèi)容如下:

m.gbin1.com

為了更好在不同的移動設(shè)備上看到展示效果,這里我們使用一個在線測試響應(yīng)式設(shè)計的web工具- Screenqueri.es,使用它能夠幫助你在不同的移動設(shè)備上查看網(wǎng)站效果,同時它支持本地localhost應(yīng)用查看效果,支持不同設(shè)備上的預(yù)覽,例如,iphone,ipad及其其它設(shè)備,并且支持橫豎屏切換,所以對于我們在線調(diào)試非常有幫助。

添加相關(guān)代碼

接下里我們需要添加一些相關(guān)代碼來處理移動設(shè)備中的顯示,包括:

  • 圖片
  • 視頻
  • 其它

因為不同的設(shè)備的顯示界面尺寸不一樣,為了能夠在小尺寸的設(shè)備上正常的顯示頁面,我們需要在加載頁面之前,處理相關(guān)圖片,或者視頻的尺寸大小。代碼如下:

  1. $(document).bind('pageinit' , function(){ 
  2.  
  3.     $('img').css({ 
  4.         'max-width':'280px'
  5.         'height':'auto' 
  6.     }); 
  7.  
  8. }); 

注意我們在以上代碼中調(diào)用了css方法來處理圖片,這里為了簡單,我們直接將圖片設(shè)置為最大寬大為280px,如果你需要支持不同的設(shè)備,你可以根據(jù)設(shè)備不同,處理不同的圖片寬度。

再 請注意這里我們使用了pageinit方法,在正常的jQuery開發(fā)中,我們一般使用document.ready() 方法來處理,但是對于jQuery mobile來說,當頁面導(dǎo)航的時候Ajax被用來加載頁面內(nèi)容,所以DOM ready方法只存在于第一個加載頁面,為了能夠讓每一個頁面都可以執(zhí)行代碼,所以我們需要在頁面初始化(pageinit)方法中調(diào)用相關(guān)方法,否則你 會看到除了第一個頁面外以后其它頁面的圖片沒有任何變化。

如果你有其它的代碼需要在也頁面中調(diào)用的話,你同樣也可以加載到上面的方法中去。如下:

  1. $(document).bind('pageinit' , function(){ 
  2.  
  3.     $('img').css({ 
  4.         'max-width':'280px'
  5.         'height':'auto' 
  6.     }); 
  7.  
  8.     $('a').each(function(){ 
  9.         var url = $(this).attr('href'); 
  10.         var laststr = url.lastIndexOf(".html"); 
  11.         var lastc = url.charAt(url.length-1); 
  12.         var startc = url.charAt(0); 
  13.         if(laststr<0&&lastc!=='/'&&startc!=='?'){ 
  14.             $(this).attr('href'url + '/'); 
  15.         } 
  16.     }); 
  17.  
  18. }); 

在我們的代碼中,我們需要處理圖片及其url中的地址問題,所以我們將相關(guān)的代碼放置到pageinit中。

完整模板代碼如下:

  1. <!DOCTYPE html> 
  2. <!-- By GBin1.com--> 
  3. <%@ page contentType="text/html;charset=utf-8"%> 
  4. <%@ taglib uri="gbin1cms-taglib" prefix="cms" %> 
  5.  
  6. <jsp:useBean id="userInfo" scope="session" class="com.gbin1.core.UserInfo" /> 
  7. <jsp:useBean id="webSite" scope="request" type="com.gbin1.core.WebSite" /> 
  8.  
  9. <html> 
  10. <head> 
  11.         <title><cms:pagetitle /></title> 
  12.         <meta name="description" content="<cms:info id="description" />" /> 
  13.         <meta name="keywords" content="<cms:info id="keywords" />" /> 
  14.         <meta name="author" content="<cms:info id="author" /> (<cms:info id="authorurl" />)" />      
  15.         <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" /> 
  16.         <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
  17.         <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script> 
  18.         <cms:pagehead />  
  19.         <style> 
  20.             #logo{ 
  21.                 background: url('/gbin1/themes/gbin1_mobile/images/logo.png') no-repeat 50% 50%; 
  22.                 height: 30px; 
  23.             } 
  24.  
  25.             #navlist{ 
  26.                 font-size:14px; 
  27.             } 
  28.  
  29.             .ui-btn-text{ 
  30.                 font-size:12px; 
  31.             } 
  32.  
  33.             H1,H2,H3,H4,H5,H6,H7,H8,H9,H10{ 
  34.                 font-size:14px; 
  35.             } 
  36.  
  37.             .ui-header .ui-title{ 
  38.                 font-size:14px; 
  39.             } 
  40.  
  41.             .ui-footer .ui-title{ 
  42.                 font-size:12px; 
  43.                 font-weight: normal; 
  44.             } 
  45.  
  46.             .ui-body-c .ui-link { 
  47.                 text-decoration: none; 
  48.                 font-size:14px; 
  49.                 font-weight: normal; 
  50.             } 
  51.             .ui-btn-text{ 
  52.                 font-weight: normal; 
  53.             } 
  54.             .ui-body-c{ 
  55.                 font-size:14px; 
  56.                 line-height: 20px; 
  57.             } 
  58.             .includetitle{ 
  59.                 background: #E1E1E1; 
  60.                 padding: 5px 5px; 
  61.                 border-left: 3px solid #BBB; 
  62.                 border-radius: 3px 3px 3px 3px; 
  63.             } 
  64.  
  65.             .includenavigation .ui-link, .includenavigation .ui-link:visited{ 
  66.                 color: #ffffff; 
  67.                 text-shadow: 0px 0px 20px #CCC; 
  68.             } 
  69.  
  70.             .includereadmore{ 
  71.                 text-align: right; 
  72.                 padding: 5px; 
  73.                 width: auto; 
  74.                 border-top: 1px solid #CCC; 
  75.             } 
  76.  
  77.             .includenavigation{ 
  78.                 color: #909090; 
  79.                 font-weight: bold; 
  80.             } 
  81.         </style> 
  82.         <script>             
  83.  
  84.             $(document).bind('pageinit' , function(){ 
  85.  
  86.                 $('img').css({ 
  87.                     'max-width':'280px', 
  88.                     'height':'auto' 
  89.                 }); 
  90.  
  91.                 $('a').each(function(){ 
  92.                     var url = $(this).attr('href'); 
  93.                     var laststr = url.lastIndexOf(".html"); 
  94.                     var lastc = url.charAt(url.length-1); 
  95.                     var startc = url.charAt(0); 
  96.                     if(laststr<0&&lastc!=='/'&&startc!=='?'){ 
  97.                         $(this).attr('href', url + '/'); 
  98.                     } 
  99.                 }); 
  100.  
  101.             }); 
  102.         </script> 
  103. </head> 
  104. <body> 
  105. <cms:editor>    
  106. <div data-role="page"> 
  107.     <div data-role="header"> 
  108.         <a href="/bloghome.html" data-icon="help"><%=new String("\u9996\u9875")%></a> 
  109.         <h1 id="logo"></h1> 
  110.         <a href="/portfolio/" data-icon="info"><%=new String("\u5173\u4e8e")%></a> 
  111.         <div data-role="navbar" data-iconpos="top" data-theme="a"> 
  112.             <nav> 
  113.                 <cms:listmenu style="listview" path="root" items="firstlevel" current="link" currentStyle="current" currentPathStyle="ui-btn-active"/> 
  114.             </nav> 
  115.         </div><!-- /navbar --> 
  116.     </div><!-- /header --> 
  117.  
  118.     <div data-role="content">    
  119.         <cms:moduletitle location="left1" /> 
  120.         <cms:ifmodule location="left1"> 
  121.             <cms:module location="left1" alt="left1" /> 
  122.         </cms:ifmodule> 
  123.         <cms:pagebody /> 
  124.     </div><!-- /content --> 
  125.  
  126.     <div data-role="footer">  
  127.         <h4>&copy; Copywrite by gbin1.com, all rights reserved.</h4>  
  128.     </div>  
  129. </div><!-- /page --> 
  130.  
  131. </cms:editor> 
  132. <!-- google and baidu code  
  133. <script type="text/javascript"> 
  134.   var _gaq_gaq = _gaq || []; 
  135.   _gaq.push(['_setAccount', 'UA-19118450-1']); 
  136.   _gaq.push(['_trackPageview']); 
  137.   (function() { 
  138.     var ga = document.createElement('script'); ga.type = 'text/javascript'ga.async = true
  139.     ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
  140.     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
  141.   })(); 
  142. </script> 
  143. <script type="text/javascript"> 
  144. var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); 
  145. document.write(unescape("%3Cscript src='" + _bdhmProtocol + "#/h.js%3Fd999331ad5ea0c0930f3aa7c3bda9fc1' type='text/javascript'%3E%3C/script%3E")); 
  146. </script> 
  147. <!-- end of google and baidu code -->    
  148. </body> 
  149. </html> 

搞定!從上面代碼中我們可以看到,使用模板化的網(wǎng)站設(shè)計對于添加jQuery mobile移動web網(wǎng)站非常重要,你只需要替換CSS,并且將所有的jQuery Mobile的處理代碼放置到模板中即可,基本無須修改其它結(jié)構(gòu),即可看到效果。

當然,整個頁面的樣式和風(fēng)格可能差強人意,不過我們會在以后的文章中,會教大家如何將mobile web應(yīng)用修改的更好,例如,修改缺省的UI樣式,添加搜索功能等等。

責(zé)任編輯:佚名 來源: GBin1.com
相關(guān)推薦

2011-07-21 14:57:34

jQuery Mobi

2011-09-05 13:45:25

jQuery MobiSencha Touc移動Web UI框架

2012-11-15 10:18:51

IBMdw

2011-07-19 14:51:54

jQuery Mobi特性

2011-09-02 10:41:51

2012-10-09 09:28:48

jQuery Mobi餐廳訂餐應(yīng)用

2011-09-01 10:16:43

JQuery Mobi

2011-05-26 16:28:08

Android jQuery

2011-07-19 17:09:44

jQuery Mobi事件

2011-09-01 10:21:52

jQuery Mobi元素

2011-09-05 16:43:00

jQuery Mobi

2011-07-21 16:10:11

button按鈕jQuery Mobi

2011-07-20 14:11:24

響應(yīng)布局jQuery Mobi

2009-02-17 12:38:21

OperaMobile Linu瀏覽器

2011-07-06 16:26:32

jQuery Mobi

2011-09-05 16:47:49

jQuery Mobi

2012-04-17 22:43:46

jQuery Mobi

2011-09-01 10:27:42

jQuery Mobi

2011-09-01 15:00:48

jQuery MobiDemo

2011-07-19 15:01:16

jQuery Mobi易用性
點贊
收藏

51CTO技術(shù)棧公眾號