微信小程序開發--網格布局


 

 

 實現上圖元素布局兩種實現方法:

1.微信小程序原生組件。

.wxml

 1 <view class="plate">
 2     <navigator url="/pages/program/program" hover-class="navigator-hover" class="plate-item">
 3       <view class="plate-img">
 4         <image src="{{a1src}}"></image>
 5          <text class="plate-info">
 6         編程開發
 7        </text>
 8       </view>
 9      
10     </navigator>
11     <navigator url="/pages/english/english" hover-class="navigator-hover" class="plate-item">
12       <view class="plate-img">
13         <image src="{{a2src}}"></image>
14         <text class="plate-info">
15         英語學習
16        </text>
17       </view>
18       
19     </navigator>
20     <navigator url="/pages/ideology/ideology" hover-class="navigator-hover" class="plate-item">
21       <view class="plate-img">
22         <image src="{{a3src}}"></image>
23         <text class="plate-info">
24         思政課堂
25        </text>
26       </view>
27       
28     </navigator>
29     <navigator url="/pages/interest/interest" class="plate-item">
30       <view class="plate-img">
31         <image src="{{a4src}}"></image>
32          <text class="plate-info">
33         生活興趣
34        </text>
35       </view>
36     </navigator>
37   </view>

.wxss

 1 .plate{
 2  display: flex;
 3 margin: 10rpx 0 10rpx 0;
 4 background-color: #f4f;
 5 }
 6 .plate-item{
 7   width: 25vw;
 8   display: flex;
 9   align-items: center;
10   flex-direction: column;
12 }
13 .plate-img{ 14 padding-top: 40rpx; 15 padding-bottom: 40rpx; 16 } 17 18 .plate-img image{ 19 width: 100rpx; 20 height: 100rpx; 21 } 22 .plate-info{ 23 font-size: 27rpx; 25 }

.js

1 a1src: '../../images/image/a1.png',
2 a2src: '../../images/image/a2.png',
3 a3src: '../../images/image/a3.png',
4 a4src: '../../images/image/a4.png',

效果圖:

 

 

 2.引入vant組件。vant組件的引入方式參見官方文檔:https://youzan.github.io/vant-weapp/#/quickstart

.wxml

1 <van-grid clickable="true" column-num="4" border="{{ false }}">
2     <van-grid-item use-slot wx:for="{{navItem}}" wx:for-item="a" wx:key="a"  link-type="navigateTo" url="{{a.imgto}}" >
3       <image style="width: 100rpx; height: 100rpx;" src="{{a.imgsrc}}"></image>
4     <text class="imginfo">{{a.imginfo}}</text>
5     </van-grid-item>
6 </van-grid>

.js,主要是數據綁定

 1 navItem: [
 2       {
 3         imgsrc: '../../images/image/a1.png',
 4         imginfo:'編程開發',
 5         imgto:'/pages/program/program' 
 6       },
 7       {
 8         imgsrc: '../../images/image/a2.png',
 9         imginfo: '英語學習',
10         imgto: '/pages/english/english'
11       },
12       {
13         imgsrc: '../../images/image/a3.png',
14         imginfo: '思政課堂',
15         imgto: '/pages/ideology/ideology'
16       },
17       {
18         imgsrc: '../../images/image/a4.png',
19         imginfo: '生活興趣',
20         imgto: '/pages/interest/interest'
21       }
22     ]

.json,引入vant組件

1 "usingComponents": {
2   "van-grid": "@vant/weapp/grid/index",
3   "van-grid-item": "@vant/weapp/grid-item/index"
4 }
View Code

效果圖

 


免責聲明!

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



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