微信小程序?qū)崙?zhàn)篇-分類頁面制作

哈嘍,大家好,又到周五啦,今天代碼君要教大家分類頁面的制作,廢話不多說,先上效果圖


分類頁面.gif

這個界面布局難度不是很大,css基礎(chǔ)好的,很快就實現(xiàn)了,分類界面,左邊是一級目錄,右邊是一級目錄對應(yīng)的二級目錄,根據(jù)這個需求,我們數(shù)據(jù)設(shè)計的結(jié)構(gòu)一定是數(shù)組嵌套數(shù)組,第一個數(shù)組包含一級目錄數(shù)據(jù),嵌套的數(shù)組包含的是二級目錄的數(shù)據(jù)。

代碼的實現(xiàn)

  1. classify.js
Page({
  data: {
    cateItems: [
      {
        cate_id: 1,
        cate_name: "護膚",
        ishaveChild: true,
        children:
        [
          {
            child_id: 1,
            name: '潔面皂',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117972563.jpg"
          },
          {
            child_id: 2,
            name: '卸妝',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161207/148110444480.jpg"
          },
          {
            child_id: 3,
            name: '潔面乳',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117973270.jpg"
          },
          {
            child_id: 4,
            name: '面部祛角質(zhì)',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161208/148117981591.jpg"
          }
        ]
      },
      {
        cate_id: 2,
        cate_name: "彩妝",
        ishaveChild: true,
        children:
        [
          {
            child_id: 1,
            name: '氣墊bb',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381301.jpg"
          },
          {
            child_id: 2,
            name: '修容/高光',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/14815381411.jpg"
          },
          {
            child_id: 3,
            name: '遮瑕',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815181.jpg"
          },
          {
            child_id: 4,
            name: '腮紅',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153815759.jpg"
          },
          {
            child_id: 5,
            name: '粉餅',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153816983.jpg"
          },
          {
            child_id: 6,
            name: '粉底',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153817721.jpg"
          },
          {
            child_id: 7,
            name: '蜜粉/散粉',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161212/148153819354.jpg"
          },
          {
            child_id: 8,
            name: '隔離霜',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161215/148179053369.jpg"
          }
        ]
      },
      {
        cate_id: 3,
        cate_name: "香水/香氛",
        ishaveChild: true,
        children:
        [
          {
            child_id: 1,
            name: '淡香水EDT',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815978910.jpg"
          },
          {
            child_id: 2,
            name: '濃香水EDP',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159789883.jpg"
          },
          {
            child_id: 3,
            name: '香體走珠',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/14815979307.jpg"
          },
          {
            child_id: 4,
            name: '古龍香水男士的最愛',
            image: "http://mz.djmall.xmisp.cn/files/logo/20161213/148159765589.jpg"
          }
        ]
      },
      {
        cate_id: 4,
        cate_name: "個人護理",
        ishaveChild: false,
        children: []
      }
    ],
    curNav: 1,
    curIndex: 0
  },

  //事件處理函數(shù)  
  switchRightTab: function (e) {
    // 獲取item項的id,和數(shù)組的下標(biāo)值  
    let id = e.target.dataset.id,
      index = parseInt(e.target.dataset.index);
    // 把點擊到的某一項,設(shè)為當(dāng)前index  
    this.setData({
      curNav: id,
      curIndex: index
    })
  }
})  

js的代碼有點長,但是宏觀看一下邏輯就很清晰了

  • cateItems 展示的數(shù)據(jù)
  • curNav 控制當(dāng)前那個按鈕點亮
  • curIndex 根據(jù)此參數(shù)來拿第幾個分類的數(shù)據(jù)
  • switchRightTab 分類tab事件的處理

cateItems里的數(shù)據(jù)每一個對象都是一個品類的數(shù)據(jù),拿第一個品類護膚來說,

  • cate_id 識別的id
  • cate_name 一級分類名稱
  • ishaveChild 判斷是否有子集
  • children 二級目錄的數(shù)據(jù)
  1. classify.wxml
<!--主盒子-->
<view class="container">
  <!--左側(cè)欄-->
  <view class="nav_left">
    <block wx:for="{{cateItems}}">
      <!--當(dāng)前項的id等于item項的id,那個就是當(dāng)前狀態(tài)-->
      <!--用data-index記錄這個數(shù)據(jù)在數(shù)組的下標(biāo)位置,使用data-id設(shè)置每個item的id值,供打開2級頁面使用-->
      <view class="nav_left_items {{curNav == item.cate_id ? 'active' : ''}}" bindtap="switchRightTab" data-index="{{index}}" data-id="{{item.cate_id}}">{{item.cate_name}}</view>
    </block>
  </view>
  <!--右側(cè)欄-->
  <view class="nav_right">
    <!--如果有數(shù)據(jù),才遍歷項-->
    <view wx:if="{{cateItems[curIndex].ishaveChild}}">
      <block wx:for="{{cateItems[curIndex].children}}">
        <view class="nav_right_items">
        <!--界面跳轉(zhuǎn) -->
          <navigator url="../../detail/detail}}">
            <image src="{{item.image}}"></image>
            <text>{{item.name}}</text>
          </navigator>
        </view>
      </block>
    </view>
    <!--如果無數(shù)據(jù),則顯示數(shù)據(jù)-->
    <view class="nodata_text" wx:else>該分類暫無數(shù)據(jù)</view>
  </view>
</view>

這里面要講解的有

  • nav_left_items {{curNav == item.cate_id ? 'active' : ''}} 在classify.js代碼中已經(jīng)說了curNav的作用,就是在這里實現(xiàn)的,根據(jù)是否和一級目錄cate_id相同,來判斷是否點亮文字。相同執(zhí)行.nav_left_items.active樣式,不相同則執(zhí)行.nav_left_items樣式
  • wx:for 和wx: if的知識點,這放在下面講,請繼續(xù)往下看
  1. classify.wxss
page{  
  background: #f5f5f5;  
}  
/*總體主盒子*/  
.container {  
  position: relative;  
  width: 100%;  
  height: 100%;  
  background-color: #fff;  
  color: #939393;  
}
 /*左側(cè)欄主盒子*/  
.nav_left{  
  /*設(shè)置行內(nèi)塊級元素(沒使用定位)*/  
  display: inline-block;  
  width: 25%;  
  height: 100%;  
  /*主盒子設(shè)置背景色為灰色*/  
  background: #f5f5f5;  
  text-align: center;  
}  
/*左側(cè)欄list的item*/  
.nav_left .nav_left_items{  
  /*每個高30px*/  
  height: 40px;  
  /*垂直居中*/  
  line-height: 40px;  
  /*再設(shè)上下padding增加高度,總高42px*/  
  padding: 6px 0;  
  /*只設(shè)下邊線*/  
  border-bottom: 1px solid #dedede;  
  /*文字14px*/  
  font-size: 14px; 
}  
/*左側(cè)欄list的item被選中時*/  
.nav_left .nav_left_items.active{  
  /*背景色變成白色*/  
  background: #fff;  
  color: #f0145a; 
}  
/*右側(cè)欄主盒子*/  
.nav_right{  
  /*右側(cè)盒子使用了絕對定位*/  
  position: absolute;  
  top: 0;  
  right: 0;  
  flex: 1;  
  /*寬度75%,高度占滿,并使用百分比布局*/  
  width: 75%;  
  height: 1000px;  
  padding: 10px;  
  box-sizing: border-box;  
  background: #fff;  
}  
/*右側(cè)欄list的item*/  
.nav_right .nav_right_items{  
  /*浮動向左*/  
  float: left;  
  /*每個item設(shè)置寬度是33.33%*/  
  width: 33.33%;  
  height: 120px;  
  text-align: center;  
}  
.nav_right .nav_right_items image{  
  /*被圖片設(shè)置寬高*/  
  width: 60px;  
  height: 60px;  
  margin-top: 15px;  
}  
.nav_right .nav_right_items text{  
  /*給text設(shè)成塊級元素*/  
  display: block;  
  margin-top: 15px;  
  font-size: 14px;  
  color: black;
  /*設(shè)置文字溢出部分為...*/  
  overflow: hidden;  
  white-space: nowrap;  
  text-overflow: ellipsis;  
} 
.nodata_text
{
  color: black;
  font-size: 14px;  
  text-align: center;  
} 

這里有個小技巧分享給大家

  • 要設(shè)置字體垂直居中要腫么辦吶?
    看我布局的樣式【.nav_left .nav_left_items】把height與line-height兩個屬性設(shè)置成一樣的就可以輕松實現(xiàn)字體垂直居中,但是這個有局限性,是字體要是單行的,為什么吶,因為line-height本身就是設(shè)置行高
  • 單行文字過長部分要用省略號應(yīng)該如何寫樣式吶? 效果是醬紫 xxxxx...
    overflow: hidden;
    white-space: nowrap; //設(shè)置單行顯示
    text-overflow: ellipsis;

知識小課堂

  1. wx:for
    微信小程序列表的渲染,我們之前做首頁的時候就有接觸過用于循環(huán)數(shù)組,展示列表型數(shù)據(jù)
  • 默認(rèn)數(shù)組的當(dāng)前項的下標(biāo)變量名默認(rèn)為index,數(shù)組當(dāng)前項的變量名默認(rèn)為item
<view wx:for="{{items}}">
  {{index}}: {{item.message}}
</view>
  • 也可以自定義變量表使用 wx:for-item可以指定數(shù)組當(dāng)前元素的變量名
    使用wx:for-index可以指定數(shù)組當(dāng)前下標(biāo)的變量名
<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">
  {{idx}}: {{itemName.message}}
</view>
  1. wx:if
    微信小程序條件渲染,通常是在if里面寫判斷語句,滿足條件就執(zhí)行這個view控件,通常有if對應(yīng)就有else,對應(yīng)的不滿足if條件就執(zhí)行else對應(yīng)的view控件
<view wx:if="{{length > 5}}"> 1 </view>
<view wx:elif="{{length > 2}}"> 2 </view>
<view wx:else> 3 </view>

我們要把** wx:if 和 hidden **做對比,他們都可以實現(xiàn)讓控件顯示與隱藏,但是他們有什么區(qū)別吶,if是當(dāng)滿足條件的時候才會渲染view,而hidden是view一定會被渲染,只不過控制顯示與隱藏罷了,那么我們要如何區(qū)分什么時機用什么方法吶?
一般來說,wx:if有更高的切換消耗而hidden有更高的初始渲染消耗。因此,如果需要頻繁切換的情景下,用hidden更好,如果在運行時條件不大可能改變則wx:if較好。

總結(jié)

好了,分類頁面的制作完成了,今天新增一個知識小課堂,目的很簡單,就是想把涉及到的知識要點歸類整理,方便讀者查閱。今天就到這,祝大家周末愉快~

上一篇:微信小程序?qū)崙?zhàn)篇-下拉刷新與加載更多

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 文章分類 后臺文章分類列表頁模板導(dǎo)的詳細步驟建立數(shù)據(jù)表blog_category,并添加相應(yīng)的文章字段使用php ...
    JoyceZhao閱讀 1,870評論 0 14
  • 個人學(xué)習(xí)批處理的初衷來源于實際工作;在某個迭代版本有個BS(安卓手游模擬器)大需求,從而在測試過程中就重復(fù)涉及到...
    Luckykailiu閱讀 4,990評論 0 11
  • WXML WXML(WeiXin Markup Language)是微信的一套標(biāo)簽語言,結(jié)合基礎(chǔ)組件、事件系統(tǒng),可...
    許劍鋒閱讀 7,290評論 3 51
  • 以下劇透,未觀影的同學(xué)請自覺跳過此段。故事在說美國NASA的一個火星項目組,準(zhǔn)備在火星上研究一下有關(guān)生存環(huán)境方面的...
    iOSTbag閱讀 957評論 0 0
  • 其實對于表達我一直談不上自信,論語言功底文字內(nèi)容在頭馬沒有幾位讓我覺得比較厲害的,最讓我佩服的倒是從容自信抑揚頓挫...
    DennisFly閱讀 165評論 0 0

友情鏈接更多精彩內(nèi)容