grid.nvue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view class="warp">
  3. <text class="example-info">宫格组件主要使用场景如:商品推荐列表、热门内容等</text>
  4. <uni-section title="默认样式(3列)" type="line"></uni-section>
  5. <view class="example-body">
  6. <uni-grid :column="3" :highlight="true" @change="change">
  7. <uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
  8. <view class="grid-item-box" style="background-color: #fff;">
  9. <image :src="item.url" class="image" mode="aspectFill" />
  10. <text class="text">{{ item.text }}</text>
  11. </view>
  12. </uni-grid-item>
  13. </uni-grid>
  14. </view>
  15. <uni-section title="滑动视图" type="line"></uni-section>
  16. <view class="example-body">
  17. <!-- 因为swiper特性的关系,请指定swiper的高度 ,swiper的高度并不会被内容撑开-->
  18. <swiper class="swiper" :indicator-dots="true">
  19. <swiper-item>
  20. <uni-grid :column="3" :highlight="true" @change="change">
  21. <uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
  22. <view class="grid-item-box">
  23. <image :src="item.url" class="image" mode="aspectFill" />
  24. <text class="text">{{ item.text }}</text>
  25. </view>
  26. </uni-grid-item>
  27. </uni-grid>
  28. </swiper-item>
  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>
  50. </view>
  51. <uni-section title="动态加载" type="line"></uni-section>
  52. <view class="example-body">
  53. <view class="grid-dynamic-box">
  54. <uni-grid :column="3" :highlight="true" @change="change">
  55. <uni-grid-item v-for="(item, index) in dynamicList" :index="index" :key="index">
  56. <view class="grid-item-box" :style="{'backgroundColor':item.color}">
  57. <image :src="item.url" class="image" mode="aspectFill" />
  58. <text class="text">{{ item.text }}</text>
  59. </view>
  60. </uni-grid-item>
  61. </uni-grid>
  62. </view>
  63. <button type="primary" @click="add">点击添加一个宫格</button>
  64. <button v-if="dynamicList.length !== 0" type="primary" style="margin-top: 15px;" @click="del">点击删除一个宫格</button>
  65. </view>
  66. <uni-section title="无边框带角标(3列)" type="line"></uni-section>
  67. <view class="example-body">
  68. <uni-grid :column="3" :show-border="false" :square="false" @change="change">
  69. <uni-grid-item v-if="index < 6" v-for="(item ,index) in list" :index="index" :key="index">
  70. <view class="grid-item-box">
  71. <image class="image" :src="item.url" mode="aspectFill" />
  72. <text class="text">{{item.text}}</text>
  73. <view v-if="item.badge" class="grid-dot">
  74. <uni-badge :text="item.badge" :type="item.type" />
  75. </view>
  76. </view>
  77. </uni-grid-item>
  78. </uni-grid>
  79. </view>
  80. <uni-section title="矩形宫格(3列)" type="line"></uni-section>
  81. <view class="example-body">
  82. <uni-grid :column="3" :square="false" :highlight="false" @change="change">
  83. <uni-grid-item v-for="(item, index) in list" :index="index" :key="index">
  84. <view class="grid-item-box">
  85. <image :src="item.url" class="image" mode="aspectFill" />
  86. <text class="text">{{ item.text }}</text>
  87. </view>
  88. </uni-grid-item>
  89. </uni-grid>
  90. </view>
  91. <uni-section title="边框颜色(4列 无文字)" type="line"></uni-section>
  92. <view class="example-body">
  93. <uni-grid :column="4" border-color="#03a9f4" @change="change">
  94. <uni-grid-item :index="0">
  95. <view class="grid-item-box">
  96. <image class="image" src="/static/c1.png" mode="aspectFill" />
  97. </view>
  98. </uni-grid-item>
  99. <uni-grid-item :index="1">
  100. <view class="grid-item-box">
  101. <image class="image" src="/static/c2.png" mode="aspectFill" />
  102. </view>
  103. </uni-grid-item>
  104. <uni-grid-item :index="2">
  105. <view class="grid-item-box">
  106. <image class="image" src="/static/c3.png" mode="aspectFill" />
  107. </view>
  108. </uni-grid-item>
  109. <uni-grid-item :index="3">
  110. <view class="grid-item-box">
  111. <image class="image" src="/static/c4.png" mode="aspectFill" />
  112. </view>
  113. </uni-grid-item>
  114. </uni-grid>
  115. </view>
  116. </view>
  117. </template>
  118. <script>
  119. export default {
  120. components: {},
  121. data() {
  122. return {
  123. dynamicList: [],
  124. list: [{
  125. url: '/static/c1.png',
  126. text: 'Grid 1',
  127. badge: '0',
  128. type: "primary"
  129. },
  130. {
  131. url: '/static/c2.png',
  132. text: 'Grid 2',
  133. badge: '1',
  134. type: "success"
  135. },
  136. {
  137. url: '/static/c3.png',
  138. text: 'Grid 3',
  139. badge: '99',
  140. type: "warning"
  141. },
  142. {
  143. url: '/static/c4.png',
  144. text: 'Grid 4',
  145. badge: '2',
  146. type: "error"
  147. },
  148. {
  149. url: '/static/c5.png',
  150. text: 'Grid 5'
  151. },
  152. {
  153. url: '/static/c6.png',
  154. text: 'Grid 6'
  155. },
  156. {
  157. url: '/static/c7.png',
  158. text: 'Grid 7'
  159. },
  160. {
  161. url: '/static/c8.png',
  162. text: 'Grid 8'
  163. },
  164. {
  165. url: '/static/c9.png',
  166. text: 'Grid 9'
  167. }
  168. ]
  169. }
  170. },
  171. methods: {
  172. change(e) {
  173. let {
  174. index
  175. } = e.detail
  176. this.list[index].badge && this.list[index].badge++
  177. uni.showToast({
  178. title: `点击第${index+1}个宫格`,
  179. icon: 'none'
  180. })
  181. },
  182. add() {
  183. if (this.dynamicList.length < 9) {
  184. this.dynamicList.push({
  185. url: `/static/c${this.dynamicList.length+1}.png`,
  186. text: `Grid ${this.dynamicList.length+1}`,
  187. color: this.dynamicList.length % 2 === 0 ? '#f5f5f5' : "#fff"
  188. })
  189. } else {
  190. uni.showToast({
  191. title: '最多添加9个',
  192. icon: 'none'
  193. });
  194. }
  195. },
  196. del() {
  197. this.dynamicList.splice(this.dynamicList.length - 1, 1)
  198. }
  199. }
  200. }
  201. </script>
  202. <style>
  203. @charset "UTF-8";
  204. /* 头条小程序组件内不能引入字体 */
  205. /* #ifdef MP-TOUTIAO */
  206. @font-face {
  207. font-family: uniicons;
  208. font-weight: normal;
  209. font-style: normal;
  210. src: url("~@/static/uni.ttf") format("truetype");
  211. }
  212. /* #endif */
  213. /* #ifndef APP-NVUE */
  214. page {
  215. display: flex;
  216. flex-direction: column;
  217. box-sizing: border-box;
  218. background-color: #efeff4;
  219. min-height: 100%;
  220. height: auto;
  221. }
  222. view {
  223. font-size: 14px;
  224. line-height: inherit;
  225. }
  226. .example {
  227. padding: 0 15px 15px;
  228. }
  229. .example-info {
  230. padding: 15px;
  231. color: #3b4144;
  232. background: #ffffff;
  233. }
  234. .example-body {
  235. /* #ifndef APP-NVUE */
  236. display: flex;
  237. /* #endif */
  238. flex-direction: row;
  239. flex-wrap: wrap;
  240. justify-content: center;
  241. padding: 0;
  242. font-size: 14px;
  243. background-color: #ffffff;
  244. }
  245. /* #endif */
  246. .example {
  247. padding: 0 15px;
  248. }
  249. .example-info {
  250. /* #ifndef APP-NVUE */
  251. display: block;
  252. /* #endif */
  253. padding: 15px;
  254. color: #3b4144;
  255. background-color: #ffffff;
  256. font-size: 14px;
  257. line-height: 20px;
  258. }
  259. .example-info-text {
  260. font-size: 14px;
  261. line-height: 20px;
  262. color: #3b4144;
  263. }
  264. .example-body {
  265. flex-direction: column;
  266. padding: 15px;
  267. background-color: #ffffff;
  268. }
  269. .word-btn-white {
  270. font-size: 18px;
  271. color: #FFFFFF;
  272. }
  273. .word-btn {
  274. /* #ifndef APP-NVUE */
  275. display: flex;
  276. /* #endif */
  277. flex-direction: row;
  278. align-items: center;
  279. justify-content: center;
  280. border-radius: 6px;
  281. height: 48px;
  282. margin: 15px;
  283. background-color: #007AFF;
  284. }
  285. .word-btn--hover {
  286. background-color: #4ca2ff;
  287. }
  288. .image {
  289. width: 50rpx;
  290. height: 50rpx;
  291. }
  292. .text {
  293. font-size: 26rpx;
  294. margin-top: 10rpx;
  295. }
  296. .example-body {
  297. /* #ifndef APP-NVUE */
  298. display: block;
  299. /* #endif */
  300. }
  301. .grid-dynamic-box {
  302. margin-bottom: 15px;
  303. }
  304. .grid-item-box {
  305. flex: 1;
  306. /* #ifndef APP-NVUE */
  307. display: flex;
  308. /* #endif */
  309. flex-direction: column;
  310. align-items: center;
  311. justify-content: center;
  312. padding: 15px 0;
  313. }
  314. .grid-dot {
  315. position: absolute;
  316. top: 5px;
  317. right: 15px;
  318. }
  319. .swiper {
  320. height: 420px;
  321. }
  322. </style>