首先讓我們來學習以下Flex布局
一,F(xiàn)lex布局簡介
布局的傳統(tǒng)解決方案,基于盒狀模型,依賴 display屬性 + position屬性 + float屬性
Flex布局簡介
-
Flex是Flexible Box的縮寫,意為”彈性布局”,用來為盒狀模型提供最大的靈活性。
-
任何一個容器都可以指定為Flex布局。
-
display: ‘flex’
容器默認存在兩根軸:水平的主軸(main axis)和垂直的交叉軸(cross axis)。主軸的開始位置(與邊框的交叉點)叫做main start,結束位置叫做main end;交叉軸的開始位置叫做cross start,結束位置叫做cross end
項目默認沿主軸排列。單個項目占據(jù)的主軸空間叫做main size,占據(jù)的交叉軸空間叫做cross size。
Flex的屬性
以下6個屬性設置在容器上
flex-direction 主軸的方向 默認為row
flex-wrap 如果一條軸線排不下,如何換行
flex-flow 是flex-direction屬性和flex-wrap屬性的簡寫形式
justify-content 定義了項目在主軸上的對齊方式
align-items 定義項目在交叉軸上如何對齊
align-content 屬性定義了多根軸線的對齊方式
注意:設為Flex布局以后,子元素的float、clear和vertical-align屬性將失效
flex-direction屬性
.box { flex-direction: row | row-reverse | column | column-reverse; }
它可能有4個值
- row(默認值):主軸為水平方向,起點在左端。
- row-reverse:主軸為水平方向,起點在右端。
- column:主軸為垂直方向,起點在上沿。
- column-reverse:主軸為垂直方向,起點在下沿
?flex-wrap屬性
默認情況下,項目都排在一條線(又稱”軸線”)上。flex-wrap屬性定義,如果一條軸線排不下,如何換行
.box{ flex-wrap: nowrap | wrap | wrap-reverse; }
(1)nowrap(默認):不換行。
(2)wrap:換行,第一行在上方。
(3)wrap-reverse:換行,第一行在下方。
?flex-flow
flex-flow屬性是flex-direction屬性和flex-wrap屬性的簡寫形式,默認值為row nowrap。
.box { flex-flow: <flex-direction> <flex-wrap>; }
justify-content屬性
justify-content屬性定義了項目在主軸上的對齊方式
.box { justify-content: flex-start | flex-end | center | space-between | space-around; }
?它可能取5個值,具體對齊方式與軸的方向有關。下面假設主軸為從左到右。
- flex-start(默認值):左對齊
- flex-end:右對齊
- center: 居中
- space-between:兩端對齊,項目之間的間隔都相等。
- space-around:每個項目兩側的間隔相等。所以,項目之間的間隔比項目與邊框的間隔大一倍
?align-items屬性
align-items屬性定義項目在交叉軸上如何對齊。
.box { align-items: flex-start | flex-end | center | baseline | stretch; }
?它可能取5個值。具體的對齊方式與交叉軸的方向有關,下面假設交叉軸從上到下。
- flex-start:交叉軸的起點對齊。
- flex-end:交叉軸的終點對齊。
- center:交叉軸的中點對齊。
- baseline: 項目的第一行文字的基線對齊。
- stretch(默認值):如果項目未設置高度或設為auto,將占滿整個容器的高度
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:每根軸線兩側的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。
- stretch(默認值):軸線占滿整個交叉軸
二、首頁布局搭建
1.底部頁面的搭建
在官網(wǎng)找到taaBar首先搭建底部菜單,將里面的數(shù)據(jù)根據(jù)自身修改即可
app.js配置
// app.json部分代碼展示
"pages":[
"pages/index/index",
"pages/meeting/list/list",
"pages/vote/list/list",
"pages/ucenter/index/index",
"pages/logs/logs"
],
"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": "設置"
}]
}
底部導航欄沒有圖標是顯得非常單調(diào)的,所以我們還要創(chuàng)建一個文件夾名為static用來存放圖片
繼續(xù)在static文件夾下創(chuàng)建一個文件名為tabBar,將我們所需的圖標放入,搭配屬性iconPath引用圖標即可
效果:
2.首頁輪播圖搭建?
2.1 輪播圖搭建??
為了后期方便維護我們先將所有的后端接口通過一個文件來保存,在根目錄下新建config文件夾隨后建立api.js文件
// 以下是業(yè)務服務器API地址
// 本機開發(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模擬數(shù)據(jù)
圖json數(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"
}
}
?因為我們現(xiàn)在的小程序開發(fā)默認檢查安全證書(域名為https)所以我們的請求過不去,我們只需點擊微信小程序開發(fā)中的詳情按鈕,再繼續(xù)點擊本地設置將不校驗合法域名選項打開
我們編譯之后查看編譯器里打印的數(shù)據(jù)
我們在主頁編寫方法測試
// index.js
// 獲取應用實例
const app = getApp()
const api = require("../../config/api")
Page({
data: {
imgSrcs:[]
},
// 事件處理函數(shù)
bindViewTap() {
wx.navigateTo({
url: '../logs/logs'
})
},
// 輪播圖的方法
loadSwiperImgs(){
let that=this;
wx.request({
url: api.SwiperImgs,
dataType: 'json',
success(res) {
console.log(res)
that.setData({
imgSrcs:res.data.images
})
}
})
},
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
// 一進來就調(diào)用輪播圖的方法
this.loadSwiperImgs();
},
getUserProfile(e) {
// 推薦使用wx.getUserProfile獲取用戶信息,開發(fā)者每次通過該接口獲取用戶個人信息均需用戶確認,開發(fā)者妥善保管用戶快速填寫的頭像昵稱,避免重復彈窗
wx.getUserProfile({
desc: '展示用戶信息', // 聲明獲取用戶個人信息后的用途,后續(xù)會展示在彈窗中,請謹慎填寫
success: (res) => {
console.log(res)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
},
getUserInfo(e) {
// 不推薦使用getUserInfo獲取用戶信息,預計自2021年4月13日起,getUserInfo將不再彈出彈窗,并直接返回匿名的用戶個人信息
console.log(e)
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
}
})
上面代碼中關鍵性代碼
const api = require("../../config/api")
Page({
data: {
imgSrcs:[]
},
// 輪播圖的方法
loadSwiperImgs(){
let that=this;
wx.request({
url: api.SwiperImgs,
dataType: 'json',
success(res) {
console.log(res)
that.setData({
imgSrcs:res.data.images
})
}
})
},
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
// 一進來就調(diào)用輪播圖的方法
this.loadSwiperImgs();
}
})
2.3建立輪播圖?
液位我們在前面已經(jīng)建立了數(shù)據(jù),所以我們在這里進行一個輪播圖的的頁面編寫
<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>
3、首頁內(nèi)容搭建
?我們照貓畫虎,和我們輪播圖的流程一樣,定義接口發(fā)送ajax再利用Mock模擬數(shù)據(jù)根據(jù)官網(wǎng)提供的組件進行完善。
3.1.會議信息ajax
//首頁會議信息的ajax
loadMeetingInfos(){
let that=this;
wx.request({
url: api.MettingInfos,
dataType: 'json',
success(res) {
console.log(res)
that.setData({
lists:res.data.lists
})
}
})
}
注意:在data中定義屬性lists和onLoad函數(shù)中調(diào)用該方法
3.2.Mock模擬數(shù)據(jù)
上圖中JSON數(shù)據(jù)包:
{
"data": {
"lists": [
{
"id": "1",
"image": "/static/persons/1.jpg",
"title": "對話產(chǎn)品總監(jiān) | 深圳·北京PM大會 【深度對話小米/京東/等產(chǎn)品總監(jiān)】",
"num":"304",
"state":"進行中",
"starttime": "2022-03-13 00:00:00",
"location": "深圳市·南山區(qū)"
},
{
"id": "1",
"image": "/static/persons/2.jpg",
"title": "AI WORLD 2016世界人工智能大會",
"num":"380",
"state":"已結束",
"starttime": "2022-03-15 00:00:00",
"location": "北京市·朝陽區(qū)"
},
{
"id": "1",
"image": "/static/persons/3.jpg",
"title": "H100太空商業(yè)大會",
"num":"500",
"state":"進行中",
"starttime": "2022-03-13 00:00:00",
"location": "大連市"
},
{
"id": "1",
"image": "/static/persons/4.jpg",
"title": "報名年度盛事,大咖云集!2016鳳凰國際論壇邀您“與世界對話”",
"num":"150",
"state":"已結束",
"starttime": "2022-03-13 00:00:00",
"location": "北京市·朝陽區(qū)"
},
{
"id": "1",
"image": "/static/persons/5.jpg",
"title": "新質生活 · 品質時代 2016消費升級創(chuàng)新大會",
"num":"217",
"state":"進行中",
"starttime": "2022-03-13 00:00:00",
"location": "北京市·朝陽區(qū)"
}
]
},
"statusCode": "200",
"header": {
"content-type":"applicaiton/json;charset=utf-8"
}
}
?3.3.搭建會議信息
布局:
index.wxml?:
<view class="mobi-title">
<text class="mobi-icon"></text>
<text class="mobi-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">
<text>到底啦</text>
</view>
css樣式:
index.wxss:
/**index.wxss**/
.section{
color: #aaa;
display: flex;
justify-content: center;
}
.list-info {
color: #aaa;
}
.list-num {
color: #e40909;
font-weight: 700;
}
.join {
padding: 0px 0px 0px 10px;
color: #aaa;
}
.state {
margin: 0px 6px 0px 6px;
border: 1px solid #93b9ff;
color: #93b9ff;
}
.list-tag {
padding: 3px 0px 10px 0px;
display: flex;
align-items: center;
}
.list-title {
display: flex;
justify-content: space-between;
font-size: 11pt;
color: #333;
font-weight: bold;
}
.list-detail {
display: flex;
flex-direction: column;
margin: 0px 0px 0px 15px;
}
.video-img {
width: 80px;
height: 80px;
}
.list {
display: flex;
flex-direction: row;
border-bottom: 1px solid #6b6e74;
padding: 10px;
}
.mobi-text {
font-weight: 700;
padding: 15px;
}
.mobi-icon {
border-left: 5px solid #e40909;
}
.mobi-title {
background-color: rgba(158, 158, 142, 0.678);
margin: 10px 0px 10px 0px;
}
.swiper-item {
height: 300rpx;
width: 100%;
border-radius: 10rpx;
}
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
color: #aaa;
}
.userinfo-avatar {
overflow: hidden;
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}
.usermotto {
margin-top: 200px;
}
效果演示:文章來源:http://www.zghlxwxcb.cn/news/detail-713012.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-713012.html
到了這里,關于小程序搭建OA項目首頁布局界面的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!