写一个微信小程序九宫格导航
时间: 2023-07-13 19:11:09 浏览: 278
微信小程序 九宫格实例代码
好的,下面是微信小程序九宫格导航的代码:
1. 在 `app.json` 文件中定义导航页面
```json
{
"pages": [
"pages/index/index",
"pages/navigation/navigation"
],
"window": {
"navigationBarTitleText": "九宫格导航"
},
"tabBar": {
"list": [
{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "images/home.png",
"selectedIconPath": "images/home-active.png"
},
{
"pagePath": "pages/navigation/navigation",
"text": "导航",
"iconPath": "images/navigation.png",
"selectedIconPath": "images/navigation-active.png"
}
]
}
}
```
2. 在 `index.wxml` 文件中添加九宫格导航
```html
<view class="grid-container">
<view wx:for="{{navigation}}" wx:key="{{item.id}}" class="grid-item" bindtap="navigateToPage">
<image src="{{item.icon}}" mode="widthFix"></image>
<text>{{item.text}}</text>
</view>
</view>
```
3. 在 `index.js` 文件中定义导航数据和跳转函数
```javascript
Page({
data: {
navigation: [
{
id: 1,
icon: "/images/navigation/news.png",
text: "新闻",
url: "/pages/news/news"
},
{
id: 2,
icon: "/images/navigation/video.png",
text: "视频",
url: "/pages/video/video"
},
{
id: 3,
icon: "/images/navigation/music.png",
text: "音乐",
url: "/pages/music/music"
},
{
id: 4,
icon: "/images/navigation/game.png",
text: "游戏",
url: "/pages/game/game"
},
{
id: 5,
icon: "/images/navigation/shopping.png",
text: "购物",
url: "/pages/shopping/shopping"
},
{
id: 6,
icon: "/images/navigation/travel.png",
text: "旅游",
url: "/pages/travel/travel"
},
{
id: 7,
icon: "/images/navigation/food.png",
text: "美食",
url: "/pages/food/food"
},
{
id: 8,
icon: "/images/navigation/sports.png",
text: "运动",
url: "/pages/sports/sports"
},
{
id: 9,
icon: "/images/navigation/more.png",
text: "更多",
url: "/pages/more/more"
}
]
},
navigateToPage: function (event) {
var url = event.currentTarget.dataset.url;
wx.navigateTo({
url: url
})
}
})
```
4. 在 `navigation.wxss` 文件中定义样式
```css
.grid-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
margin-top: 20rpx;
}
.grid-item {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 200rpx;
height: 200rpx;
margin: 20rpx;
border-radius: 10rpx;
background-color: #f8f8f8;
box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
}
.grid-item image {
width: 80rpx;
height: 80rpx;
margin-bottom: 10rpx;
}
.grid-item text {
font-size: 28rpx;
color: #333333;
text-align: center;
}
```
以上就是微信小程序九宫格导航的代码,希望能够帮到你!
阅读全文