微信小程序商城項(xiàng)目之側(cè)欄分類效果(1)
在商場項(xiàng)目中,一般都會有分類頁面。
分類頁面可以給用戶快速找到相關(guān)的商品,下面以側(cè)欄分類為例,如下圖
布局分析:
<主盒子>
<左盒子></左盒子>
<右盒子></右盒子>
</主盒子>
左盒子使用標(biāo)準(zhǔn)流
右盒子使用絕對定位(top、right)
wxml:
<!--主盒子--> <view class="container"> <!--左側(cè)欄--> <view class="nav_left"> <block wx:for="{{navLeftItems}}"> <!--當(dāng)前項(xiàng)的id等于item項(xiàng)的id,那個(gè)就是當(dāng)前狀態(tài)--> <!--用data-index記錄這個(gè)數(shù)據(jù)在數(shù)組的下標(biāo)位置,使用data-id設(shè)置每個(gè)item的id值,供打開2級頁面使用--> <view class="nav_left_items {{curNav == item.id ? 'active' : ''}}" bindtap="switchRightTab" data-index="{{index}}" data-id="{{item.id}}">{{item.tree.desc}}</view> </block> </view> <!--右側(cè)欄--> <view class="nav_right"> <!--如果有數(shù)據(jù),才遍歷項(xiàng)--> <view wx:if="{{navRightItems[curIndex].tree.nodes[1].tree.nodes}}"> <block wx:for="{{navRightItems[curIndex].tree.nodes[1].tree.nodes}}"> <view class="nav_right_items"> <navigator url="../list/index?brand={{item.tree.id}}&typeid={{navRightItems[curIndex].id}}"> <!--用view包裹圖片組合,如果有圖片就用,無圖片提供就使用50x30的這個(gè)默認(rèn)圖片--> <view> <block wx:if="{{item.tree.logo}}"> <image src="{{item.tree.logo}}"></image> </block> <block wx:else> <image src="http://temp.im/50x30"></image> </block> </view> <!--如果有文字,就用文字;無文字就用其他--> <view wx:if="{{item.tree.desc}}"> <text>{{item.tree.desc}}</text> </view> <view wx:else> <text>{{item.tree.desc2}}</text> </view> </navigator> </view> </block> </view> <!--如果無數(shù)據(jù),則顯示數(shù)據(jù)--> <view wx:else>暫無數(shù)據(jù)</view> </view> </view>
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{ /*每個(gè)高30px*/ height: 30px; /*垂直居中*/ line-height: 30px; /*再設(shè)上下padding增加高度,總高42px*/ padding: 6px 0; /*只設(shè)下邊線*/ border-bottom: 1px solid #dedede; /*文字14px*/ font-size: 14px; } /*左側(cè)欄list的item被選中時(shí)*/ .nav_left .nav_left_items.active{ /*背景色變成白色*/ background: #fff; } /*右側(cè)欄主盒子*/ .nav_right{ /*右側(cè)盒子使用了絕對定位*/ position: absolute; top: 0; right: 0; flex: 1; /*寬度75%,高度占滿,并使用百分比布局*/ width: 75%; height: 100%; padding: 10px; box-sizing: border-box; background: #fff; } /*右側(cè)欄list的item*/ .nav_right .nav_right_items{ /*浮動(dòng)向左*/ float: left; /*每個(gè)item設(shè)置寬度是33.33%*/ width: 33.33%; height: 80px; text-align: center; } .nav_right .nav_right_items image{ /*被圖片設(shè)置寬高*/ width: 50px; height: 30px; } .nav_right .nav_right_items text{ /*給text設(shè)成塊級元素*/ display: block; margin-top: 5px; font-size: 10px; /*設(shè)置文字溢出部分為...*/ overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
js:
Page({ data: { navLeftItems: [], navRightItems: [], curNav: 1, curIndex: 0 }, onLoad: function() { // 加載的使用進(jìn)行網(wǎng)絡(luò)訪問,把需要的數(shù)據(jù)設(shè)置到data數(shù)據(jù)對象 var that = this wx.request({ url: 'http://huanqiuxiaozhen.com/wemall/goodstype/typebrandList', method: 'GET', data: {}, header: { 'Accept': 'application/json' }, success: function(res) { console.log(res) that.setData({ navLeftItems: res.data, navRightItems: res.data }) } }) }, //事件處理函數(shù) switchRightTab: function(e) { // 獲取item項(xiàng)的id,和數(shù)組的下標(biāo)值 let id = e.target.dataset.id, index = parseInt(e.target.dataset.index); // 把點(diǎn)擊到的某一項(xiàng),設(shè)為當(dāng)前index this.setData({ curNav: id, curIndex: index }) } })
demo地址:側(cè)欄分類效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS中append字符串包含onclick無效傳遞參數(shù)失敗的解決方案
這篇文章主要介紹了JS中append字符串包含onclick無效傳遞參數(shù)失敗的解決方案,需要的朋友可以參考下2016-12-12JavaScript實(shí)現(xiàn)點(diǎn)擊單元格改變背景色的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)點(diǎn)擊單元格改變背景色的方法,涉及JavaScript響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁面元素屬性的相關(guān)技巧,需要的朋友可以參考下2016-02-02JavaScript實(shí)現(xiàn)url地址自動(dòng)檢測并添加URL鏈接示例代碼
寫一個(gè)簡單的聊天系統(tǒng),發(fā)出Htpp的Url實(shí)現(xiàn)跳轉(zhuǎn)加上a標(biāo)簽,下面是具體的實(shí)現(xiàn),感興趣的朋友不要錯(cuò)過2013-11-11如何在js代碼中消滅for循環(huán)實(shí)例詳解
for循環(huán)對大家來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于如何在js代碼中消滅for循環(huán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07