Sencha Touch開發執行個體:新浪微博iPhone介面

來源:互聯網
上載者:User

相信不少Sencha Touch的粉絲們都為JavaScript能開發出手機Web App應用而感到興奮。本次樣本將以iPhone介面作為參考,使用Sencha Touch開發設計模仿iPhone介面。

首先,將以iPhone下述的介面目標作為參考:

Sencha Touch將採用以下UI組件:

Ext.Panel Ext.List

首先,建立一個html 5頁面和一個js頁面,如下代碼:

  1. <!DOCTYPE HTML> 
  2. <html> 
  3.     <head> 
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  5.         <title>demos</title> 
  6.         <link rel="stylesheet" type="text/css" href="sencha-touch.css"  /> 
  7.         <script type="text/javascript" src="sencha-touch-debug.js"></script> 
  8.         <script type="text/javascript" src="sencha.js" charset="utf-8"></script> 
  9.     </head> 
  10.     <body> 
  11.     </body> 
  12.     </html> 

sencha.js先增加setup以便文檔載入完後初始化Panel組件,如下代碼:

  1. Ext.setup({ 
  2.     onReady: function() { 
  3.         var moreObj; 
  4.         moreObj = Ext.extend(Ext.Panel, { 
  5.             layout : 'card', 
  6.             initComponent : function() { 
  7.                 moreObj.superclass.initComponent.call(this,arguments); 
  8.             } 
  9.         }) 
  10.   
  11.         new moreObj({ 
  12.             fullscreen : true, 
  13.             scroll : 'vertical' 
  14.         }); 
  15.   
  16.     } 
  17. }); 

完成初始化Panel組件後,介面將存在一個Panel的UI,此時並沒有任何內容,現在添加一個list以便顯示更多資料列表。在initComponent初始化函數內增加以下一段Ext.list代碼:

  1. this.list5 = new Ext.List({ 
  2.       itemTpl: '<tpl if="needsIcon"><img width="26" height="26" src="images/{icon}.jpg" align="absmiddle"  /></tpl>{name}', 
  3.       store: new Ext.data.Store({ 
  4.              fields: ['name','icon','needsIcon'], 
  5.              data: [ 
  6.                     {"name" : "直接登入","icon":'login',"needsIcon":true}, 
  7.                     {"name" : "找回密碼","icon":'password',"needsIcon":true}, 
  8.                     {"name" : "聲音提示","icon":'sound',"needsIcon":true}, 
  9.                     {"name" : "關於我們","icon":'version',"needsIcon":false}, 
  10.                     {"name" : "問題反饋","icon":'question',"needsIcon":true}, 
  11.                     {"name" : "客服電話","icon":'phone',"needsIcon":true}, 
  12.                     {"name" : "軟體版本0.92","icon":'version',"needsIcon":true} 
  13.              ] 
  14.         }), 
  15.         title: 'title1' 
  16. }); 

完成list的初始化後,將list加入到外部Panel中,在initComponent增加如下代碼:

  1. this.listpanel = new Ext.Panel({ 
  2.        title: '更多', 
  3.        items: [this.list5] 
  4. }) 
  5. this.items = [this.listpanel]; 

此時大部分sencha代碼都基本完畢,效果如:

相信大家都看到此時的介面仍然不是我們所想要的微博更多介面,那我們現在開始對該list介面進行修飾一翻,在初始化Ext.list內部增加以下幾個屬性:

  1. ui: 'round', 
  2. scroll : false, 
  3. margin : '10 15 10 15' 

增加屬性margin是為了list的資料和上下左右的邊框距離遠一點,以便更好展示按鈕效果。

接著,在html5頁面上增加一個css檔案,以便對list的ui進行定製,如下匯入css檔案:

  1. <link href="test.css" rel="stylesheet"  /> 

在sencha.js裡面的list組件增加兩個css屬性,如下代碼:

  1. cls : 'list2', 
  2. itemCls : 'list2_item', 

第一個cls為list組件的class屬性,第二個為list組件的每個資料項目各自的class屬性

最後在test.css檔案內增加以下css代碼,以便修正每個資料項目的位置以及風格:

  1. .list2{ 
  2.     -webkit-border-radius : 10px; 
  3. } 
  4.   
  5. .list2 .x-list-item{ 
  6.     background-color : #FFF; 
  7.     border:1px solid silver; 
  8.     -webkit-border-radius : 10px; 
  9. } 
  10.   
  11. .list2 .x-list-item:first-of-type{ 
  12.     margin : 0 0 15px 0; 
  13.     border-top:1px solid silver; 
  14. } 
  15. .list2 .x-list-item:nth-of-type(2){ 
  16.     -webkit-border-bottom-left-radius:0px; 
  17.     -webkit-border-bottom-right-radius:0px; 
  18. } 
  19. .list2 .x-list-item:nth-of-type(3){ 
  20.     border-top:0px; 
  21.     margin : 0 0 15px 0; 
  22.     -webkit-border-top-left-radius:0px; 
  23.     -webkit-border-top-right-radius:0px; 
  24. } 
  25. .list2 .x-list-item:nth-of-type(4){ 
  26.     -webkit-border-bottom-left-radius:0px; 
  27.     -webkit-border-bottom-right-radius:0px; 
  28. } 
  29. .list2 .x-list-item:nth-of-type(5){ 
  30.     border-top:0px; 
  31.     -webkit-border-radius:0px; 
  32. } 
  33. .list2 .x-list-item:nth-of-type(6){ 
  34.     border-top:0px; 
  35.     margin : 0 0 15px 0; 
  36.     -webkit-border-top-left-radius:0px; 
  37.     -webkit-border-top-right-radius:0px; 
  38. } 
  39. .list2 .x-list-item:nth-of-type(7){ 
  40.     margin : 0 0 15px 0; 
  41. } 
  42.   
  43. .list2 .x-item-selected:last-child{ 
  44.     -webkit-border-bottom-left-radius:10px; 
  45.     -webkit-border-bottom-right-radius:10px; 
  46. } 
  47. .list2 .x-item-selected:first-of-type{ 
  48.     -webkit-border-top-left-radius:10px; 
  49.     -webkit-border-top-right-radius:10px; 
  50. } 
  51.   
  52. .list2 img{ 
  53.     margin-right:10px; 
  54. } 

此處的css更多的是利用radius屬性對每個選項的的四個角進行圓角效果,由於list的每個資料項目都需要分組,css裡面則採用nth-of-type等選擇符自訂分組,因此存在一定的局限性。

此時代碼已經基本完成。如下:

這樣,iPhone微博更多UI介面已經製作完成。目前對於此解決方案,存在一種局限性,list資料需固定、分組需固定,暫時無法做到資料的即時性等功能。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.