grid.nvue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="warp">
  3. <uni-card is-full :is-shadow="false">
  4. <text class="uni-h6">宫格组件主要使用场景如:商品推荐列表、热门内容等</text>
  5. </uni-card>
  6. <uni-section title="基础样式" type="line" padding>
  7. <uni-grid :column="4" :highlight="true" @change="change">
  8. <uni-grid-item v-for="(item, index) in 4" :index="index" :key="index">
  9. <view class="grid-item-box" style="background-color: #fff;">
  10. <uni-icons type="image" :size="30" color="#777" />
  11. <text class="text">文本信息</text>
  12. </view>
  13. </uni-grid-item>
  14. </uni-grid>
  15. </uni-section>
  16. <uni-section title="自定义列数" type="line" padding>
  17. <uni-grid :column="4" :highlight="true" @change="change">
  18. <uni-grid-item v-for="(item, index) in 8" :index="index" :key="index">
  19. <view class="grid-item-box" style="background-color: #fff;">
  20. <uni-icons type="image" :size="30" color="#777" />
  21. <text class="text">文本信息</text>
  22. </view>
  23. </uni-grid-item>
  24. </uni-grid>
  25. </uni-section>
  26. <uni-section title="滑动视图" type="line" padding>
  27. <!-- 因为swiper特性的关系,请指定swiper的高度 ,swiper的高度并不会被内容撑开-->
  28. <swiper class="swiper" :indicator-dots="true">
  29. <swiper-item>
  30. <uni-grid :column="3" :highlight="true" @change="change">
  31. <uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
  32. <view class="grid-item-box">
  33. <image :src="item.url" class="image" mode="aspectFill" />
  34. <text class="text">{{ item.text }}</text>
  35. </view>
  36. </uni-grid-item>
  37. </uni-grid>
  38. </swiper-item>
  39. <swiper-item>
  40. <uni-grid :column="3" :highlight="true" @change="change">
  41. <uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
  42. <view class="grid-item-box">
  43. <image :src="item.url" class="image" mode="aspectFill" />
  44. <text class="text">{{ item.text }}</text>
  45. </view>
  46. </uni-grid-item>
  47. </uni-grid>
  48. </swiper-item>
  49. <swiper-item>
  50. <uni-grid :column="3" :highlight="true" @change="change">
  51. <uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
  52. <view class="grid-item-box">
  53. <image :src="item.url" class="image" mode="aspectFill" />
  54. <text class="text">{{ item.text }}</text>
  55. </view>
  56. </uni-grid-item>
  57. </uni-grid>
  58. </swiper-item>
  59. </swiper>
  60. </uni-section>
  61. <uni-section title="动态加载" type="line" padding>
  62. <view class="grid-dynamic-box">
  63. <uni-grid :column="3" :highlight="true" @change="change">
  64. <uni-grid-item v-for="(item, index) in dynamicList" :index="index" :key="index">
  65. <view class="grid-item-box" :style="{'backgroundColor':item.color}">
  66. <image :src="item.url" class="image" mode="aspectFill" />
  67. <text class="text">{{ item.text }}</text>
  68. </view>
  69. </uni-grid-item>
  70. </uni-grid>
  71. </view>
  72. <button type="primary" @click="add">点击添加一个宫格</button>
  73. <button v-if="dynamicList.length !== 0" type="primary" style="margin-top: 15px;"
  74. @click="del">点击删除一个宫格</button>
  75. </uni-section>
  76. <uni-section title="无边框带角标(3列)" type="line" padding>
  77. <uni-grid :column="3" :show-border="false" :square="false" @change="change">
  78. <uni-grid-item v-for="(item ,index) in list" :index="index" :key="index">
  79. <view class="grid-item-box">
  80. <image class="image" :src="item.url" mode="aspectFill" />
  81. <text class="text">{{item.text}}</text>
  82. <view v-if="item.badge" class="grid-dot">
  83. <uni-badge :text="item.badge" :type="item.type" />
  84. </view>
  85. </view>
  86. </uni-grid-item>
  87. </uni-grid>
  88. </uni-section>
  89. <uni-section title="矩形宫格(3列)" type="line" padding>
  90. <uni-grid :column="3" :square="false" :highlight="false" @change="change">
  91. <uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
  92. <view class="grid-item-box">
  93. <image :src="item.url" class="image" mode="aspectFill" />
  94. <text class="text">{{ item.text }}</text>
  95. </view>
  96. </uni-grid-item>
  97. </uni-grid>
  98. </uni-section>
  99. <uni-section title="边框颜色(4列 无文字)" type="line" padding>
  100. <uni-grid :column="4" border-color="#03a9f4" @change="change">
  101. <uni-grid-item :index="0">
  102. <view class="grid-item-box">
  103. <image class="image" src="/static/c1.png" mode="aspectFill" />
  104. </view>
  105. </uni-grid-item>
  106. <uni-grid-item :index="1">
  107. <view class="grid-item-box">
  108. <image class="image" src="/static/c2.png" mode="aspectFill" />
  109. </view>
  110. </uni-grid-item>
  111. <uni-grid-item :index="2">
  112. <view class="grid-item-box">
  113. <image class="image" src="/static/c3.png" mode="aspectFill" />
  114. </view>
  115. </uni-grid-item>
  116. <uni-grid-item :index="3">
  117. <view class="grid-item-box">
  118. <image class="image" src="/static/c4.png" mode="aspectFill" />
  119. </view>
  120. </uni-grid-item>
  121. </uni-grid>
  122. </uni-section>
  123. </view>
  124. </template>
  125. <script>
  126. export default {
  127. components: {},
  128. data() {
  129. return {
  130. dynamicList: [],
  131. list: [{
  132. url: '/static/c1.png',
  133. text: 'Grid 1',
  134. badge: '0',
  135. type: "primary"
  136. },
  137. {
  138. url: '/static/c2.png',
  139. text: 'Grid 2',
  140. badge: '1',
  141. type: "success"
  142. },
  143. {
  144. url: '/static/c3.png',
  145. text: 'Grid 3',
  146. badge: '99',
  147. type: "warning"
  148. },
  149. {
  150. url: '/static/c4.png',
  151. text: 'Grid 4',
  152. badge: '2',
  153. type: "error"
  154. },
  155. {
  156. url: '/static/c5.png',
  157. text: 'Grid 5'
  158. },
  159. {
  160. url: '/static/c6.png',
  161. text: 'Grid 6'
  162. },
  163. {
  164. url: '/static/c7.png',
  165. text: 'Grid 7'
  166. },
  167. {
  168. url: '/static/c8.png',
  169. text: 'Grid 8'
  170. },
  171. {
  172. url: '/static/c9.png',
  173. text: 'Grid 9'
  174. }
  175. ]
  176. }
  177. },
  178. methods: {
  179. change(e) {
  180. let {
  181. index
  182. } = e.detail
  183. this.list[index].badge && this.list[index].badge++
  184. uni.showToast({
  185. title: `点击第${index+1}个宫格`,
  186. icon: 'none'
  187. })
  188. },
  189. add() {
  190. if (this.dynamicList.length < 9) {
  191. this.dynamicList.push({
  192. url: `/static/c${this.dynamicList.length+1}.png`,
  193. text: `Grid ${this.dynamicList.length+1}`,
  194. color: this.dynamicList.length % 2 === 0 ? '#f5f5f5' : "#fff"
  195. })
  196. } else {
  197. uni.showToast({
  198. title: '最多添加9个',
  199. icon: 'none'
  200. });
  201. }
  202. },
  203. del() {
  204. this.dynamicList.splice(this.dynamicList.length - 1, 1)
  205. }
  206. }
  207. }
  208. </script>
  209. <style lang="scss">
  210. .image {
  211. width: 50rpx;
  212. height: 50rpx;
  213. }
  214. .text {
  215. font-size: 26rpx;
  216. margin-top: 10rpx;
  217. }
  218. .example-body {
  219. /* #ifndef APP-NVUE */
  220. // display: block;
  221. /* #endif */
  222. }
  223. .grid-dynamic-box {
  224. margin-bottom: 15px;
  225. }
  226. .grid-item-box {
  227. flex: 1;
  228. // position: relative;
  229. /* #ifndef APP-NVUE */
  230. display: flex;
  231. /* #endif */
  232. flex-direction: column;
  233. align-items: center;
  234. justify-content: center;
  235. padding: 15px 0;
  236. }
  237. .grid-item-box-row {
  238. flex: 1;
  239. // position: relative;
  240. /* #ifndef APP-NVUE */
  241. display: flex;
  242. /* #endif */
  243. flex-direction: row;
  244. align-items: center;
  245. justify-content: center;
  246. padding: 15px 0;
  247. }
  248. .grid-dot {
  249. position: absolute;
  250. top: 5px;
  251. right: 15px;
  252. }
  253. .swiper {
  254. height: 420px;
  255. }
  256. /* #ifdef H5 */
  257. @media screen and (min-width: 768px) and (max-width: 1425px) {
  258. .swiper {
  259. height: 630px;
  260. }
  261. }
  262. @media screen and (min-width: 1425px) {
  263. .swiper {
  264. height: 830px;
  265. }
  266. }
  267. /* #endif */
  268. </style>