• 微信小程序首页-----布局(详细教程赶快收藏吧)


                                                      🎬 艳艳耶✌️:个人主页

                                                      🔥 个人专栏 :《Spring与Mybatis集成整合》《Vue.js使用》

                                                      ⛺️ 越努力 ,越幸运。

    1.flex弹性布局

      Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。

    网址:Flex 布局语法教程icon-default.png?t=N7T8https://www.runoob.com/w3cnote/flex-grammar.html

    flex属性:
    flex-direction 主轴的方向 默认为row

    flex-wrap 如果一条轴线排不下,如何换行

    flex-flow 是flex-direction属性和flex-wrap属性的简写形式

    justify-content 定义了项目在主轴上的对齐方式

    align-items 定义项目在交叉轴上如何对齐

    align-content 属性定义了多根轴线的对齐方式

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

    1.1 display: flex 弹性布局属性

    前端代码:

    1. <view class="box">
    2. <view>1</view>
    3. <view>2</view>
    4. <view>3</view>
    5. <view>4</view>
    6. <view>5</view>
    7. <view>6</view>
    8. <view>7</view>
    9. <view>8</view>
    10. <view>9</view>
    11. <view>10</view>
    12. <view>11</view>
    13. <view>12</view>
    14. </view>

    样式添加:

            给每一个view设置了宽高为100rpx,众所周知小程序的手机端的宽度是750rpx

    1. .box{
    2. height: 750rpx;
    3. width: 750rpx;
    4. background-color: pink;
    5. display: flex;
    6. }
    7. view{
    8. height: 100rpx;
    9. width: 100rpx;
    10. border: 1px solid greenyellow;
    11. }

    添加display: flex之前与之后的对比: 

    1.2  flex-direction属性

    flex-direction属性决定主轴的方向(即项目的排列方向)。

     flex-direction: row | row-reverse | column | column-reverse;

    1.3 flex-wrap属性

    默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

     flex-wrap: nowrap (不换行) | wrap | wrap-reverse;

    2.4 flex-flow属性

    flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,就是将两者结合起来了,默认值为row nowrap。

    2.5 justify-content属性

     justify-content: flex-start(居右对齐) | flex-end(居左对齐) | center(居中对齐) | space-between(两端对齐,项目之间的间隔都相等) | space-around(每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。)

    2.首页轮播图

         2.1   我们先把一级菜单底座打好

    在app.json里面:

    1. "pages":[
    2. "pages/index/index",
    3. "pages/meeting/list/list",
    4. "pages/vote/list/list",
    5. "pages/ucenter/index/index",
    6. "pages/logs/logs"
    7. ],
    1. "tabBar": {
    2. "list": [{
    3. "pagePath": "pages/index/index",
    4. "text": "首页",
    5. "iconPath": "/static/tabBar/coding.png",
    6. "selectedIconPath": "/static/tabBar/coding-active.png"
    7. },
    8. {
    9. "pagePath": "pages/meeting/list/list",
    10. "iconPath": "/static/tabBar/sdk.png",
    11. "selectedIconPath": "/static/tabBar/sdk-active.png",
    12. "text": "会议"
    13. },
    14. {
    15. "pagePath": "pages/vote/list/list",
    16. "iconPath": "/static/tabBar/template.png",
    17. "selectedIconPath": "/static/tabBar/template-active.png",
    18. "text": "投票"
    19. },
    20. {
    21. "pagePath": "pages/ucenter/index/index",
    22. "iconPath": "/static/tabBar/component.png",
    23. "selectedIconPath": "/static/tabBar/component-active.png",
    24. "text": "设置"
    25. }]
    26. },

    效果展示: 

    2.2 mockJS模拟数据

    定义接口:

    右击新建立一个文件---->在建立一个js文件:

    在app.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. };

    前端轮播图代码:index.wxml

    这段代码可以从官网拿的,里面的属性官网中可以看到:

    视图容器icon-default.png?t=N7T8https://developers.weixin.qq.com/miniprogram/dev/component/swiper.htmlindex.wxml页面

    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>

    样式:index.wxss页面

    1. .swiper-item {
    2. height: 300rpx;
    3. width: 100%;
    4. border-radius: 10rpx;
    5. }

    在index.js里面添加轮播图方法:

    1. // 轮播图数据
    2. loadSwiperImgs(){
    3. let that=this;
    4. wx.request({
    5. url: api.SwiperImgs,
    6. dataType: 'json',
    7. success(res) {
    8. console.log(res)
    9. that.setData({
    10. imgSrcs:res.data.images
    11. })
    12. }
    13. })
    14. },

    在index.js的onload方法中调用:

    1. onLoad() {
    2. if (wx.getUserProfile) {
    3. this.setData({
    4. canIUseGetUserProfile: true
    5. })
    6. }
    7. this.loadSwiperImgs();
    8. },

    在index.js里面 接口mockjs

    1. // 轮播图 mockjs
    2. const api = require("../config/app.js");

     注意:在这里用的是http的网址,不是https所以会报错,需要修改.


    接着点开调式器的mock,在里面新增接口: 

    json数据:

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

    2.3轮播图效果展示:

    3.首页布局

    在静态资源文件夹 ( static ) 中创建一个放首页会议用户头像图片的文件夹 : persons

    注 : 需要在项目的本地路径下进行创建和增加

    3.1. 视图

    找到 index.wxml  将所有代码修改为以下代码 : 

    3.2. 数据

    找到 index.js 将所有代码修改为以下代码 : 

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

    3.3. 样式

     index.wxss 中编写样式,进行美化页面,以下是样式的所有代码 :

    1. /**index.wxss**/
    2. .swiper-item {
    3. height: 300rpx;
    4. width: 100%;
    5. border-radius: 10rpx;
    6. }
    7. .mobi-title {
    8. font-size: 12pt;
    9. color: #777;
    10. line-height: 110%;
    11. font-weight: bold;
    12. width: 100%;
    13. padding: 15rpx;
    14. background-color: #f3f3f3;
    15. }
    16. .mobi-icon {
    17. padding: 0rpx 3rpx;
    18. border-radius: 3rpx;
    19. background-color: #ff7777;
    20. position: relative;
    21. margin-right: 10rpx;
    22. }
    23. /*list*/
    24. .list {
    25. display: flex;
    26. flex-direction: row;
    27. width: 100%;
    28. padding: 0 20rpx 0 0;
    29. border-top: 1px solid #eeeeee;
    30. background-color: #fff;
    31. margin-bottom: 5rpx;
    32. /* border-radius: 20rpx;
    33. box-shadow: 0px 0px 10px 6px rgba(0,0,0,0.1); */
    34. }
    35. .list-img {
    36. display: flex;
    37. margin: 10rpx 10rpx;
    38. width: 150rpx;
    39. height: 220rpx;
    40. justify-content: center;
    41. align-items: center;
    42. }
    43. .list-img .video-img {
    44. width: 120rpx;
    45. height: 120rpx;
    46. }
    47. .list-detail {
    48. margin: 10rpx 10rpx;
    49. display: flex;
    50. flex-direction: column;
    51. width: 600rpx;
    52. height: 220rpx;
    53. }
    54. .list-title text {
    55. font-size: 11pt;
    56. color: #333;
    57. font-weight: bold;
    58. }
    59. .list-detail .list-tag {
    60. display: flex;
    61. height: 70rpx;
    62. }
    63. .list-tag .state {
    64. font-size: 9pt;
    65. color: #81aaf7;
    66. width: 120rpx;
    67. border: 1px solid #93b9ff;
    68. border-radius: 2px;
    69. margin: 10rpx 0rpx;
    70. display: flex;
    71. justify-content: center;
    72. align-items: center;
    73. }
    74. .list-tag .join {
    75. font-size: 11pt;
    76. color: #bbb;
    77. margin-left: 20rpx;
    78. display: flex;
    79. justify-content: center;
    80. align-items: center;
    81. }
    82. .list-tag .list-num {
    83. font-size: 11pt;
    84. color: #ff6666;
    85. }
    86. .list-info {
    87. font-size: 9pt;
    88. color: #bbb;
    89. margin-top: 20rpx;
    90. }
    91. .bottom-line{
    92. display: flex;
    93. height: 60rpx;
    94. justify-content: center;
    95. align-items: center;
    96. background-color: #f3f3f3;
    97. }
    98. .bottom-line text{
    99. font-size: 9pt;
    100. color: #666;
    101. }

    效果展示:

    今日分享结束!!!!!

  • 相关阅读:
    IC设计高级018:一种主动的流控实现方式
    《微积分的力量》读书摘记
    java计算机毕业设计健身俱乐部管理系统MyBatis+系统+LW文档+源码+调试部署
    Python实现基于alpha-beta剪枝技术的五子棋
    大数据ClickHouse进阶(六):Distributed引擎深入了解
    机器学习中矩阵可视化只显示一部分数据
    random生成随机数的灵活运用
    Linux高并发服务器开发(六)线程
    量子OFFICE之q6platform讲座:1-FileSystem
    手撕常见JS面试题
  • 原文地址:https://blog.csdn.net/2301_76988707/article/details/133882748