1. フレックスレイアウト
[ボックス モデル]に基づくレイアウトの従来のソリューションは、表示属性 + 位置属性 + フロート属性に依存します。
1. フレックスレイアウトとは何ですか?
- Flex はFlexible Boxの略語で、「柔軟なレイアウト」を意味し、ボックス型モデルに最大限の柔軟性を提供するために使用されます。
- 任意のコンテナを Flex レイアウトとして指定できます。
- 表示: 'フレックス'
コンテナにはデフォルトで 2 つの軸があります:水平主軸と垂直交差軸です。
- 主軸の開始位置(境界線との交点)を主開始位置、終了位置を主終了といいます。
- 交差軸の開始位置をクロススタート、終了位置をクロスエンドといいます。
デフォルトでは、項目は主軸に沿って配置されます。1 つのアイテムが占める主軸のスペースをメイン サイズと呼び、1 つのアイテムが占める横軸のスペースをクロス サイズと呼びます。
2. フレックス属性
属性 | 効果 |
---|---|
フレックス方向 | 主軸の方向はデフォルトで行になります。 |
フレックスラップ | 1本の軸線を配置できない場合、線を折り返すにはどうすればよいですか? |
フレックスフロー | flex-direction プロパティと flex-wrap プロパティの略称です。 |
コンテンツの正当化 | 主軸上の項目の配置を定義します |
アイテムの整列 | 項目を交差軸上でどのように配置するかを定義します |
コンテンツの整列 | プロパティは複数の軸の位置合わせを定義します |
[注意] Flexレイアウトに設定すると、子要素のfloat、clear、vertical-align属性は無効になります。
flex-direction
(コンテナの主軸に沿った) 主軸上の子要素の配置を定義します。これには次の値が含まれます。
row
(デフォルト): 子要素は主軸の左から右に配置されます。row-reverse
: 子要素は主軸の右から左に配置されます。column
: 子要素は主軸の上から下に配置されます。column-reverse
: 子要素は主軸上で下から上に配置されます。
flex-wrap
コンテナの幅が不十分な場合に子要素を折り返す方法を定義します。これには次の値が含まれます。
nowrap
(デフォルト値): 子要素は折り返されず、コンテナの幅を超える部分は圧縮されます。wrap
: 子要素は行ごとに折り返され、コンテナの幅を超える子要素は次の行に移動されます。wrap-reverse
: 子要素は逆行で折り返され、コンテナの幅を超える子要素は次の行から配置されます。
flex-flow
flex-direction
と 2 つの属性flex-wrap
の略称 です 。これには、スペースで区切られた 2 つの値が含まれます。
flex-direction
値 (デフォルトはrow
)。flex-wrap
値 (デフォルトはnowrap
)。
justify-content
主軸上の子要素の配置を定義します。これには次の値が含まれます。
flex-start
(デフォルト): 子要素はコンテナの開始端に対して配置されます。flex-end
: 子要素はコンテナの終わり近くに配置されます。center
: 子要素はコンテナの主軸の中心に配置されます。space-between
: 子要素はコンテナ上に均等に配置され、最初の子要素が開始エッジに、最後の子要素が最後のエッジに配置されます。space-around
: 子要素はコンテナ上に均等に配置され、子要素間には空白が入ります。
align-items
交差軸 (主軸に垂直な軸) 上の子要素の配置を定義します。これには次の値が含まれます。
stretch
(デフォルト): 子要素は交差軸を満たすまで引き伸ばされます。flex-start
: 子要素は交差軸の開始エッジに位置合わせされます。flex-end
: 子要素は交差軸の最後の端に配置されます。center
: 子要素はコンテナの交差軸の中心に配置されます。baseline
: 子要素はベースラインに基づいて配置されます。
align-content
交差軸上の子要素の複数行の配置を定義します。これは、次の値を含む子要素の行が複数ある場合にのみ有効になります。
stretch
(デフォルト): 複数行の子要素が交差軸を満たすように引き伸ばされます。flex-start
: 複数行のサブ要素が交差軸の開始エッジに整列します。flex-end
: 複数行のサブ要素が交差軸の終端に整列します。center
: 子要素の複数の行がコンテナの交差軸の中心に配置されます。space-between
: サブ要素の複数の行がコンテナ上に均等に配置され、最初の行が開始エッジに、最後の行が最後のエッジに配置されます。space-around
: 複数行の子要素がコンテナ上に均等に配置され、各行の間には空白が入ります。
さらに多くの例は自分でテストする必要があります。詳細については、公式 Web サイトの AIPアプレット構成 | WeChat オープン ドキュメント を参照するか、Flex レイアウト構文チュートリアル | ルーキー チュートリアル (runoob.com)を参照してください。
2. 総合的な事例
1、スワイプ
1.1. 共通の属性
属性 | タイプ | デフォルト値 | 必須 | 説明する | 最小バージョン | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
インジケータードット | ブール値 | 間違い | いいえ | パネルインジケーターポイントを表示するかどうか | 1.0.0 | |||||||||||||
インジケーターの色 | 色 | rgba(0, 0, 0, .3) | いいえ | ポインタの色 | 1.1.0 | |||||||||||||
インジケーターアクティブカラー | 色 | #000000 | いいえ | 現在選択されているインジケーターポイントの色 | 1.1.0 | |||||||||||||
自動再生 | ブール値 | 間違い | いいえ | 自動で切り替えるかどうか | 1.0.0 | |||||||||||||
現在 | 番号 | 0 | いいえ | 現在のスライダーのインデックス | 1.0.0 | |||||||||||||
間隔 | 番号 | 5000 | いいえ | 自動切り替え時間間隔 | 1.0.0 | |||||||||||||
間隔 | 番号 | 500 | いいえ | スライドアニメーションの長さ | 1.0.0 | |||||||||||||
円形 | ブール値 | 間違い | いいえ | 接続スライドを使用するかどうか | 1.0.0 | |||||||||||||
垂直 | ブール値 | 間違い | いいえ | スライド方向が縦方向かどうか | 1.0.0 | |||||||||||||
複数の項目を表示 | 番号 | 1 | いいえ | 同時に表示されるスライダーの数 | 1.9.0 | |||||||||||||
イージング関数 | 弦 | "デフォルト" | いいえ | スワイパーを指定してイージングアニメーションの種類を切り替えます | 2.6.5 | |||||||||||||
|
||||||||||||||||||
バインドチェンジ | イベントハンドル | いいえ | 現在の変更時に変更イベントがトリガーされます。event.detail = {current,source} | 1.0.0 | ||||||||||||||
バインド遷移 | イベントハンドル | いいえ | スワイパー アイテムの位置が変更されると、遷移イベント (event.detail = {dx: dx, dy: dy}) がトリガーされます。Skyline は、ワークレット コンポーネント以外のメソッドのみをコールバックとしてサポートします。 | 2.4.3 | ||||||||||||||
バインドアニメーション仕上げ | イベントハンドル | いいえ | アニメーションが終了すると、animationfinish イベントがトリガーされます。event.detail は上記と同じです。Skyline は、ワークレット コンポーネント以外のメソッドのみをコールバックとしてサポートします。 | 1.9.0 |
2. ホーム下部メニュー
新しいミニ プログラム プロジェクトを作成した後、新しいページを作成し、 app.json内のページに tabBer をバインドします。
アプリ.json
{
"pages": [
"pages/index/index",
"pages/meeting/list/list",
"pages/vote/list/list",
"pages/ucenter/index/index",
"pages/logs/logs"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "Weixin",
"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": "设置"
}
]
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
効果
3. ホームページコンテンツ構築
3.1. バックエンドインターフェースの作成
プロジェクト内にファイルconfig/api.jsを作成します。
// 以下是业务服务器API地址 // 本机开发API地址 var WxApiRoot = 'http://localhost:8080/demo/wx/'; // 测试环境部署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', //首页数据接口 SwiperImgs: WxApiRoot+'swiperImgs', //轮播图 MettingInfos: WxApiRoot+'meeting/list', //会议信息 };
3.2. モックによるデータの作成
液面の背景には接続していないので、アプレットのモックを使用して偽のデータをシミュレートできます。
偽のデータを使用して7に入れます
{ "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" } }
ホームページにメソッドテストを書きます
インデックス.js
// index.js // 获取应用实例 const app = getApp() const api = require("../../config/api") Page({ data: { imgSrcs:[] }, // 事件处理函数 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 }) } // 一进来就调用轮播图的方法 this.loadSwiperImgs(); }, getUserProfile(e) { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 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 }) } // 一进来就调用轮播图的方法 this.loadSwiperImgs(); } })
[注意] この場所で忘れずに開いてください
コンパイル後、コンパイラーで出力されたデータを確認します。
3.3. カルーセルチャートの作成
液面データはすでに確立しているので、ここにカルーセル チャート ページを作成します。
<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.4. 事例 -- ホームページコンテンツ構築
カンファレンスケースを使用してコンテンツの例を示します
前の手順に基づいて、ホームページによって呼び出されるメソッドを作成します。
//首页内容 loadMeetingInfos() { let that = this; wx.request({ url: api.MettingInfos, dataType: 'json', success(res) { console.log(res) that.setData({ lists: res.data.lists }) } }) }
Mac を使用してデータをシミュレートする
JSONデータパケット
{ "data": { "lists": [ { "id": "1", "image": "/static/persons/1.jpg", "title": "对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】", "num":"304", "state":"进行中", "starttime": "2022-03-13 00:00:00", "location": "深圳市·南山区" }, { "id": "1", "image": "/static/persons/2.jpg", "title": "AI WORLD 2016世界人工智能大会", "num":"380", "state":"已结束", "starttime": "2022-03-15 00:00:00", "location": "北京市·朝阳区" }, { "id": "1", "image": "/static/persons/3.jpg", "title": "H100太空商业大会", "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": "北京市·朝阳区" }, { "id": "1", "image": "/static/persons/5.jpg", "title": "新质生活 · 品质时代 2016消费升级创新大会", "num":"217", "state":"进行中", "starttime": "2022-03-13 00:00:00", "location": "北京市·朝阳区" } ] }, "statusCode": "200", "header": { "content-type":"applicaiton/json;charset=utf-8" } }
レイアウト
<view class="indexbg"> <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 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" class="bg"> <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> </view>
インデックス.js
// index.js // 获取应用实例 const app = getApp() const api = require("../../config/api") Page({ data: { imgSrcs: [{ "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" }], "lists": [ { "id": "1", "image": "/static/persons/1.jpg", "title": "对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】", "num": "304", "state": "进行中", "starttime": "2022-03-13 00:00:00", "location": "深圳市·南山区" }, { "id": "1", "image": "/static/persons/2.jpg", "title": "AI WORLD 2016世界人工智能大会", "num": "380", "state": "已结束", "starttime": "2022-03-15 00:00:00", "location": "北京市·朝阳区" }, { "id": "1", "image": "/static/persons/3.jpg", "title": "H100太空商业大会", "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": "北京市·朝阳区" }, { "id": "1", "image": "/static/persons/5.jpg", "title": "新质生活 · 品质时代 2016消费升级创新大会", "num": "217", "state": "进行中", "starttime": "2022-03-13 00:00:00", "location": "北京市·朝阳区" } ] }, // 事件处理函数 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 }) } }) }, //首页会议信息的ajax loadMeetingInfos() { let that = this; wx.request({ url: api.MettingInfos, dataType: 'json', success(res) { console.log(res) that.setData({ lists: res.data.lists }) } }) }, onLoad() { if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } // 一进来就调用轮播图的方法 this.loadSwiperImgs(); }, getUserProfile(e) { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 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 }) } })
wxss
/**index.wxss**/ .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; } /**index.wxss**/ .section { color: #aaa; display: flex; justify-content: center; } .list-info { color: #aaa; } .list-num { color: red; /* font-weight: 700; */ } .join { padding: 0px 0px 0px 10px; color: #aaa; } .state { margin: 0px 6px 0px 6px; border: 1px solid #4083ff; color: #4083ff; padding: 3px 5px 3px 5px; } .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; background-color: seashell; border-bottom: 1px solid #cecece; padding: 10px; } .mobi-text { font-weight: 700; padding: 15px; } /* .mobi-icon { border-left: 5px solid #57f564; } */ .indexbg{ background-color: rgba(219, 219, 219, 0.678); } .mobi-title { /* background-color: rgba(219, 219, 219, 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; }