目錄
前言
一、Flex 布局(?分類?編程技術(shù))
1、Flex布局是什么?
2、基本概念
3、容器的屬性
3.1 flex-direction屬性
3.2 flex-wrap屬性
3.3 flex-flow
3.4 justify-content屬性
3.5 align-items屬性
3.6 align-content屬性
4、項目的屬性
4.1 order屬性
4.2 flex-grow屬性
4.3 flex-shrink屬性
4.4 flex-basis屬性
4.5 flex屬性
4.6 align-self屬性
二、首頁布局搭建
1.底部導(dǎo)航欄設(shè)置
2.首頁輪播圖搭建
2.1.后端接口管理
2.2.Mock.js模擬數(shù)據(jù)
2.3.輪播圖的搭建
3.首頁會議信息搭建
3.1.模擬mock.js傳輸數(shù)據(jù)
3.2.搭建首頁會議信息
前言
當(dāng)搭建微信小程序項目時,使用Flex布局和Mock數(shù)據(jù)交互是常見的需求。接下來本篇博客將帶大家了解Flex布局,Mock數(shù)據(jù)交互和OA會議系統(tǒng)首頁搭建的實現(xiàn)
一、Flex 布局(?分類?編程技術(shù))
網(wǎng)頁布局(layout)是CSS的一個重點應(yīng)用。
布局的傳統(tǒng)解決方案,基于盒狀模型,依賴?display屬性 +?position屬性 +?float屬性。它對于那些特殊布局非常不方便,比如,垂直居中就不容易實現(xiàn)。
2009年,W3C提出了一種新的方案—-Flex布局,可以簡便、完整、響應(yīng)式地實現(xiàn)各種頁面布局。目前,它已經(jīng)得到了所有瀏覽器的支持,這意味著,現(xiàn)在就能很安全地使用這項功能。
Flex布局將成為未來布局的首選方案。本文介紹Flex布局的語法。
以下內(nèi)容主要參考了下面兩篇文章:A Complete Guide to Flexbox?和?A Visual Guide to CSS3 Flexbox Properties。
1、Flex布局是什么?
Flex是Flexible Box的縮寫,意為”彈性布局”,用來為盒狀模型提供最大的靈活性。
任何一個容器都可以指定為Flex布局。
.box{
display: flex;
}
行內(nèi)元素也可以使用Flex布局。
.box{
display: inline-flex;
}
Webkit內(nèi)核的瀏覽器,必須加上-webkit前綴。
.box{
display: -webkit-flex; /* Safari */
display: flex;
}
注意,設(shè)為Flex布局以后,子元素的float、clear和vertical-align屬性將失效。
2、基本概念
采用Flex布局的元素,稱為Flex容器(flex container),簡稱”容器”。它的所有子元素自動成為容器成員,稱為Flex項目(flex item),簡稱”項目”。
容器默認(rèn)存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置(與邊框的交叉點)叫做main start,結(jié)束位置叫做main end;交叉軸的開始位置叫做cross start,結(jié)束位置叫做cross end。
項目默認(rèn)沿主軸排列。單個項目占據(jù)的主軸空間叫做main size,占據(jù)的交叉軸空間叫做cross size。
3、容器的屬性
以下6個屬性設(shè)置在容器上。
- flex-direction
- flex-wrap
- flex-flow
- justify-content
- align-items
- align-content
3.1 flex-direction屬性
flex-direction屬性決定主軸的方向(即項目的排列方向)。
.box {
flex-direction: row | row-reverse | column | column-reverse;
}
它可能有4個值。
- row(默認(rèn)值):主軸為水平方向,起點在左端。
- row-reverse:主軸為水平方向,起點在右端。
- column:主軸為垂直方向,起點在上沿。
- column-reverse:主軸為垂直方向,起點在下沿。
3.2 flex-wrap屬性
默認(rèn)情況下,項目都排在一條線(又稱”軸線”)上。flex-wrap屬性定義,如果一條軸線排不下,如何換行。
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}
它可能取三個值。
(1)nowrap(默認(rèn)):不換行。
(2)wrap:換行,第一行在上方。
(3)wrap-reverse:換行,第一行在下方。
3.3 flex-flow
flex-flow屬性是flex-direction屬性和flex-wrap屬性的簡寫形式,默認(rèn)值為row nowrap。
.box {
flex-flow: <flex-direction> <flex-wrap>;
}
3.4 justify-content屬性
justify-content屬性定義了項目在主軸上的對齊方式。
.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
}
它可能取5個值,具體對齊方式與軸的方向有關(guān)。下面假設(shè)主軸為從左到右。
- flex-start(默認(rèn)值):左對齊
- flex-end:右對齊
- center: 居中
- space-between:兩端對齊,項目之間的間隔都相等。
- space-around:每個項目兩側(cè)的間隔相等。所以,項目之間的間隔比項目與邊框的間隔大一倍。
3.5 align-items屬性
align-items屬性定義項目在交叉軸上如何對齊。
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}
它可能取5個值。具體的對齊方式與交叉軸的方向有關(guān),下面假設(shè)交叉軸從上到下。
- flex-start:交叉軸的起點對齊。
- flex-end:交叉軸的終點對齊。
- center:交叉軸的中點對齊。
- baseline: 項目的第一行文字的基線對齊。
- stretch(默認(rèn)值):如果項目未設(shè)置高度或設(shè)為auto,將占滿整個容器的高度。
3.6 align-content屬性
align-content屬性定義了多根軸線的對齊方式。如果項目只有一根軸線,該屬性不起作用。
.box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
該屬性可能取6個值。
- flex-start:與交叉軸的起點對齊。
- flex-end:與交叉軸的終點對齊。
- center:與交叉軸的中點對齊。
- space-between:與交叉軸兩端對齊,軸線之間的間隔平均分布。
- space-around:每根軸線兩側(cè)的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。
- stretch(默認(rèn)值):軸線占滿整個交叉軸。
4、項目的屬性
以下6個屬性設(shè)置在項目上。
- order
- flex-grow
- flex-shrink
- flex-basis
- flex
- align-self
4.1 order屬性
order屬性定義項目的排列順序。數(shù)值越小,排列越靠前,默認(rèn)為0。
.item {
order: <integer>;
}
4.2 flex-grow屬性
flex-grow屬性定義項目的放大比例,默認(rèn)為0,即如果存在剩余空間,也不放大。
.item {
flex-grow: <number>; /* default 0 */
}
如果所有項目的flex-grow屬性都為1,則它們將等分剩余空間(如果有的話)。如果一個項目的flex-grow屬性為2,其他項目都為1,則前者占據(jù)的剩余空間將比其他項多一倍。
4.3 flex-shrink屬性
flex-shrink屬性定義了項目的縮小比例,默認(rèn)為1,即如果空間不足,該項目將縮小。
.item {
flex-shrink: <number>; /* default 1 */
}
如果所有項目的flex-shrink屬性都為1,當(dāng)空間不足時,都將等比例縮小。如果一個項目的flex-shrink屬性為0,其他項目都為1,則空間不足時,前者不縮小。
負(fù)值對該屬性無效。
4.4 flex-basis屬性
flex-basis屬性定義了在分配多余空間之前,項目占據(jù)的主軸空間(main size)。瀏覽器根據(jù)這個屬性,計算主軸是否有多余空間。它的默認(rèn)值為auto,即項目的本來大小。
.item {
flex-basis: <length> | auto; /* default auto */
}
它可以設(shè)為跟width或height屬性一樣的值(比如350px),則項目將占據(jù)固定空間。
4.5 flex屬性
flex屬性是flex-grow, flex-shrink 和 flex-basis的簡寫,默認(rèn)值為0 1 auto。后兩個屬性可選。
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}
該屬性有兩個快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建議優(yōu)先使用這個屬性,而不是單獨寫三個分離的屬性,因為瀏覽器會推算相關(guān)值。
4.6 align-self屬性
align-self屬性允許單個項目有與其他項目不一樣的對齊方式,可覆蓋align-items屬性。默認(rèn)值為auto,表示繼承父元素的align-items屬性,如果沒有父元素,則等同于stretch。
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
該屬性可能取6個值,除了auto,其他都與align-items屬性完全一致。
二、首頁布局搭建
1.底部導(dǎo)航欄設(shè)置
一個微信小程序必不可少的就是底部導(dǎo)航欄,所以我們先搭建一個底部導(dǎo)航欄 ,根據(jù)微信小程序的開發(fā)者文檔可知,底部導(dǎo)航欄需要在api.json定義tabBar并做好相應(yīng)的配置(注意先在pages做好頁面的定義)。
底部導(dǎo)航欄沒有圖標(biāo)是顯得非常單調(diào)的,所以我們還要創(chuàng)建一個文件夾名為static用來存放圖片
繼續(xù)在static文件夾下創(chuàng)建一個文件名為tabBar,將我們所需的圖標(biāo)放入,搭配屬性iconPath引用圖標(biāo)即可。
{
"pages": [
"pages/index/index",
"pages/meeting/list/list",
"pages/vote/list/list",
"pages/ucenter/index/index"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "OA會議系統(tǒng)",
"navigationBarTextStyle": "black"
},
"tabBar": {
"list": [
{
"pagePath": "pages/index/index",
"text": "首頁",
"iconPath": "/static/tabBar/coding.png",
"selectedIconPath": "/static/tabBar/coding-active.png"
},
{
"pagePath": "pages/meeting/list/list",
"iconPath": "/static/tabBar/sdk.png",
"selectedIconPath": "/static/tabBar/sdk-active.png",
"text": "會議"
},
{
"pagePath": "pages/vote/list/list",
"iconPath": "/static/tabBar/template.png",
"selectedIconPath": "/static/tabBar/template-active.png",
"text": "投票"
},
{
"pagePath": "pages/ucenter/index/index",
"iconPath": "/static/tabBar/component.png",
"selectedIconPath": "/static/tabBar/component-active.png",
"text": "設(shè)置"
}
]
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
效果演示
2.首頁輪播圖搭建
2.1.后端接口管理
在頁面加載時,我們需要向后端請求最新的會議和通知數(shù)據(jù),并渲染到輪播圖上。由于我們暫時還沒有后端接口,為了方便調(diào)試,我們選擇使用Mock.js模擬數(shù)據(jù)。
在onLoad
生命周期函數(shù)中使用Axios發(fā)起請求:
onLoad: function () {
// 獲取輪播圖數(shù)據(jù)
axios.get('http://localhost:8080/demo/wx/swiperImgs').then(res => {
this.setData({
hotNews: res.data
})
})
}
為了后期方便維護(hù)我們先將所有的后端接口通過一個文件來保存,在根目錄下新建config文件夾隨后建立api.js文件。
// 以下是業(yè)務(wù)服務(wù)器API地址
// 本機(jī)開發(fā)API地址
var WxApiRoot = 'http://localhost:8080/demo/wx/';
// 測試環(huán)境部署api地址
// var WxApiRoot = 'http://192.168.0.101:8070/demo/wx/';
// 線上平臺api地址
//var WxApiRoot = 'https://www.oa-mini.com/demo/wx/';
module.exports = {
IndexUrl: WxApiRoot + 'home/index', //首頁數(shù)據(jù)接口
SwiperImgs: WxApiRoot+'swiperImgs', //輪播圖
MettingInfos: WxApiRoot+'meeting/list', //會議信息
};
2.2.Mock.js模擬數(shù)據(jù)
接下來我們將通過Mock.js模擬數(shù)據(jù)傳輸
打開mock
傳入模擬的數(shù)據(jù)?
模擬的輪播圖數(shù)據(jù)
{
"data": {
"images":[
{
"img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner1.png",
"text": "1"
},
{
"img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner2.png",
"text": "2"
},
{
"img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner3.png",
"text": "3"
},
{
"img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner4.png",
"text": "4"
},
{
"img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png",
"text": "5"
},
{
"img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner6.png",
"text": "6"
}
]
},
"statusCode": "200",
"header": {
"content-type":"applicaiton/json;charset=utf-8"
}
}
將下例方法放到onLoad
生命周期函數(shù)中并執(zhí)行以下方法
loadSwiperImgs(){
let that=this;
// http://localhost:8080/demo/wx/swiperImgs
wx.request({
url: api.SwiperImgs,
dataType: 'json',
success(res) {
console.log(res)
that.setData({
imgSrcs:res.data.images
})
}
})
}
我們將可以得到以下數(shù)據(jù)
注意:
有一部分人可能會遇到以下問題
因為我們現(xiàn)在的小程序開發(fā)默認(rèn)檢查安全證書(域名為https)所以我們的請求過不去,我們只需點擊微信小程序開發(fā)中的詳情按鈕,再繼續(xù)點擊本地設(shè)置將不校驗合法域名選項打開。
2.3.輪播圖的搭建
根據(jù)我們官網(wǎng)提供的組件swiper進(jìn)行搭建輪播圖。
編寫組件和樣式?
<!--index.wxml-->
<view>
<d autoplay="true" indicator-dots="true">
<block wx:for="{{imgSrcs}}" wx:key="text">
<swiper-item>
<view>
<image src="{{item.img}}" class="swiper-item" />
</view>
</swiper-item>
</block>
</swiper>
</view>
/**index.wxss**/
.swiper-item {
height: 300rpx;
width: 100%;
border-radius: 10rpx;
}v
效果演示
3.首頁會議信息搭建
3.1.模擬mock.js傳輸數(shù)據(jù)
發(fā)送后臺請求并將方法放到onLoad
生命周期函數(shù)中
//首頁會議信息的ajax
loadMeetingInfos(){
let that=this;
wx.request({
url: api.MettingInfos,
dataType: 'json',
success(res) {
console.log(res)
that.setData({
lists:res.data.lists
})
}
})
}
模擬的會議信息數(shù)據(jù)
{
"data": {
"lists": [
{
"id": "1",
"image": "/static/persons/1.jpg",
"title": "對話產(chǎn)品總監(jiān) | 深圳·北京PM大會 【深度對話小米/京東/等產(chǎn)品總監(jiān)】",
"num":"304",
"state":"進(jìn)行中",
"starttime": "2022-03-13 00:00:00",
"location": "深圳市·南山區(qū)"
},
{
"id": "1",
"image": "/static/persons/2.jpg",
"title": "AI WORLD 2016世界人工智能大會",
"num":"380",
"state":"已結(jié)束",
"starttime": "2022-03-15 00:00:00",
"location": "北京市·朝陽區(qū)"
},
{
"id": "1",
"image": "/static/persons/3.jpg",
"title": "H100太空商業(yè)大會",
"num":"500",
"state":"進(jìn)行中",
"starttime": "2022-03-13 00:00:00",
"location": "大連市"
},
{
"id": "1",
"image": "/static/persons/4.jpg",
"title": "報名年度盛事,大咖云集!2016鳳凰國際論壇邀您“與世界對話”",
"num":"150",
"state":"已結(jié)束",
"starttime": "2022-03-13 00:00:00",
"location": "北京市·朝陽區(qū)"
},
{
"id": "1",
"image": "/static/persons/5.jpg",
"title": "新質(zhì)生活 · 品質(zhì)時代 2016消費升級創(chuàng)新大會",
"num":"217",
"state":"進(jìn)行中",
"starttime": "2022-03-13 00:00:00",
"location": "北京市·朝陽區(qū)"
}
]
},
"statusCode": "200",
"header": {
"content-type":"applicaiton/json;charset=utf-8"
}
}
3.2.搭建首頁會議信息
根據(jù)我們所設(shè)想的去搭建我們的模樣,以下是一個樣式模板圖。
index.wxml
<!--pages/index/index.wxml-->
<!-- <text>pages/index/index.wxml</text> -->
<view>
<swiper autoplay="true" indicator-dots="true" indicator-color="#fff" indicator-active-color="#00f">
<block wx:for="{{imgSrcs}}" wx:key="text">
<swiper-item>
<view>
<image src="{{item.img}}" class="swiper-item" />
</view>
</swiper-item>
</block>
</swiper>
</view>
<view class="mobi-title">
<text class="mobi-icon"></text>
<text>會議信息</text>
</view>
<block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id">
<view class="list" data-id="{{item.id}}">
<view class="list-img">
<image class="video-img" mode="scaleToFill" src="{{item.image}}"></image>
</view>
<view class="list-detail">
<view class="list-title"><text>{{item.title}}</text></view>
<view class="list-tag">
<view class="state">{{item.state}}</view>
<view class="join"><text class="list-num">{{item.num}}</text>人報名</view>
</view>
<view class="list-info"><text>{{item.location}}</text>|<text>{{item.starttime}}</text></view>
</view>
</view>
</block>
<view class="section bottom-line">
<text>到底啦</text>
</view>
效果演示?
我們沒有給它調(diào)節(jié)樣式,是不是看起來不那么美觀,接下來將加入我們的樣式
index.wxss文章來源:http://www.zghlxwxcb.cn/news/detail-715080.html
/* pages/index/index.wxss */
page{
height: 100%;
background-color: #efeff4;
}
.swiper-item {
height: 300rpx;
width: 100%;
border-radius: 10rpx;
}
.mobi-title{
font-size: 18px;
margin: 10rpx;
}
.mobi-icon{
background-color: red;
padding: 3rpx;
}
.mobi-title text{
margin-left: 10rpx;
}
.list{
background-color: #fff;
display: flex;
margin: 10rpx;
padding: 10rpx;
}
.list-img,.video-img{
height: 150rpx;
width: 150rpx;
}
.list-img{
margin: 20rpx 0 0 0;
}
.list-detail{
margin: 0 0 0 15rpx;
}
.list-title{
font-weight: 700;
}
.list-tag{
display: flex;
margin: 10px 0;
}
.state{
border: 2px solid lightskyblue;
padding: 2px;
color: lightskyblue;
}
.join{
border: 2px solid #fff;
padding: 2px;
margin: 0 0 0 20rpx;
color: gray;
}
.list-num{
color: red;
}
.list-info{
color: gray;
}
.bottom-line{
text-align: center;
margin-bottom: 10px;
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-715080.html
到了這里,關(guān)于微信小程序之會議OA系統(tǒng)首頁布局搭建與Mock數(shù)據(jù)交互的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!