小程序是長在微信上的,是移動端的界面,爲了可以更方便的使用,咱們經常但願使用九宮格界面的方式做爲導航,那要如何實現呢? 小程序
基於一個簡單的思考,九宮格就是三行三列,若是把行做爲一個單位,再將每一行分紅三列,那是否是就能夠了?咱們實踐一下。 微信小程序
首先來考慮九宮格數據的生成,每個格子須要有一個圖標、一個標題、一個便於跳轉的路由,那天如今咱們有九個頁面,因此定義一個一維數組便可。爲了更好的進行後續的配置,咱們將這個數組獨立到一個文件中routes.js,而後將其在index.js頁面中引用,routes放到index的目錄下。數組
var PageItems = [ { text: '格子1', icon: '../../images/c1.png', route: '../c1/c1', }, { text: '格子2', icon: '../../images/c2.png', route: '../c2/c2', }, { text: '格子3', icon: '../../images/c3.png', route: '../c3/c3', }, { text: '格子4', icon: '../../images/c4.png', route: '../c4/c4', }, { text: '格子5', icon: '../../images/c5', route: '../c5/c5', }, { text: '格子6', icon: '../../images/c6.png', route: '../c6/c6', }, { text: '格子7', icon: '../../images/c7.png', route: '../c7/c7', }, { text: '格子8', icon: '../../images/c8', route: '../c8/c8', }, { text: '格子9', icon: '../../images/c9.png', route: '../c9/c9', } ]; module.exports = { PageItems: PageItems }
在index.js頁面中咱們引用routes.js,而後獲得數據PageItems,但PageItems是一維數組,而咱們前面思考是要用一行三列爲一個組的,因此須要將這一維數組進行從新組合,最直接的方法就是生成一個數組,每一個數組的元素又包含了一個只有三個元素的一維數組,代碼以下 微信
//index.js //獲取應用實例 var app = getApp() var routes = require('routes'); Page({ data: { userInfo: {}, cellHeight: '120px', pageItems: [] }, //事件處理函數 onLoad: function () { var that = this console.log(app); //調用應用實例的方法獲取全局數據 app.getUserInfo(function (userInfo) { wx.setNavigationBarTitle({ title: '全新測試追蹤系統-' + userInfo.nickName, success: function (res) { // success } }) that.setData({ userInfo: userInfo }) var pageItems = []; var row = []; var len = routes.PageItems.length;//重組PageItems len = Math.floor((len + 2) / 3) * 3; for (var i = 0; i < len; i++) { if ((i + 1) % 3 == 0) { row.push(indexs.PageItems[i]); pageItems.push(row); row = []; continue; } else { row.push(indexs.PageItems[i]); } } wx.getSystemInfo({ success: function (res) { var windowWidth = res.windowWidth; that.setData({ cellHeight: (windowWidth / 3) + 'px' }) }, complete: function () { that.setData({ pageItems: pageItems }) } }) }) } })
在index.wxml中,咱們來佈局界面,因爲每個格子都是同樣的,只是數據不同,因此想到用模板來呈現。爲此,咱們先作一個單元格的模板面cell.wxml.app
<template name="cell"> <navigator url="{{route}}" class="pages-item" style="height:{{cellHeight}}"> <view class="{{text==null||text.length==0?'pages-icon-wrapper-no-bg':'pages-icon-wrapper'}}" > <image src="{{icon}}" class="pages-icon"></image> </view> <view class="pages-text-wrapper"> <text class="pages-text">{{text}}</text> </view> </navigator> </template>
這裏看到兩個大括號內套的是從外面傳入的數據,而後在裏面能夠進行簡單的邏輯判斷,以便於更好的呈現。好比text==null的時候,咱們但願呈現的是一個空背景的格子,在有數據的時候咱們但願呈現一個含背景的格子,因此框架
"{{text==null||text.length==0?'pages-icon-wrapper-no-bg':'pages-icon-wrapper'}}".
另一點,因爲咱們是將該界面文件做爲模板的,因此必需要用template標記來包住,同時命一個名字name,這樣在引用模板的地方纔能夠識別調用。
如今咱們在index.wxml中引用這個模板xss
<!--index.wxml--> <import src="cell.wxml" /> <view class="pages-container"> <scroll-view scroll-y="true" class="pages-wrapper"> <view wx:for="{{pageItems}}" wx:key="{{text}}"> <view class="pages-row"> <template is="cell" data="{{...item[0],cellHeight}}" /> <template is="cell" data="{{...item[1],cellHeight}}" /> <template is="cell" data="{{...item[2],cellHeight}}" /> </view> </view> </scroll-view> </view>
模板的引用使用import來引用,在調用的地方使用template和is,其中is指定的是cell.wxml中的name。item[0]、item[1]、item[2]是循環傳入的數據,cellHeight是在index.js的data中存放的數據。在將數據傳入到模板內部時,框架會將其展開在字段的形式,即key-value對,因此再看cell.wxml文件,就會發現內部是直接使用key來做爲數據的。
將數據呈現到界面以後,咱們須要至關的樣式來配合,index.wxss代碼以下。函數
/**index.wxss**/ .pages-container { height: 100%; display: flex; flex-direction: column; box-sizing: border-box; padding-top: 10rpx; padding-bottom: 10rpx; } .pages-title-bg { width: 100%; } .pages-wrapper { } .pages-row { width: 100%; display: flex; flex-direction: row; justify-content: space-around; } .pages-item { position: relative; padding: 10rpx; width: 33%; background-color: #fff; border: #ddd solid 1px; } .pages-icon-wrapper { display: flex; justify-content: space-around; align-items: center; margin: 10rpx; border-radius: 30%; height: 75%; background:#00CD0D; } .pages-icon-wrapper-no-bg { display: flex; justify-content: space-around; align-items: center; margin: 10rpx; height: 75%; } .pages-icon { width: 100rpx; height: 100rpx; } .pages-text-wrapper { text-align: center; } .pages-text { font-weight: bolder; }
效果以下圖佈局
咱們模板中使用navigator元素來呈現格子,因此每一個格子天然就能夠導航了。