微信小程序之側欄分類 —— 微信小程序實戰商城系列(1)


在商場項目中,一般都會有分類頁面。

分類頁面可以給用戶快速找到相關的商品,下面以側欄分類為例,如下圖

 

布局分析:

<主盒子>

<左盒子></左盒子>

<右盒子></右盒子>

</主盒子>

左盒子使用標准流

右盒子使用絕對定位(top、right)

 

 

wxml:

[html]  view plain  copy
 
 
  1. <!--主盒子-->  
  2. <view class="container">  
  3.   <!--左側欄-->  
  4.   <view class="nav_left">  
  5.     <block wx:for="{{navLeftItems}}">  
  6.       <!--當前項的id等於item項的id,那個就是當前狀態-->  
  7.       <!--用data-index記錄這個數據在數組的下標位置,使用data-id設置每個item的id值,供打開2級頁面使用-->  
  8.       <view class="nav_left_items {{curNav == item.id ? 'active' : ''}}" bindtap="switchRightTab" data-index="{{index}}" data-id="{{item.id}}">{{item.tree.desc}}</view>  
  9.     </block>  
  10.   </view>  
  11.   <!--右側欄-->  
  12.   <view class="nav_right">  
  13.     <!--如果有數據,才遍歷項-->  
  14.     <view wx:if="{{navRightItems[curIndex].tree.nodes[1].tree.nodes}}">  
  15.       <block wx:for="{{navRightItems[curIndex].tree.nodes[1].tree.nodes}}">  
  16.         <view class="nav_right_items">  
  17.           <navigator url="../list/index?brand={{item.tree.id}}&typeid={{navRightItems[curIndex].id}}">  
  18.             <!--用view包裹圖片組合,如果有圖片就用,無圖片提供就使用50x30的這個默認圖片-->  
  19.             <view>                
  20.               <block wx:if="{{item.tree.logo}}">  
  21.                 <image src="{{item.tree.logo}}"></image>  
  22.               </block>  
  23.               <block wx:else>  
  24.                 <image src="http://temp.im/50x30"></image>  
  25.               </block>  
  26.             </view>  
  27.             <!--如果有文字,就用文字;無文字就用其他-->  
  28.             <view wx:if="{{item.tree.desc}}">  
  29.               <text>{{item.tree.desc}}</text>  
  30.             </view>  
  31.             <view wx:else>  
  32.               <text>{{item.tree.desc2}}</text>  
  33.             </view>  
  34.           </navigator>  
  35.         </view>  
  36.       </block>  
  37.     </view>  
  38.     <!--如果無數據,則顯示數據-->  
  39.     <view wx:else>暫無數據</view>  
  40.   </view>  
  41. </view>  

 

 

 

wxss:

[css]  view plain  copy
 
 
  1. page{  
  2.   background: #f5f5f5;  
  3. }  
  4. /*總體主盒子*/  
  5. .container {  
  6.   position: relative;  
  7.   width: 100%;  
  8.   height: 100%;  
  9.   background-color: #fff;  
  10.   color: #939393;  
  11. }  
  12.   
  13. /*左側欄主盒子*/  
  14. .nav_left{  
  15.   /*設置行內塊級元素(沒使用定位)*/  
  16.   display: inline-block;  
  17.   width: 25%;  
  18.   height: 100%;  
  19.   /*主盒子設置背景色為灰色*/  
  20.   background: #f5f5f5;  
  21.   text-align: center;  
  22. }  
  23. /*左側欄list的item*/  
  24. .nav_left .nav_left_items{  
  25.   /*每個高30px*/  
  26.   height: 30px;  
  27.   /*垂直居中*/  
  28.   line-height: 30px;  
  29.   /*再設上下padding增加高度,總高42px*/  
  30.   padding: 6px 0;  
  31.   /*只設下邊線*/  
  32.   border-bottom: 1px solid #dedede;  
  33.   /*文字14px*/  
  34.   font-size: 14px;  
  35. }  
  36. /*左側欄list的item被選中時*/  
  37. .nav_left .nav_left_items.active{  
  38.   /*背景色變成白色*/  
  39.   background: #fff;  
  40. }  
  41.   
  42. /*右側欄主盒子*/  
  43. .nav_right{  
  44.   /*右側盒子使用了絕對定位*/  
  45.   position: absolute;  
  46.   top: 0;  
  47.   right: 0;  
  48.   flex: 1;  
  49.   /*寬度75%,高度占滿,並使用百分比布局*/  
  50.   width: 75%;  
  51.   height: 100%;  
  52.   padding: 10px;  
  53.   box-sizing: border-box;  
  54.   background: #fff;  
  55. }  
  56. /*右側欄list的item*/  
  57. .nav_right .nav_right_items{  
  58.   /*浮動向左*/  
  59.   float: left;  
  60.   /*每個item設置寬度是33.33%*/  
  61.   width: 33.33%;  
  62.   height: 80px;  
  63.   text-align: center;  
  64. }  
  65. .nav_right .nav_right_items image{  
  66.   /*被圖片設置寬高*/  
  67.   width: 50px;  
  68.   height: 30px;  
  69. }  
  70. .nav_right .nav_right_items text{  
  71.   /*給text設成塊級元素*/  
  72.   display: block;  
  73.   margin-top: 5px;  
  74.   font-size: 10px;  
  75.   /*設置文字溢出部分為...*/  
  76.   overflow: hidden;  
  77.   white-space: nowrap;  
  78.   text-overflow: ellipsis;  
  79. }  


 

js:

[javascript]  view plain  copy
 
 
  1. Page({  
  2.     data: {  
  3.         navLeftItems: [],  
  4.         navRightItems: [],  
  5.         curNav: 1,  
  6.         curIndex: 0  
  7.     },  
  8.     onLoad: function() {  
  9.         // 加載的使用進行網絡訪問,把需要的數據設置到data數據對象  
  10.         var that = this          
  11.         wx.request({  
  12.             url: 'http://huanqiuxiaozhen.com/wemall/goodstype/typebrandList',  
  13.             method: 'GET',  
  14.             data: {},  
  15.             header: {  
  16.                 'Accept': 'application/json'  
  17.             },  
  18.             success: function(res) {  
  19.                 console.log(res)  
  20.                 that.setData({  
  21.                     navLeftItems: res.data,  
  22.                     navRightItems: res.data  
  23.                 })  
  24.             }  
  25.         })  
  26.     },  
  27.   
  28.     //事件處理函數  
  29.     switchRightTab: function(e) {  
  30.         // 獲取item項的id,和數組的下標值  
  31.         let id = e.target.dataset.id,  
  32.             index = parseInt(e.target.dataset.index);  
  33.         // 把點擊到的某一項,設為當前index  
  34.         this.setData({  
  35.             curNav: id,  
  36.             curIndex: index  
  37.         })  
  38.     }  
  39.   
  40. })  

 

demo地址:http://download.csdn.net/detail/michael_ouyang/9816426

 

更多小程序的教程

 

 

微信開發者工具的快捷鍵

微信小程序的文件結構 —— 微信小程序教程系列(1)

微信小程序的生命周期實例演示 —— 微信小程序教程系列(2)

微信小程序的動態修改視圖層的數據 —— 微信小程序教程系列(3)

微信小程序的新建頁面 —— 微信小程序教程系列(4)

微信小程序的如何使用全局屬性 —— 微信小程序教程系列(5)

微信小程序的頁面跳轉 —— 微信小程序教程系列(6)

微信小程序標題欄和導航欄的設置 —— 微信小程序教程系列(7)

微信小程序的作用域和模塊化 —— 微信小程序教程系列(8)

微信小程序視圖層的數據綁定 —— 微信小程序教程系列(9)

微信小程序視圖層的條件渲染 —— 微信小程序教程系列(10)

微信小程序視圖層的列表渲染 —— 微信小程序教程系列(11)

微信小程序視圖層的模板 —— 微信小程序教程系列(12)

微信小程序wxss的尺寸單位rpx —— 微信小程序教程系列(13)

微信小程序的網絡請求 —— 微信小程序教程系列(14)

微信小程序的百度地圖獲取地理位置 —— 微信小程序教程系列(15)

微信小程序使用百度api獲取天氣信息 —— 微信小程序教程系列(16)

微信小程序獲取系統日期和時間 —— 微信小程序教程系列(17)

微信小程序之頂部導航欄實例 —— 微信小程序實戰系列(1)

微信小程序之上拉加載(分頁加載)實例 —— 微信小程序實戰系列(2)

微信小程序之輪播圖實例 —— 微信小程序實戰系列(3)

微信小程序之仿android fragment之可滑動的底部導航欄實例 —— 微信小程序實戰系列(4)

 

微信小程序之登錄頁實例 —— 微信小程序實戰系列(5)

微信小程序之自定義toast實例 —— 微信小程序實戰系列(6)

微信小程序之自定義抽屜菜單(從下拉出)實例 —— 微信小程序實戰系列(7)

微信小程序之自定義模態彈窗(帶動畫)實例 —— 微信小程序實戰系列(8)

 

更多小程序的教程請查看:http://blog.csdn.net/michael_ouyang/article/details/54700871

謝謝觀看,不足之處,敬請指導

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM