微信小程序Flex弹性布局实验教程

发布时间:2026/9/11 19:02:42
微信小程序Flex弹性布局实验教程 一、实验目的1. 理解Flex弹性布局的基本概念掌握弹性容器、弹性项目、主轴、交叉轴的含义。2. 熟练使用flex‑direction、flex‑grow属性实现水平均匀布局、左右混合布局、上下混合布局。3. 将第一行水平均匀布局修改为**从右向左排列**。4. 使用scroll‑view组件实现页面竖向滚动关闭Skyline渲染引擎。二、实验环境1. 微信开发者工具三、实验原理Flex为弹性布局。设置display:flex的元素是Flex容器子元素就是Flex项目。 - flex‑direction控制主轴方向row‑reverse实现从右向左排布。 - flex‑grow分配容器剩余空间控制子元素占比。 - scroll‑view添加scroll‑y实现页面手动竖向滚动。四、实验作业要求1. 完成案例2.4 Flex弹性布局。2. 第一行水平布局改为从右到左排列。3. 保留左右混合布局、上下混合布局。4. app.json删除Skyline相关配置。5. scroll‑view实现页面滚动。五、完整源代码### app.json{pages: [pages/index/index],window: {navigationBarTextStyle: black,navigationStyle: custom},style: v2,sitemapLocation: sitemap.json,lazyCodeLoading: requiredComponents}pages/index/index.wxml!--index.wxml--navigation-bar titleFlex弹性盒模型 back{{false}} colorblack background#FFF/navigation-barscroll-view classscrollarea scroll-y typelistview classboxview classtitle水平均匀布局/viewview styledisplay: flex;flex-direction: row-reverse;line-height: 120rpx;text-align: center;view stylebackground-color: red;flex-grow: 1;中/viewview stylebackground-color: green;flex-grow: 1;华/viewview stylebackground-color: blue;flex-grow: 1;民/viewview stylebackground-color: purple;flex-grow: 1;族/view/viewview classtitle stylemargin-top: 40rpx;左右混合布局/viewview styledisplay:flex;text-align:center;view stylebackground-color:red;width:50%;line-height:240rpx;强/viewview styledisplay:flex;flex-direction:column;flex-grow:1;line-height:120rpx;view stylebackground-color:green;国/viewview stylebackground-color:blue;梦/view/view/viewview classtitle stylemargin-top:40rpx;上下混合布局/viewview styledisplay:flex;flex-direction:column;view stylebackground-color:red;line-height:120rpx;强/viewview styledisplay:flex;line-height:120rpx;view stylebackground-color:green;flex-grow:0.5;军/viewview stylebackground-color:blue;flex-grow:1;梦/view/view/view/view/scroll-viewpages/index/index.wxss/**index.wxss**/page {height: 100vh;display: flex;flex-direction: column;}.scrollarea {flex: 1;overflow-y: hidden;}app.wxss/**app.wxss**/.box {margin: 20rpx;padding: 20rpx;border: 2rpx solid silver;}.title {font-size: larger;font-weight: bolder;text-align: center;margin-bottom: 30rpx;color: red;}六、运行效果第一行水平布局从右往左显示文字顺序族、民、华、中。存在水平均匀布局、左右混合布局、上下混合布局三块内容。页面可以手指上下滑动外层银色边框标题红色居中。七、实验总结本次实验学习Flex弹性布局使用row‑reverse完成布局反转利用flex‑grow实现不等分布局。通过scroll‑view实现页面滚动。写CSS要注意单词拼写拼写错误会直接导致样式完全失效。

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询