欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 九宮格實(shí)例代碼

 更新時(shí)間:2017年01月21日 11:47:14   投稿:lqh  
這篇文章主要介紹了微信小程序 九宮格實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

微信小程序 九宮格

實(shí)現(xiàn)效果圖:

小程序是長(zhǎng)在微信上的,是移動(dòng)端的界面,為了能夠更方便的使用,我們常常希望使用九宮格界面的方式作為導(dǎo)航,那要如何實(shí)現(xiàn)呢?

基于一個(gè)簡(jiǎn)單的思考,九宮格就是三行三列,如果把行作為一個(gè)單位,再將每一行分成三列,那是不是就可以了?我們實(shí)踐一下。

首先來(lái)考慮九宮格數(shù)據(jù)的生成,每一個(gè)格子需要有一個(gè)圖標(biāo)、一個(gè)標(biāo)題、一個(gè)便于跳轉(zhuǎn)的路由,那天現(xiàn)在我們有九個(gè)頁(yè)面,所以定義一個(gè)一維數(shù)組即可。為了更好的進(jìn)行后續(xù)的配置,我們將這個(gè)數(shù)組獨(dú)立到一個(gè)文件中routes.js,然后將其在index.js頁(yè)面中引用,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頁(yè)面中我們引用routes.js,然后得到數(shù)據(jù)PageItems,但PageItems是一維數(shù)組,而我們前面思考是要用一行三列為一個(gè)組的,所以需要將這一維數(shù)組進(jìn)行重新組合,最直接的方法就是生成一個(gè)數(shù)組,每個(gè)數(shù)組的元素又包含了一個(gè)只有三個(gè)元素的一維數(shù)組,代碼如下

//index.js 
//獲取應(yīng)用實(shí)例 
var app = getApp() 
var routes = require('routes'); 
Page({ 
 data: { 
  userInfo: {}, 
  cellHeight: '120px', 
  pageItems: [] 
 }, 
 //事件處理函數(shù) 
 onLoad: function () { 
  var that = this 
  console.log(app); 
  //調(diào)用應(yīng)用實(shí)例的方法獲取全局?jǐn)?shù)據(jù) 
  app.getUserInfo(function (userInfo) { 
   wx.setNavigationBarTitle({ 
    title: '全新測(cè)試追蹤系統(tǒng)-' + 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中,我們來(lái)布局界面,由于每一個(gè)格子都是一樣的,只是數(shù)據(jù)不一樣,所以想到用模板來(lái)呈現(xiàn)。為此,我們先做一個(gè)單元格的模板面cell.wxml.

<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> 

這里看到兩個(gè)大括號(hào)內(nèi)套的是從外面?zhèn)魅氲臄?shù)據(jù),然后在里面可以進(jìn)行簡(jiǎn)單的邏輯判斷,以便于更好的呈現(xiàn)。比如text==null的時(shí)候,我們希望呈現(xiàn)的是一個(gè)空背景的格子,在有數(shù)據(jù)的時(shí)候我們希望呈現(xiàn)一個(gè)含背景的格子,所以"{{text==null||text.length==0?'pages-icon-wrapper-no-bg':'pages-icon-wrapper'}}".

另外一點(diǎn),由于我們是將該界面文件作為模板的,所以必須要用template標(biāo)記來(lái)包住,同時(shí)命一個(gè)名字name,這樣在引用模板的地方才可以識(shí)別調(diào)用。

現(xiàn)在我們?cè)趇ndex.wxml中引用這個(gè)模板

<!--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來(lái)引用,在調(diào)用的地方使用template和is,其中is指定的是cell.wxml中的name。item[0]、item[1]、item[2]是循環(huán)傳入的數(shù)據(jù),cellHeight是在index.js的data中存放的數(shù)據(jù)。在將數(shù)據(jù)傳入到模板內(nèi)部時(shí),框架會(huì)將其展開(kāi)在字段的形式,即key-value對(duì),所以再看cell.wxml文件,就會(huì)發(fā)現(xiàn)內(nèi)部是直接使用key來(lái)作為數(shù)據(jù)的。

將數(shù)據(jù)呈現(xiàn)到界面之后,我們需要相當(dāng)?shù)臉邮絹?lái)配合,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元素來(lái)呈現(xiàn)格子,所以每個(gè)格子自然就可以導(dǎo)航了。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論