微信小程序九宮格實現


1.九宮格實現示例圖:


Tips: 說明:

使用 display: -webkit-flex;對布局做兼容。
保證每排三個圖標的兩對對齊,采用:justify-content: space-between;屬性。
父元素必須設置 flex-wrap: wrap;屬性進行換行,否則會在一排展示。
每個子元素的寬度width: 33.33333333%;按照其占位大小分配。
使用justify-content:center;屬性設置元素居中。
使用flex-direction: column;和flex-wrap: wrap;屬性設置icon和text元素豎直排列。
2.在 .js文件中定義數據源:

Page({

/**
* 頁面數據源
*/
data: {
iconColor: [
'red', 'orange', 'yellow', 'green', 'rgb(0,255,255)', 'blue', 'purple'
],
iconStyle: [
{
"type":"success",
"size":30,
"color":"#32CD32"
},
{
"type": "success_no_circle",
"size": 30,
"color": "orange"
},
{
"type": "info",
"size": 30,
"color": "yellow"
},
{
"type": "warn",
"size": 30,
"color": "green"
},
{
"type": "waiting",
"size": 30,
"color": "rgb(0,255,255)"
},
{
"type": "cancel",
"size": 30,
"color": "blue"
},
{
"type": "download",
"size": 30,
"color": "purple"
},
{
"type": "search",
"size": 30,
"color": "#C4C4C4"
},
{
"type": "clear",
"size": 30,
"color": "red"
}
]
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2.在 .wxss文件中定義樣式如下:

/*
九宮格容器布局樣式
*/
.grid-item-container {
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
border-top: 1rpx solid #D9D9D9;
}

/*
item容器樣式
*/
.grid-item-child {
display:flex;
display: -webkit-flex;
justify-content:center;
flex-direction: column;
flex-wrap: wrap;
float: left;
width: 33.33333333%;
height: 200rpx;
box-sizing: border-box;
background-color: #FFFFFF;
border-right: 1rpx solid #D9D9D9;
border-bottom: 1rpx solid #D9D9D9;
}

/*
icon樣式
*/
.grid-item-icon {
display:flex;
display: -webkit-flex;
justify-content:center;
}

/*
文本樣式
*/
.grid-item-label {
display:flex;
display: -webkit-flex;
justify-content:center;
color: #666;
font-size: 14px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
3.在 .wxml文件中具體使用:

<view class='grid-item-container'>
<block wx:for="{{iconStyle}}" wx:key="index">
<view class='grid-item-child'>
<view>
<icon class='grid-item-icon' type='{{item.type}}' size='{{item.size}}' color='{{item.color}}'/>
<text class='grid-item-label'>{{item.type}}</text>
</view>
</view>
</block>
</view>
————————————————
版權聲明:本文為CSDN博主「秦川小將」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/mjb00000/java/article/details/90513424


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM