当前位置:   article > 正文

【微信小程序】6天精准入门(第3天:小程序flex布局、轮播图组件及mock运用以及综合案例)附源码_微信小程序实例附源代码

微信小程序实例附源代码

一、flex布局

布局的传统解决方案,基于[盒状模型],依赖display属性 + position属性 + float属性

1、什么是flex布局?

  1. Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。
  2. 任何一个容器都可以指定为Flex布局。
  3. display: ‘flex’

        容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)

  • 主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;
  • 交叉轴的开始位置叫做cross start,结束位置叫做cross end。

        项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size。

2、flex属性

属性作用
flex-direction主轴的方向  默认为row
flex-wrap如果一条轴线排不下,如何换行
flex-flow是flex-direction属性和flex-wrap属性的简写形式
justify-content定义了项目在主轴上的对齐方式
align-items定义项目在交叉轴上如何对齐
align-content属性定义了多根轴线的对齐方式

【注意】设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。

  • flex-direction 定义了子元素在主轴(沿着容器的主轴线)上的排列方式。它包括以下几个值:

    • row(默认值):子元素在主轴上从左往右排列。
    • row-reverse:子元素在主轴上从右往左排列。
    • column:子元素在主轴上从上往下排列。
    • column-reverse:子元素在主轴上从下往上排列。
  • flex-wrap 定义了子元素在容器宽度不足时如何换行。它包括以下几个值:

    • nowrap(默认值):子元素不换行,超出容器宽度部分会被压缩。
    • wrap:子元素按行换行,超出容器宽度的子元素会移动到下一行。
    • wrap-reverse:子元素按行反向换行,超出容器宽度的子元素会从下一行开始排列。
  • flex-flow 是 flex-direction 和 flex-wrap 两个属性的简写形式。它包含两个值,以空格分隔:

    • 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:多行子元素均匀分布在容器上,各行之间有空白间隔。

更多实例需要自己进行测试,更详情的可以查看官网AIP小程序配置 | 微信开放文档,或者查看Flex 布局语法教程 | 菜鸟教程 (runoob.com)

二、综合案例

1、swiper

1.1、通用属性

属性类型默认值必填说明最低版本
indicator-dotsbooleanfalse是否显示面板指示点1.0.0
indicator-colorcolorrgba(0, 0, 0, .3)指示点颜色1.1.0
indicator-active-colorcolor#000000当前选中的指示点颜色1.1.0
autoplaybooleanfalse是否自动切换1.0.0
currentnumber0当前所在滑块的 index1.0.0
intervalnumber5000自动切换时间间隔1.0.0
durationnumber500滑动动画时长1.0.0
circularbooleanfalse是否采用衔接滑动1.0.0
verticalbooleanfalse滑动方向是否为纵向1.0.0
display-multiple-itemsnumber1同时显示的滑块数量1.9.0
easing-functionstring"default"指定 swiper 切换缓动动画类型2.6.5
合法值说明
default默认缓动函数
linear线性动画
easeInCubic缓入动画
easeOutCubic缓出动画
easeInOutCubic缓入缓出动画
bindchangeeventhandlecurrent 改变时会触发 change 事件,event.detail = {current, source}1.0.0
bindtransitioneventhandleswiper-item 的位置发生改变时会触发 transition 事件,event.detail = {dx: dx, dy: dy}。Skyline 仅支持非 worklet 的组件方法作为回调。2.4.3
bindanimationfinisheventhandle动画结束时会触发 animationfinish 事件,event.detail 同上。Skyline 仅支持非 worklet 的组件方法作为回调。1.9.0

2、首页底部菜单

创建新的小程序项目之后,在app.json里面pages新建页面和绑定tabBer

app.json

  1. {
  2. "pages": [
  3. "pages/index/index",
  4. "pages/meeting/list/list",
  5. "pages/vote/list/list",
  6. "pages/ucenter/index/index",
  7. "pages/logs/logs"
  8. ],
  9. "window": {
  10. "backgroundTextStyle": "light",
  11. "navigationBarBackgroundColor": "#fff",
  12. "navigationBarTitleText": "Weixin",
  13. "navigationBarTextStyle": "black"
  14. },
  15. "tabBar": {
  16. "list": [
  17. {
  18. "pagePath": "pages/index/index",
  19. "text": "首页",
  20. "iconPath": "/static/tabBar/coding.png",
  21. "selectedIconPath": "/static/tabBar/coding-active.png"
  22. },
  23. {
  24. "pagePath": "pages/meeting/list/list",
  25. "iconPath": "/static/tabBar/sdk.png",
  26. "selectedIconPath": "/static/tabBar/sdk-active.png",
  27. "text": "会议"
  28. },
  29. {
  30. "pagePath": "pages/vote/list/list",
  31. "iconPath": "/static/tabBar/template.png",
  32. "selectedIconPath": "/static/tabBar/template-active.png",
  33. "text": "投票"
  34. },
  35. {
  36. "pagePath": "pages/ucenter/index/index",
  37. "iconPath": "/static/tabBar/component.png",
  38. "selectedIconPath": "/static/tabBar/component-active.png",
  39. "text": "设置"
  40. }
  41. ]
  42. },
  43. "style": "v2",
  44. "sitemapLocation": "sitemap.json"
  45. }

效果

3、首页内容搭建

3.1、创建后端结口

在你的项目里面创建一个文件config/api.js

  1. // 以下是业务服务器API地址
  2. // 本机开发API地址
  3. var WxApiRoot = 'http://localhost:8080/demo/wx/';
  4. // 测试环境部署api地址
  5. // var WxApiRoot = 'http://192.168.0.101:8070/demo/wx/';
  6. // 线上平台api地址
  7. //var WxApiRoot = 'https://www.oa-mini.com/demo/wx/';
  8. module.exports = {
  9. IndexUrl: WxApiRoot + 'home/index', //首页数据接口
  10. SwiperImgs: WxApiRoot+'swiperImgs', //轮播图
  11. MettingInfos: WxApiRoot+'meeting/list', //会议信息
  12. };

3.2、Mock创建数据

液位我们没有连接到后台,所以我们可以利用小程序里面的Mock模拟一些假的数据。

利用我们的假数据放进7

  1. {
  2. "data": {
  3. "images":[
  4. {
  5. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner1.png",
  6. "text": "1"
  7. },
  8. {
  9. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner2.png",
  10. "text": "2"
  11. },
  12. {
  13. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner3.png",
  14. "text": "3"
  15. },
  16. {
  17. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner4.png",
  18. "text": "4"
  19. },
  20. {
  21. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png",
  22. "text": "5"
  23. },
  24. {
  25. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner6.png",
  26. "text": "6"
  27. }
  28. ]
  29. },
  30. "statusCode": "200",
  31. "header": {
  32. "content-type":"applicaiton/json;charset=utf-8"
  33. }
  34. }

我们在主页编写方法测试

index.js

  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const api = require("../../config/api")
  5. Page({
  6. data: {
  7. imgSrcs:[]
  8. },
  9. // 事件处理函数
  10. bindViewTap() {
  11. wx.navigateTo({
  12. url: '../logs/logs'
  13. })
  14. },
  15. // 轮播图的方法
  16. loadSwiperImgs(){
  17. let that=this;
  18. wx.request({
  19. url: api.SwiperImgs,
  20. dataType: 'json',
  21. success(res) {
  22. console.log(res)
  23. that.setData({
  24. imgSrcs:res.data.images
  25. })
  26. }
  27. })
  28. },
  29. onLoad() {
  30. if (wx.getUserProfile) {
  31. this.setData({
  32. canIUseGetUserProfile: true
  33. })
  34. }
  35. // 一进来就调用轮播图的方法
  36. this.loadSwiperImgs();
  37. },
  38. getUserProfile(e) {
  39. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  40. wx.getUserProfile({
  41. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  42. success: (res) => {
  43. console.log(res)
  44. this.setData({
  45. userInfo: res.userInfo,
  46. hasUserInfo: true
  47. })
  48. }
  49. })
  50. },
  51. getUserInfo(e) {
  52. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
  53. console.log(e)
  54. this.setData({
  55. userInfo: e.detail.userInfo,
  56. hasUserInfo: true
  57. })
  58. }
  59. })

关键性的代码

  1. const api = require("../../config/api")
  2. Page({
  3. data: {
  4. imgSrcs:[]
  5. },
  6. // 轮播图的方法
  7. loadSwiperImgs(){
  8. let that=this;
  9. wx.request({
  10. url: api.SwiperImgs,
  11. dataType: 'json',
  12. success(res) {
  13. console.log(res)
  14. that.setData({
  15. imgSrcs:res.data.images
  16. })
  17. }
  18. })
  19. },
  20. onLoad() {
  21. if (wx.getUserProfile) {
  22. this.setData({
  23. canIUseGetUserProfile: true
  24. })
  25. }
  26. // 一进来就调用轮播图的方法
  27. this.loadSwiperImgs();
  28. }
  29. })

【注意】记得在这个位置把这个打开

我们编译之后查看编译器里打印的数据

3.3、建立轮播图

液位我们在前面已经建立了数据,所以我们在这里进行一个轮播图的的页面编写

  1. <view>
  2. <swiper autoplay="true" indicator-dots="true" indicator-color="#fff" indicator-active-color="#00f">
  3. <block wx:for="{{imgSrcs}}" wx:key="text">
  4. <swiper-item>
  5. <view>
  6. <image src="{{item.img}}" class="swiper-item" />
  7. </view>
  8. </swiper-item>
  9. </block>
  10. </swiper>
  11. </view>

3.4、案例--首页内容搭建

我们利用一个会议的案例进行一个内容的实例

在之前的基础上,创建首页调用的方法

  1. //首页内容
  2. loadMeetingInfos() {
  3. let that = this;
  4. wx.request({
  5. url: api.MettingInfos,
  6. dataType: 'json',
  7. success(res) {
  8. console.log(res)
  9. that.setData({
  10. lists: res.data.lists
  11. })
  12. }
  13. })
  14. }

利用Mack模拟数据

JSON数据包

  1. {
  2. "data": {
  3. "lists": [
  4. {
  5. "id": "1",
  6. "image": "/static/persons/1.jpg",
  7. "title": "对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】",
  8. "num":"304",
  9. "state":"进行中",
  10. "starttime": "2022-03-13 00:00:00",
  11. "location": "深圳市·南山区"
  12. },
  13. {
  14. "id": "1",
  15. "image": "/static/persons/2.jpg",
  16. "title": "AI WORLD 2016世界人工智能大会",
  17. "num":"380",
  18. "state":"已结束",
  19. "starttime": "2022-03-15 00:00:00",
  20. "location": "北京市·朝阳区"
  21. },
  22. {
  23. "id": "1",
  24. "image": "/static/persons/3.jpg",
  25. "title": "H100太空商业大会",
  26. "num":"500",
  27. "state":"进行中",
  28. "starttime": "2022-03-13 00:00:00",
  29. "location": "大连市"
  30. },
  31. {
  32. "id": "1",
  33. "image": "/static/persons/4.jpg",
  34. "title": "报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”",
  35. "num":"150",
  36. "state":"已结束",
  37. "starttime": "2022-03-13 00:00:00",
  38. "location": "北京市·朝阳区"
  39. },
  40. {
  41. "id": "1",
  42. "image": "/static/persons/5.jpg",
  43. "title": "新质生活 · 品质时代 2016消费升级创新大会",
  44. "num":"217",
  45. "state":"进行中",
  46. "starttime": "2022-03-13 00:00:00",
  47. "location": "北京市·朝阳区"
  48. }
  49. ]
  50. },
  51. "statusCode": "200",
  52. "header": {
  53. "content-type":"applicaiton/json;charset=utf-8"
  54. }
  55. }

布局

  1. <view class="indexbg">
  2. <swiper autoplay="true" indicator-dots="true" indicator-color="#fff" indicator-active-color="#00f">
  3. <block wx:for="{{imgSrcs}}" wx:key="text">
  4. <swiper-item>
  5. <view>
  6. <image src="{{item.img}}" class="swiper-item" />
  7. </view>
  8. </swiper-item>
  9. </block>
  10. </swiper>
  11. <view class="mobi-title">
  12. <text class="mobi-icon"></text>
  13. <text class="mobi-text">会议信息</text>
  14. </view>
  15. <block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id" class="bg">
  16. <view class="list" data-id="{{item.id}}">
  17. <view class="list-img">
  18. <image class="video-img" mode="scaleToFill" src="{{item.image}}"></image>
  19. </view>
  20. <view class="list-detail">
  21. <view class="list-title"><text>{{item.title}}</text></view>
  22. <view class="list-tag">
  23. <view class="state">{{item.state}}</view>
  24. <view class="join"><text class="list-num">{{item.num}}</text>人报名</view>
  25. </view>
  26. <view class="list-info"><text>{{item.location}}</text>|<text>{{item.starttime}}</text></view>
  27. </view>
  28. </view>
  29. </block>
  30. <view class="section">
  31. <text>到底啦</text>
  32. </view>
  33. </view>

index.js

  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const api = require("../../config/api")
  5. Page({
  6. data: {
  7. imgSrcs: [{
  8. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner1.png",
  9. "text": "1"
  10. },
  11. {
  12. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner2.png",
  13. "text": "2"
  14. },
  15. {
  16. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner3.png",
  17. "text": "3"
  18. },
  19. {
  20. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner4.png",
  21. "text": "4"
  22. },
  23. {
  24. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png",
  25. "text": "5"
  26. },
  27. {
  28. "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner6.png",
  29. "text": "6"
  30. }],
  31. "lists": [
  32. {
  33. "id": "1",
  34. "image": "/static/persons/1.jpg",
  35. "title": "对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】",
  36. "num": "304",
  37. "state": "进行中",
  38. "starttime": "2022-03-13 00:00:00",
  39. "location": "深圳市·南山区"
  40. },
  41. {
  42. "id": "1",
  43. "image": "/static/persons/2.jpg",
  44. "title": "AI WORLD 2016世界人工智能大会",
  45. "num": "380",
  46. "state": "已结束",
  47. "starttime": "2022-03-15 00:00:00",
  48. "location": "北京市·朝阳区"
  49. },
  50. {
  51. "id": "1",
  52. "image": "/static/persons/3.jpg",
  53. "title": "H100太空商业大会",
  54. "num": "500",
  55. "state": "进行中",
  56. "starttime": "2022-03-13 00:00:00",
  57. "location": "大连市"
  58. },
  59. {
  60. "id": "1",
  61. "image": "/static/persons/4.jpg",
  62. "title": "报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”",
  63. "num": "150",
  64. "state": "已结束",
  65. "starttime": "2022-03-13 00:00:00",
  66. "location": "北京市·朝阳区"
  67. },
  68. {
  69. "id": "1",
  70. "image": "/static/persons/5.jpg",
  71. "title": "新质生活 · 品质时代 2016消费升级创新大会",
  72. "num": "217",
  73. "state": "进行中",
  74. "starttime": "2022-03-13 00:00:00",
  75. "location": "北京市·朝阳区"
  76. }
  77. ]
  78. },
  79. // 事件处理函数
  80. bindViewTap() {
  81. wx.navigateTo({
  82. url: '../logs/logs'
  83. })
  84. },
  85. // 轮播图的方法
  86. loadSwiperImgs() {
  87. let that = this;
  88. wx.request({
  89. url: api.SwiperImgs,
  90. dataType: 'json',
  91. success(res) {
  92. console.log(res)
  93. that.setData({
  94. imgSrcs: res.data.images
  95. })
  96. }
  97. })
  98. },
  99. //首页会议信息的ajax
  100. loadMeetingInfos() {
  101. let that = this;
  102. wx.request({
  103. url: api.MettingInfos,
  104. dataType: 'json',
  105. success(res) {
  106. console.log(res)
  107. that.setData({
  108. lists: res.data.lists
  109. })
  110. }
  111. })
  112. },
  113. onLoad() {
  114. if (wx.getUserProfile) {
  115. this.setData({
  116. canIUseGetUserProfile: true
  117. })
  118. }
  119. // 一进来就调用轮播图的方法
  120. this.loadSwiperImgs();
  121. },
  122. getUserProfile(e) {
  123. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  124. wx.getUserProfile({
  125. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  126. success: (res) => {
  127. console.log(res)
  128. this.setData({
  129. userInfo: res.userInfo,
  130. hasUserInfo: true
  131. })
  132. }
  133. })
  134. },
  135. getUserInfo(e) {
  136. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
  137. console.log(e)
  138. this.setData({
  139. userInfo: e.detail.userInfo,
  140. hasUserInfo: true
  141. })
  142. }
  143. })

wxss

  1. /**index.wxss**/
  2. .userinfo {
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. color: #aaa;
  7. }
  8. .userinfo-avatar {
  9. overflow: hidden;
  10. width: 128rpx;
  11. height: 128rpx;
  12. margin: 20rpx;
  13. border-radius: 50%;
  14. }
  15. .usermotto {
  16. margin-top: 200px;
  17. }
  18. /**index.wxss**/
  19. .section {
  20. color: #aaa;
  21. display: flex;
  22. justify-content: center;
  23. }
  24. .list-info {
  25. color: #aaa;
  26. }
  27. .list-num {
  28. color: red;
  29. /* font-weight: 700; */
  30. }
  31. .join {
  32. padding: 0px 0px 0px 10px;
  33. color: #aaa;
  34. }
  35. .state {
  36. margin: 0px 6px 0px 6px;
  37. border: 1px solid #4083ff;
  38. color: #4083ff;
  39. padding: 3px 5px 3px 5px;
  40. }
  41. .list-tag {
  42. padding: 3px 0px 10px 0px;
  43. display: flex;
  44. align-items: center;
  45. }
  46. .list-title {
  47. display: flex;
  48. justify-content: space-between;
  49. font-size: 11pt;
  50. color: #333;
  51. font-weight: bold;
  52. }
  53. .list-detail {
  54. display: flex;
  55. flex-direction: column;
  56. margin: 0px 0px 0px 15px;
  57. }
  58. .video-img {
  59. width: 80px;
  60. height: 80px;
  61. }
  62. .list {
  63. display: flex;
  64. flex-direction: row;
  65. background-color: seashell;
  66. border-bottom: 1px solid #cecece;
  67. padding: 10px;
  68. }
  69. .mobi-text {
  70. font-weight: 700;
  71. padding: 15px;
  72. }
  73. /* .mobi-icon {
  74. border-left: 5px solid #57f564;
  75. } */
  76. .indexbg{
  77. background-color: rgba(219, 219, 219, 0.678);
  78. }
  79. .mobi-title {
  80. /* background-color: rgba(219, 219, 219, 0.678); */
  81. margin: 10px 0px 10px 0px;
  82. }
  83. .swiper-item {
  84. height: 300rpx;
  85. width: 100%;
  86. border-radius: 10rpx;
  87. }
  88. .userinfo {
  89. display: flex;
  90. flex-direction: column;
  91. align-items: center;
  92. color: #aaa;
  93. }
  94. .userinfo-avatar {
  95. overflow: hidden;
  96. width: 128rpx;
  97. height: 128rpx;
  98. margin: 20rpx;
  99. border-radius: 50%;
  100. }
  101. .usermotto {
  102. margin-top: 200px;
  103. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/249576
推荐阅读
相关标签
  

闽ICP备14008679号