目标:把屏幕按九宫格的方式均分切块,为了方便扩展,增加了 scroll-view
九宫格效果如下:
超出九宫格的效果如下:
http://mpvideo.qpic.cn/0bc3yyac4aaatqafgxfxpjqvbrwdf3daalqa.f10004.mp4?
一. 在 js 文件的 data 中,增加 items
data: {
items: ['BLE', 'WIFI', 'NFC',
'BLE', 'WIFI', 'NFC',
'BLE', 'WIFI', 'NFC'
]
},
二. 在 wxml 文件中,增加如下代码
<!-- 页面根 -->
<view class="pageRoot">
<!-- 页面内容超出屏幕时可滑动 -->
<scroll-view scroll-y="true">
<view class="scrollView">
<view class="content" wx:for="{{items}}" wx:key="index">
<text>{{items[index]}}</text>
</view>
</view>
</scroll-view>
</view>
•用 wx:for 遍历 data(js文件)中的 items,去生成 页面中的每一个方格
三. 在 wxss 文件中做两个步骤
1.定义 scrollView
.scrollView {
display: flex; # 页面显示使用 flex
flex-direction: row; # flex 方向为 行
flex-wrap: wrap;
justify-content: space-between; # 两端对齐,方格之间等间隔
}
2. 定义每一个方格
.content {
width: 33vw; # 定义方格的宽度 为 屏幕宽度的 33 等分, 总共 100 份
height: 33vw;
box-sizing: border-box;
background-color: lightblue;
color: whitesmoke;
display: flex; # 定义 方格中的内容
flex-direction: row;
align-items: center; /* 子 view 垂直居中 */
justify-content: center;
margin-top: 1px;
}
四. 测试
测试效果如开头所示
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有