uni-grid.vue
4.3 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<template>
<view class="uni-grid" :class="{'uni-grid-no-border':!showBorder,'uni-grid-no-out-border':showBorder && !showOutBorder}">
<view class="uni-grid__flex" v-for="(items,i) in gridGroup" :key="i">
<view class="uni-grid-item" hover-class="uni-grid-item-hover" :hover-start-time="20" :hover-stay-time="70" v-for="(item,index) in items"
:key="index" :class="[index == columnNum ? 'uni-grid-item-last' : '','uni-grid-item-' + type]" @click="onClick(i,index,item.url)" :style="{visibility:item.seize ? 'hidden' : 'inherit'}">
<view class="uni-grid-item__content" v-if="!item.seize">
<image class="uni-grid-item-image" :src="item.image"></image>
<text class="uni-grid-item-text">{{item.text}}</text>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
name: "uni-grid",
props: {
options: Array,
type: { //布局格式,长方形oblong,正方形square
type: String,
default: 'square'
},
columnNum: { //每一行有多少个
type: [Number, String],
default: 3
},
showOutBorder: { //显示外边框
type: [Boolean, String],
default: true
},
showBorder: { //是否显示border,如果为false,showOutBorder无效
type: [Boolean, String],
default: true
}
},
data() {
return {}
},
created() {
this.columnNumber = this.gridGroup[0].length
},
computed: {
gridGroup() {
let group = []
let groupItem = []
this.options && this.options.forEach((item, index) => {
groupItem.push(item)
if (index % this.columnNum === this.columnNum - 1) {
group.push(groupItem)
groupItem = []
}
})
if (groupItem.length > 0) {
if (this.columnNum > groupItem.length) {
for (let i = 0,length = groupItem.length; i < this.columnNum - length; i++) {
groupItem.push({seize:true})
}
}
group.push(groupItem)
}
groupItem = null
return group
}
},
methods: {
onClick(index, num,url) {
uni.navigateTo({
url: url
});
}
}
}
</script>
<style>
@charset "UTF-8";
.uni-grid {
position: relative;
display: flex;
flex-direction: column;
}
.uni-grid__flex {
display: flex;
flex-direction: row;
}
.uni-grid-item {
display: flex;
position: relative;
flex-direction: column;
background-color: #FFFFFF;
flex: 1
}
.uni-grid-item:before {
display: block;
content: " ";
padding-bottom: 100%
}
.uni-grid-item:after {
content: '';
position: absolute;
z-index: 1;
transform-origin: center;
box-sizing: border-box;
top: -50%;
left: -50%;
right: -50%;
bottom: -50%;
border-color: #c8c7cc;
border-style: solid;
border-width: 1px;
-webkit-transform: scale(.5);
transform: scale(.5);
border-top-width: 0;
border-left-width: 0
}
.uni-grid-item__content {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center
}
.uni-grid-item-text {
font-size: 32upx;
color: #333;
margin-top: 12upx
}
.uni-grid-item-hover {
background-color: #f1f1f1
}
.uni-grid-item-image {
width: 80upx;
height: 80upx
}
.uni-grid .uni-grid__flex:first-child .uni-grid-item:after {
border-top-width: 1px
}
.uni-grid .uni-grid__flex .uni-grid-item:first-child:after {
border-left-width: 1px
}
.uni-grid.uni-grid-no-out-border .uni-grid__flex:first-child .uni-grid-item:after {
border-top-width: 0
}
.uni-grid.uni-grid-no-out-border .uni-grid__flex:last-child .uni-grid-item:after {
border-bottom-width: 0
}
.uni-grid.uni-grid-no-out-border .uni-grid__flex .uni-grid-item:first-child:after {
border-left-width: 0
}
.uni-grid.uni-grid-no-out-border .uni-grid__flex .uni-grid-item:last-child:after {
border-right-width: 0
}
.uni-grid.uni-grid-no-border .uni-grid-item:after {
border-width: 0
}
.uni-grid.uni-grid-no-border .uni-grid__flex:first-child .uni-grid-item:after {
border-top-width: 0
}
.uni-grid.uni-grid-no-border .uni-grid__flex .uni-grid-item:first-child:after {
border-left-width: 0
}
.uni-grid-item-oblong.uni-grid-item:before {
padding-bottom: 60%
}
.uni-grid-item-oblong .uni-grid-item__content {
flex-direction: row
}
.uni-grid-item-oblong .uni-grid-item-image {
width: 52upx;
height: 52upx
}
.uni-grid-item-oblong .uni-grid-item-text {
margin-top: 0;
margin-left: 12upx
}
</style>