本文以一个简单的小例子,简述在微信小程序开发中左右布局功能的实现方式,主要涉及scroll-view ,列表数据绑定,及简单样式等内容,仅供学习分享使用。
在微信小程序开发中,左右分栏(左边显示分类,右边显示明细,然后进行联动)是一种常见的布局方式,多应用于点餐,冷饮店,外卖,以及其他类似的商城。
布局分析图示如下:
示例效果图如下所示:
WXML代码如下:
1 <!--pages/show/show.wxml--> 2 <view class="show-info"> 3 <scroll-view class='left' scroll-y> 4 <view class="jy-item" wx:for="{{jytype}}" wx:key="id" hover-class="jy-item-hover" wx:for-item="item" bindtap='showItem' data-id="{{item.id}}"> 5 <image src="{{item.url}}"></image> 6 <label>{{item.name}}</label> 7 </view> 8 </scroll-view> 9 <scroll-view class='right' scroll-y scroll-into-view="{{viewId}}"> 10 <view class="jy-detail" wx:for="{{jydetail}}" wx:key="id" id= "D-{{detail.typeid}}-{{detail.id}}" wx:for-item="detail" bindtap='showDetail' data-id="{{detail.id}}"> 11 <image src="{{detail.url}}"></image> 12 <label>{{detail.name}}</label> 13 </view> 14 </scroll-view> 15 </view>JS代码如下:
1 showItem: function(event) { 2 var that=this; 3 var viewId = "D-" + event.currentTarget.dataset.id + "-" + event.currentTarget.dataset.id+"00"; 4 that.setData({ 5 viewId: viewId 6 }); 7 console.log(viewId); 8 },WXSS布局如下,此处主要用到了盒子布局(display: flex;flex-direction: row;):
1 .show-info { 2 height: 100%; 3 display: flex; 4 flex-direction: row; 5 align-items: flex-start; 6 padding: 10rpx 0; 7 box-sizing: border-box; 8 } 9 10 .left { 11 width: 30%; 12 height: 100%; 13 display: flex; 14 flex-direction: column; 15 margin:2px; 16 } 17 18 .jy-item-hover{ 19 border: none; 20 } 21 22 .right { 23 width: 70%; 24 height: 1200rpx; 25 display: flex; 26 flex-direction: column; 27 margin: 2px; 28 }学而时习之,不亦说乎。