js
data: {
cateItems: [{
cate_id: 1,
cate_name: "护肤",
ishaveChild: true,
children: [{
child_id: 1,
name: '洁面皂',
image: ""
},
{
child_id: 2,
name: '卸妆',
image: ""
},
{
child_id: 3,
name: '洁面乳',
image: ""
}
]
},
{
cate_id: 2,
cate_name: "彩妆",
ishaveChild: true,
children: [{
child_id: 1,
name: '淡香水EDT',
image: ""
},
{
child_id: 2,
name: '浓香水EDP',
image: ""
},
{
child_id: 3,
name: '香体走珠',
image: ""
},
{
child_id: 4,
name: '古龙香水男士的最爱',
image: ""
}
]
}
],
curNav: 1,
curIndex: 0
},
//事件处理函数
switchRightTab: function(e) {
// 获取item项的id,和数组的下标值
let id = e.target.dataset.id,
index = parseInt(e.target.dataset.index);
// 把点击到的某一项,设为当前index
this.setData({
curNav: id,
curIndex: index
})
}
js代码有点长,但是逻辑很清晰
1) cateItems 展示的数据
2) curNav 控制当前那个按钮点亮
3) curIndex 根据此参数来拿第几个分类的数据
4) switchRightTab 分类tab事件的处理
cateItems里的数据每一个对象都是一个品类的数据,拿第一个品类护肤来说
1) cate_id 识别的id
2) cate_name 一级分类名称
3) ishaveChild 判断是否有子集
4) children 二级目录的数据
wxml
<!-- 主盒子 -->
<view class="container">
<!-- 主盒子-左侧栏 -->
<view class="nav_left">
<block wx:for="{{cateItems}}">
<!-- 当前项的id等于item项的id,那个就是当前状态
用data-index记录这个数据在数组的下标位置,使用data-id设置每个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>
<!-- 右侧栏 -->
<view class="nav_right">
<!-- 如果有数据,才遍历项 -->
<view wx:if="{{cateItems[curIndex].ishaveChild}}">
<block wx:for="{{cateItems[curIndex].children}}">
<view class="nav_right_items">
<!-- 界面跳转 -->
<navigator url="../../detail/detail}}">
<image src="{{item.image}}"></image>
<text>{{item.name}}</text>
</navigator>
</view>
</block>
</view>
<!-- 如果无数据,则显示数据 -->
<view class="nodata_text" wx:else>该分类暂无数据</view>
</view>
</view>
1.nav_left_items {{curNav == item.cate_id ? ‘active’ : ‘’}} 在js代码中已经说了curNav的作用,就是在这里实现的
2.根据是否和一级目录cate_id相同,来判断是否点亮文字。相同执行.nav_left_items.active样式,不相同则执行.nav_left_items样式
wxss
page {
background: #f5f5f5;
}
/**
*总体主盒子
*/
.container {
position: relative;
width: 100%;
height: 100%;
background-color: #fff;
color: #939393;
}
/**
*左侧栏主盒子
*/
.nav_left {
/*设置行内块级元素(没使用定位)*/
display: inline-block;
width: 25%;
height: 100%;
/*主盒子设置背景色为灰色*/
background: #f5f5f5;
text-align: center;
}
/**
*左侧栏list的item
*/
.nav_left .nav_left_items {
/*每个高30px*/
height: 40px;
/*垂直居中*/
line-height: 40px;
/*再设上下padding增加高度,总高42px*/
padding: 6px 0;
/*只设下边线*/
border-bottom: 1px solid #dedede;
/*文字14px*/
font-size: 14px;
}
/**
*左侧栏list的item被选中时
*/
.nav_left .nav_left_items.active {
/*背景色变成白色*/
background: #fff;
color: #f0145a;
}
/**
*右侧栏主盒子
*/
.nav_right {
/*右侧盒子使用了绝对定位*/
position: absolute;
top: 0;
right: 0;
flex: 1;
/*宽度75%,高度占满,并使用百分比布局*/
width: 75%;
height: 1000px;
padding: 10px;
box-sizing: border-box;
background: #fff;
}
/**
*右侧栏list的item
*/
.nav_right .nav_right_items {
/*浮动向左*/
float: left;
/*每个item设置宽度是33.33%*/
width: 33.33%;
height: 120px;
text-align: center;
}
.nav_right .nav_right_items image {
/*被图片设置宽高*/
width: 60px;
height: 60px;
margin-top: 15px;
}
.nav_right .nav_right_items text {
/*给text设成块级元素*/
display: block;
margin-top: 15px;
font-size: 14px;
color: black;
/*设置文字溢出部分为...*/
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.nodata_text {
color: black;
font-size: 14px;
text-align: center;
}
设置字体垂直居中
【.nav_left .nav_left_items】把height与line-height两个属性设置成一样即可实现字体垂直居中
但有个局限性,是字体要是单行的,因为line-height本身也是设置行高
单行文字过长部分要用省略号(之前的公告中,css也有实现)
overflow: hidden;
white-space: nowrap; //设置单行显示
text-overflow: ellipsis;
拓展
wx:for
微信小程序列表的渲染,我们之前做首页的时候就有接触过用于循环数组,展示列表型数据
默认数组的当前项的下标变量名默认为index,数组当前项的变量名默认为item.
<view wx:for="{{items}}" wx:key="unique">
{{index}}: {{item.message}}
</view>
也可以自定义变量表使用 wx:for-item可以指定数组当前元素的变量名
使用wx:for-index可以指定数组当前下标的变量名
<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName" wx:key="unique">
{{idx}}: {{itemName.message}}
</view>
wx:if
微信小程序条件渲染,通常是在if里面写判断语句,满足条件就执行这个view控件,通常有if对应就有else,对应的不满足if条件就
执行else对应的view控件。
<view wx:if="{{length > 5}}"> 1 </view>
<view wx:elif="{{length > 2}}"> 2 </view>
<view wx:else> 3 </view>
把 wx:if 和 hidden 做对比,他们都可以实现让控件显示与隐藏
if是当满足条件的时候才会渲染view,而hidden是view一定会被渲染,只不过控制显示与隐藏罢了
一般来说,wx:if有更高的切换消耗而hidden有更高的初始渲染消耗。因此,如果需要频繁切换的情景下,用hidden更好,
如果在运行时条件不大可能改变则 wx:if 较好
转载请注明出处!