博客列表 >10 弹性盒子grow、shrink因子及移动电商首页简单操练

10 弹性盒子grow、shrink因子及移动电商首页简单操练

老黑
老黑原创
2020年06月26日 09:01:33777浏览

主要内容:

  1. item增长因子(flex-grow)、收缩因子(flex-shrink)及尺寸设置的优先级、简写
  2. PC端、移动端解决方案实战(主要练习弹性盒的各种操作)

1、item grow、shrink解决弹性盒大小问题

  • 这是一个很基础的功能,毕竟盒子大小无法控制的话,那很多布局使用等于还是有问题。
  • grow、shrink通过赋值的方式可以让内部的盒子快速达到理想分布。
  • 基本的原理也比较好理解。例如grow,如果剩余空间为3,刚好有3个元素(可以是最小单元的,也可以是一个弹性盒包),如果大家的因子都是1,那么每个就得1。如果两个为1,一个为0.5,那么每一单元就是3/2.5。然后每个去拿自己对应的部分。
  • 但是重点来了:如果所有item的grow factor加起来不等于1的时候,就按它们自己直接声称的比例去赋予。例如同样一个item的grow factor为0.5。如果只有它自己的话,或者连同其他item的值加起来不够1的时候,总剩余为150px的情况下,它可以得到75(150乘以0.5)。
    但是如果总值大于1,例如另外两个为1,它自己为0.5,那么它就只能得到150/2.5乘0.5=30px了。
  • shrink同样,总的多了多少,然后每个根据自己claim的因子大小去shrink对应的比例。
  • 不得不说老外的这种思路挺好的。
    1. <div class="container">
    2. <div class="item">1</div>
    3. <div class="item">2</div>
    4. <div class="item">3</div>
    5. </div>
    1. <style>
    2. .container {
    3. width: 300px;
    4. display: flex;
    5. }
    6. .container > .item {
    7. width: 60px;
    8. /* 默认为0,不放大 */
    9. flex-grow: 0;
    10. /* 3 * 60 = 180, 主轴空间的宽度300,剩余空间为300-180=120px */
    11. /* 120/3 = 40px,均分的话,每个人可以得到40。这个通过firefox中的元素查看可以看到对应的增长 */
    12. flex: 1;
    13. flex: auto;
    14. }
    15. .container > .item:first-of-type {
    16. flex-grow: 0.5;
    17. }
    18. .container > .item:nth-of-type(2) {
    19. flex-grow: 1;
    20. }
    21. .container > .item:last-of-type {
    22. flex-grow: 1;
    23. }
    24. /* 增长因子和: 0.5 + 1 + 1 = 2.5 */
    25. /* 120px / 2.5 = 48px */
    26. /* 第2个,第3个最终为: 108px(60+48*1) */
    27. /* 第1个最终是84px(60+48*0.5) */
    28. </style>
    自己尝试的小于的例子:
    1. <style>
    2. .container {
    3. width: 300px;
    4. height: 45px;
    5. display: flex;
    6. }
    7. .container .item{
    8. width: 50px;
    9. font-size: 14px;
    10. /* flex-grow: 0.2; */
    11. }
    12. .container :first-of-type{
    13. flex-grow: 0.5;
    14. }
    15. .container :nth-last-child(2){
    16. flex-grow: 0.1;
    17. }
    18. .container :last-of-type{
    19. flex-grow: 0.1;
    20. }
    21. </style>

2、简写、shrink的优先次序

  • ① 默认不放大、可收缩,使用原始大小。“flex: 0 1 auto;” 第一个0为不放大,第二个1为可收缩(占满收缩)。auto:为原始尺寸(flex-basis),为0或1。
    【注意】flex:0 1 auto不是放在弹性盒子里面,而是放在元素下。盒子中设置就会返回说上一级不是弹性盒因此无效。如图:

  1. <style>
  2. .container {
  3. width: 300px;
  4. display: flex;
  5. }
  6. .container > .item {
  7. width: 60px;
  8. /* flex: 放大因子 收缩因子 基础尺寸 */
  9. /* 默认不放大,可收缩,尺寸使用原始大小 */
  10. flex: 0 1 auto;
  11. /* 可放大可收缩尺寸自动 */
  12. flex: 1 1 auto;
  13. /* 等价于 */
  14. flex: 1;
  15. flex: auto;
  16. /* 禁止放大和收缩,保持原样 */
  17. flex: 0 0 auto;
  18. flex: none;
  19. /* flex: 0; */
  20. /* 等价于 */
  21. flex: 0 0 0%;
  22. /* 常用的值: 0 , 1, auto */
  23. }
  24. </style>
  • ② 大小的优先次序。min-width > flex-basis > width

    1. .container > .item {
    2. /* 原始大小,初始大小 */
    3. width: 60px;
    4. /* 基础尺寸优先级大于原始大小 */
    5. flex-basis: 80px;
    6. width: 60px;
    7. /* 最小宽度优先级又大于基础尺寸 */
    8. /* min-width: 90px; */
  • ③ 为了方便显示shrink,可以将container的高度调高,item的高度调低,效果如下图。

3、PC实战 - 典型网站三列

  • 最大的体会是:化繁为简。先框架再细节。
  • 自己的一个作业。其中有两个问题暂时解决不了,具体见下面的代码里面:
id 问题
1 list中的last-of-type右侧浮动不发挥作用。
2 list:hover时无法排除first-of-typle。
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. body{
  15. margin: auto;
  16. }
  17. .header, .footer{
  18. background-color: #555555;
  19. color: aqua;
  20. height: 50px;
  21. display: flex;
  22. align-items: center;
  23. }
  24. .footer{
  25. flex-direction: column;
  26. justify-content: center;
  27. }
  28. li{
  29. list-style: none;
  30. float:left;
  31. margin: 0 10px;
  32. }
  33. /* 下面这个不发挥作用。貌似这种情况用not进行限制无效。
  34. .header ul li:hover :not(:first-of-type){
  35. background-color: brown;
  36. color: chartreuse;
  37. font-size: large;
  38. cursor: pointer;
  39. }
  40. 但如果如果是a标签的时候却可以:
  41. header > a:hover:not(:first-of-type) {
  42. color: coral;
  43. }
  44. 下面这个也是可以用的。
  45. .header ul li:nth-of-type(1){
  46. margin-right: 40px;
  47. }
  48. */
  49. .header > ul > li:hover :not(:first-of-type) {
  50. background-color: brown;
  51. color: chartreuse;
  52. font-size: large;
  53. cursor: pointer;
  54. }
  55. /* li:hover{
  56. background-color: brown;
  57. color: chartreuse;
  58. font-size: large;
  59. cursor: pointer;
  60. } */
  61. .header ul li:first-of-type{
  62. margin-right: 40px;
  63. color: gold;
  64. }
  65. /* 测试下来发现1:貌似li无法进行右侧浮动。
  66. .header ul li:last-of-type{
  67. float: right;
  68. color:gold;
  69. } */
  70. .content{
  71. height: 450px;
  72. width: 800px;
  73. display: flex;
  74. margin: 5px auto;
  75. }
  76. .content >.item{
  77. width: 180px;
  78. border: 1px solid black;
  79. }
  80. .content >.item:nth-of-type(2){
  81. flex-grow: 1;
  82. margin: 0 10px;
  83. }
  84. .footer{
  85. margin: auto;
  86. }
  87. </style>
  88. <body>
  89. <div class="header">
  90. <ul>
  91. <li class="meau">宏宇</li>
  92. <li class="meau">凡界</li>
  93. <li class="meau">妖界</li>
  94. <li class="meau">凡界</li>
  95. <li class="meau">造册</li>
  96. <li class="meau">帮助</li>
  97. </ul>
  98. </div>
  99. <div class="content">
  100. <div class="item">左宇</div>
  101. <div class="item">中宙</div>
  102. <div class="item">右宇</div>
  103. </div>
  104. <div class="footer">
  105. <div class="p">凡天下人事,人间妖,天上仙,均有其道</div>
  106. <div class="p">混沌一开,万物显灵</div>
  107. </div>
  108. </body>
  109. </html>

值得学习的几个点:

① 老司机对footer的简化处理:

  1. footer {
  2. display: flex;
  3. flex-flow: column nowrap;
  4. text-align: center;
  5. }

② header直接用a来解决。而非div中用class的header。

  1. <header>
  2. <a href="">LOGO</a>
  3. <a href="">首页</a>
  4. <a href="">栏目1</a>
  5. <a href="">栏目2</a>
  6. <a href="">栏目3</a>
  7. <a href="">登录</a>
  8. </header>

4、移动实战 - 电商首页

移动端开发,先将浏览器展示端从pc切换到手机。

firefox元素检查右上角有一个切换按钮,点击下就可以。

(先看自己的后再看老司机的,发现有很多点值得学习,在后面有说明)

下面是最后截图:

下面是代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. a {
  15. text-decoration: none;
  16. color: #666;
  17. }
  18. html {
  19. width: 100vw;
  20. height: 100vh;
  21. font-size: 14px;
  22. color: #666;
  23. }
  24. body {
  25. min-width: 360px;
  26. background-color: #fff;
  27. flex-flow: column nowrap;
  28. }
  29. .flex {
  30. display: flex;
  31. position: relative;
  32. }
  33. .h1 {
  34. height: 35px;
  35. background-color: lightgray;
  36. justify-content: space-between;
  37. }
  38. .logo > img {
  39. height: 35px;
  40. }
  41. .h2 {
  42. height: 180px;
  43. flex-direction: row;
  44. justify-content: center;
  45. }
  46. .h2 > img {
  47. height: 100%;
  48. }
  49. .h3 {
  50. width: 355px;
  51. height: 188px;
  52. flex-wrap: wrap;
  53. margin: 10px auto;
  54. justify-content: center;
  55. }
  56. .head {
  57. display: flex;
  58. flex-direction: column;
  59. align-items: center;
  60. }
  61. .h3 > div > img {
  62. width: 60px;
  63. margin: 0 10px;
  64. }
  65. .h3 > div > img,
  66. a:hover {
  67. cursor: pointer;
  68. color: lime;
  69. }
  70. footer {
  71. color: #666;
  72. background-color: #efefef;
  73. border-top: 1px solid #ccc;
  74. height: 55px;
  75. /* position: fixed; */
  76. bottom: 0;
  77. width: 100vw;
  78. display: flex;
  79. justify-content: space-evenly;
  80. }
  81. footer > a {
  82. display: flex;
  83. flex-direction: column;
  84. align-items: center; /*这个没想到用,结果字体不知道怎么去居中。纵轴的操作*/
  85. }
  86. body > footer a > span:first-of-type {
  87. /* 图标字体应该大一些 */
  88. font-size: 1.6rem;
  89. }
  90. /*---中间相对复杂的部分-----*/
  91. section{
  92. width: 358px;
  93. display: flex;
  94. flex-flow: row wrap;
  95. justify-content: center;
  96. }
  97. section> div > a > img {
  98. width: 90px;
  99. }
  100. section > .goodshow {
  101. width: 110px;
  102. display: flex;
  103. flex-flow: column nowrap;
  104. align-items: center;
  105. }
  106. section> div> a {
  107. display: flex;
  108. flex-flow: row nowrap;
  109. flex-grow: 1;
  110. justify-content: center;
  111. }
  112. .info {
  113. height: 16px;
  114. display: flex;
  115. flex-flow: row nowrap;
  116. justify-content: space-around;
  117. }
  118. </style>
  119. <body>
  120. <header class="flex h1">
  121. <div class="logo"><img src="static\images\logo.jpg" alt="" /></div>
  122. <div class="meau">=</div>
  123. </header>
  124. <header class="flex h2">
  125. <img src="static\images\banner.jpg" alt="" />
  126. </header>
  127. <header class="flex h3">
  128. <div class="head">
  129. <img src="static\images\link1.webp" alt="" />
  130. <a href="">牛头</a>
  131. </div>
  132. <div class="head">
  133. <img src="static\images\link2.webp" alt="" />
  134. <a href="">马面</a>
  135. </div>
  136. <div class="head">
  137. <img src="static\images\link3.webp" alt="" />
  138. <a href="">狗尾</a>
  139. </div>
  140. <div class="head">
  141. <img src="static\images\link4.webp" alt="" />
  142. <a href="">狼爪</a>
  143. </div>
  144. <div class="head">
  145. <img src="static\images\link1.webp" alt="" />
  146. <a href="">云气</a>
  147. </div>
  148. <div class="head">
  149. <img src="static\images\link2.webp" alt="" />
  150. <a href="">天色</a>
  151. </div>
  152. <div class="head">
  153. <img src="static\images\link3.webp" alt="" />
  154. <a href="">红日</a>
  155. </div>
  156. <div class="head">
  157. <img src="static\images\link4.webp" alt="" />
  158. <a href="">金鲜</a>
  159. </div>
  160. </header>
  161. <!--这块是中间部分-->
  162. <h2>
  163. 风高云淡<span class="iconfont hot"></span>
  164. </h2>
  165. <section>
  166. <div class="goodshow">
  167. <a href=""><img src="static\images\goods2.jpg" alt=""></a>
  168. <div class="info">
  169. <p class="name">Nokia</p>
  170. <p class="price">2000尾</p>
  171. </div>
  172. <div class="iconfont hot"></div>
  173. </div>
  174. <div class="goodshow">
  175. <a href=""><img src="static\images\goods1.jpg" alt=""></a>
  176. <div class="info">
  177. <p class="name">Nokia</p>
  178. <p class="price">2000尾</p>
  179. </div>
  180. <div class="iconfont hot"></div>
  181. </div>
  182. <div class="goodshow">
  183. <a href=""><img src="static\images\goods3.jpg" alt=""></a>
  184. <div class="info">
  185. <p class="name">Nokia</p>
  186. <p class="price">2000尾</p>
  187. </div>
  188. <div class="iconfont hot"></div>
  189. </div>
  190. <div class="goodshow">
  191. <a href=""><img src="static\images\goods2.jpg" alt=""></a>
  192. <div class="info">
  193. <p class="name">Nokia</p>
  194. <p class="price">2000尾</p>
  195. </div>
  196. <div class="iconfont hot"></div>
  197. </div>
  198. <div class="goodshow">
  199. <a href=""><img src="static\images\goods1.jpg" alt=""></a>
  200. <div class="info">
  201. <p class="name">Nokia</p>
  202. <p class="price">2000尾</p>
  203. </div>
  204. <div class="iconfont hot"></div>
  205. </div>
  206. </section>
  207. <!---->
  208. <footer>
  209. <a href="">
  210. <span class="iconfont"></span>
  211. <span>首页</span>
  212. </a>
  213. <a href="">
  214. <span class="iconfont"></span>
  215. <span>凡品</span>
  216. </a>
  217. <a href="">
  218. <span class="iconfont"></span>
  219. <span>妖品</span>
  220. </a>
  221. <a href="">
  222. <span class="iconfont"></span>
  223. <span>仙品</span>
  224. </a>
  225. </footer>
  226. </body>
  227. </html>
  • 老司机经验学习:
    ① 一上来的对Style的基本处理:清零及基本配置做好。例如:
id 详情
1 整体的潜在设置取消掉
2 整体的字体大小、颜色灰度
3 移动端大结构方面的column方向设置,nowrap
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. a {
  7. text-decoration: none;
  8. color: #666;
  9. }
  10. html {
  11. /* vw: 可视区宽度,100指100份 */
  12. /* vh:可视区的高度,100指100分 */
  13. width: 100vw;
  14. height: 100vh;
  15. font-size: 14px;
  16. color: #666;
  17. }
  18. body {
  19. min-width: 360px;
  20. background-color: #fff;
  21. display: flex;
  22. flex-flow: column nowrap;
  23. }

② header是这样处理的,用了字体图标,自己却忘了:

  1. <header>
  2. <a href="">LOGO</a>
  3. <span class="iconfont"></span>
  4. </header>

header的处理,通过jusity-content中的space-between来让两元素分别显示到两侧。

postion:fixed来确定一直显示,不随下滑而消失。

  1. body > header {
  2. color: white;
  3. background-color: #333;
  4. height: 30px;
  5. width: 100vw;
  6. padding: 0 15px;
  7. position: fixed;
  8. display: flex;
  9. align-items: center;
  10. justify-content: space-between;
  11. }

③ 主体部分,每个div下面用两个a来实现。自己的没有第一个a,这样可能点了图片就没有效果。

  1. <div>
  2. <a href=""><img src="static/images/link1.webp" alt="" /></a>
  3. <a href="">京东超市</a>
  4. </div>

④ footer的做法(space-evenly来做平均分配;居中显示在横轴的时候用align-items)

  1. body > footer {
  2. color: #666;
  3. background-color: #efefef;
  4. border-top: 1px solid #ccc;
  5. height: 55px;
  6. position: fixed;
  7. bottom: 0;
  8. width: 100vw;
  9. display: flex;
  10. justify-content: space-evenly;
  11. }
  12. body > footer a {
  13. margin-top: 10px;
  14. font-size: 0.8rem;
  15. display: flex;
  16. /* 垂直排列不换行 */
  17. flex-flow: column nowrap;
  18. /* 交叉轴项目居中显示 */
  19. align-items: center;
  20. }
  21. body > footer a > span:first-of-type {
  22. /* 图标字体应该大一些 */
  23. font-size: 1.6rem;
  24. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议