博客列表 >两个经典的组件flex写法案例(商品列表及评论区块)--PHP线上班十期1226

两个经典的组件flex写法案例(商品列表及评论区块)--PHP线上班十期1226

高的PHP十期培训学习笔记
高的PHP十期培训学习笔记原创
2019年12月31日 12:39:461067浏览

案例一:商品列表展示区块的实现方法

要实现的效果

首先分析这个案例的结构,并确定关系

结构如下

按照分析的结构写出HTML代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>二手商品列表</title>
  6. <link rel="stylesheet" href="public_second_hand.css">
  7. </head>
  8. <body>
  9. <!--标题组件-->
  10. <div class="public-headline">
  11. <span>二手交易</span>
  12. </div>
  13. <!--交易专区-->
  14. <div class="publi-second-hand">
  15. <!-- 抢好货标题-->
  16. <div class="title1">
  17. <a href="">抢好货</a>
  18. <span>0元低价,便捷,安全</span>
  19. </div>
  20. <!-- 分类标题-->
  21. <div class="title2">
  22. <span>热门分类</span>
  23. <!-- 在光标所在行按CTRL+D可以快速复制当前行-->
  24. <a href="">美女写真</a>
  25. <a href="">日本美女</a>
  26. <a href="">美国美女</a>
  27. <a href="">国内美女</a>
  28. </div>
  29. <!-- 商品内容区-->
  30. <div class="goods">
  31. <!-- 商品列表-->
  32. <div class="goods-list">
  33. <!-- 单个商品块-->
  34. <div class="intro">
  35. <a href=""><img src="../../../static/images/shop1.jpg" alt="" width="176" height="120"></a>
  36. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  37. <div>
  38. <span>&yen; 333</span>
  39. <span>美女</span>
  40. </div>
  41. </div>
  42. <!-- 单个商品块-->
  43. <div class="intro">
  44. <a href=""><img src="../../../static/images/shop2.jpg" alt="" width="176" height="120"></a>
  45. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  46. <div>
  47. <span>&yen; 333</span>
  48. <span>美女</span>
  49. </div>
  50. </div>
  51. <!-- 单个商品块-->
  52. <div class="intro">
  53. <a href=""><img src="../../../static/images/shop3.jpg" alt="" width="176" height="120"></a>
  54. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  55. <div>
  56. <span>&yen; 333</span>
  57. <span>美女</span>
  58. </div>
  59. </div>
  60. <!-- 单个商品块-->
  61. <div class="intro">
  62. <a href=""><img src="../../../static/images/shop4.jpg" alt="" width="176" height="120"></a>
  63. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  64. <div>
  65. <span>&yen; 333</span>
  66. <span>美女</span>
  67. </div>
  68. </div>
  69. <!-- 单个商品块-->
  70. <div class="intro">
  71. <a href=""><img src="../../../static/images/shop5.jpg" alt="" width="176" height="120"></a>
  72. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  73. <div>
  74. <span>&yen; 333</span>
  75. <span>美女</span>
  76. </div>
  77. </div>
  78. <!-- 单个商品块-->
  79. <div class="intro">
  80. <a href=""><img src="../../../static/images/shop6.jpg" alt="" width="176" height="120"></a>
  81. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  82. <div>
  83. <span>&yen; 333</span>
  84. <span>美女</span>
  85. </div>
  86. </div>
  87. <!-- 单个商品块-->
  88. <div class="intro">
  89. <a href=""><img src="../../../static/images/shop7.jpg" alt="" width="176" height="120"></a>
  90. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  91. <div>
  92. <span>&yen; 333</span>
  93. <span>美女</span>
  94. </div>
  95. </div>
  96. <!-- 单个商品块-->
  97. <div class="intro">
  98. <a href=""><img src="../../../static/images/shop8.jpg" alt="" width="176" height="120"></a>
  99. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  100. <div>
  101. <span>&yen; 333</span>
  102. <span>美女</span>
  103. </div>
  104. </div>
  105. </div>
  106. <!-- 右侧图片功能区-->
  107. <div class="quick-entry">
  108. <a href=""><img src="../../../static/images/1.png" alt=""></a>
  109. <a href=""><img src="../../../static/images/2.png" alt=""></a>
  110. <a href=""><img src="../../../static/images/3.png" alt=""></a>
  111. <a href=""><img src="../../../static/images/4.png" alt=""></a>
  112. <div>
  113. <a href=""><img src="../../../static/images/ad1.png" alt=""></a>
  114. <a href=""><img src="../../../static/images/ad2.jpg" alt=""></a>
  115. </div>
  116. </div>
  117. </div>
  118. </div>
  119. </body>
  120. </html>

先运行一下看下效果

添加公共初始化的样式表

将PhpStorm显示方式设置为左右两栏方式,方便对照HTML

写出CSS样式表,并做好注释,尤其是自己还不太熟悉的命令

一定要做好注释加深印象

  1. /*引入初始化样式表*/
  2. @import "../public_reset.css";
  3. /*引入标题样式*/
  4. @import "../../public/public_headline/public-headline.css";
  5. /*交易专区的样式表*/
  6. .publi-second-hand {
  7. width: 1200px;
  8. padding: 10px;
  9. /*防止盒子撑大*/
  10. box-sizing: border-box;
  11. margin: auto;
  12. /*加圆角*/
  13. border-radius: 5px;
  14. background-color: #fff;
  15. }
  16. /*鼠标经过加阴影*/
  17. .publi-second-hand:hover {
  18. box-shadow: 0 0 8px #888;
  19. }
  20. /*抢好货标题*/
  21. .publi-second-hand > .title1 {
  22. height: 50px;
  23. /*一个像素的的底部实线*/
  24. border-bottom: 1px solid #cccccc;
  25. box-sizing: border-box;
  26. }
  27. .publi-second-hand > .title1 > a {
  28. font-size: 23px;
  29. margin-right: 20px;
  30. }
  31. .publi-second-hand > .title1 > span {
  32. color: red;
  33. }
  34. /*分类标题*/
  35. .publi-second-hand > .title2 {
  36. height: 55px;
  37. }
  38. .publi-second-hand > .title2 > span {
  39. color: red;
  40. font-size: 23px;
  41. }
  42. .publi-second-hand > .title2 > span ~ a {
  43. padding-left: 20px;
  44. }
  45. /*鼠标经过分类标题时的颜色变化*/
  46. .publi-second-hand > .title2 > span ~ a:hover {
  47. color: lightcoral;
  48. }
  49. /*商品内容区*/
  50. .publi-second-hand > .goods {
  51. height: 440px;
  52. /*转为flex*/
  53. display: flex;
  54. }
  55. .publi-second-hand > .goods > .goods-list {
  56. padding: 10px;
  57. flex-basis: 800px;
  58. /*转为flex*/
  59. display: flex;
  60. flex-flow: row wrap;
  61. /*设置对齐方式为两端对齐*/
  62. justify-content: space-between;
  63. }
  64. .publi-second-hand > .goods > .goods-list > .intro{
  65. width: 178px;
  66. height: 200px;
  67. /*转为flex*/
  68. display: flex;
  69. /*设置主轴方向,并不允许换行*/
  70. flex-flow: column nowrap;
  71. justify-content: space-between;
  72. }
  73. .publi-second-hand > .goods > .goods-list > .intro img {
  74. border: 1px solid #cccccc;
  75. border-radius: 5px;
  76. }
  77. .publi-second-hand > .goods > .goods-list > .intro span:first-of-type {
  78. color: red;
  79. }
  80. .publi-second-hand > .goods > .goods-list > .intro > div {
  81. display: flex;
  82. }
  83. .publi-second-hand > .goods > .goods-list > .intro span:last-of-type {
  84. color: white;
  85. background-color: #55ce9f;
  86. padding: 0 5px;
  87. margin-left: auto;
  88. }
  89. /*右侧图片功能区*/
  90. .publi-second-hand > .goods > .quick-entry {
  91. flex-basis: 400px;
  92. padding: 10px;
  93. display: flex;
  94. /*按行方向排列并允许换行*/
  95. flex-flow: row wrap;
  96. justify-content: space-between;
  97. }
  98. .publi-second-hand > .goods > .quick-entry img {
  99. width: 190px;
  100. height: 130px;
  101. }
  102. .publi-second-hand > .goods > .quick-entry > div {
  103. width: 400px;
  104. display: flex;
  105. /*按列方向排列*/
  106. flex-direction: column;
  107. }
  108. .publi-second-hand > .goods > .quick-entry > div img {
  109. width: 393px;
  110. height: 60px;
  111. }

最终运行效果如下

案例二:评论组件的实现方法及步骤

要实现的效果

首先拿到图需要分析架构,写出HTML代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>评论组件</title>
  6. <!-- 引入字体图标样式表-->
  7. <link rel="stylesheet" href="../../../static/font/iconfont.css">
  8. <link rel="stylesheet" href="public_comment_reply.css">
  9. </head>
  10. <body>
  11. <!--先写一个大区块(同组件名)-->
  12. <div class="public-comment-reply">
  13. <!-- 发表评论区-->
  14. <div class="public-comment">
  15. <h3>发表评论</h3>
  16. <div>
  17. <label for="public-comment">
  18. <img src="../../../static/images/user.jpg" alt="">
  19. </label>
  20. <textarea name="" id="public-comment"></textarea>
  21. </div>
  22. <button>发表评论</button>
  23. </div>
  24. <!-- 评论列表显示区-->
  25. <div class="public-reply">
  26. <h3>最新回复</h3>
  27. <!-- 单条评论-->
  28. <div>
  29. <img src="../../../static/images/user.jpg" alt="">
  30. <div class="detail">
  31. <span>用户名字</span>
  32. <span>留言内容: php中文网,是一个有温度,有思想的学习平台</span>
  33. <div>
  34. <span>2019-12-12 15:34:23发表</span>
  35. <span><i class="iconfont icon-dianzan"></i>回复</span>
  36. </div>
  37. </div>
  38. </div>
  39. <div>
  40. <img src="../../../static/images/user.jpg" alt="">
  41. <div class="detail">
  42. <span>用户名字</span>
  43. <span>留言内容: php中文网,是一个有温度,有思想的学习平台</span>
  44. <div>
  45. <span>2019-12-12 15:34:23发表</span>
  46. <span><i class="iconfont icon-dianzan"></i>回复</span>
  47. </div>
  48. </div>
  49. </div>
  50. <div>
  51. <img src="../../../static/images/user.jpg" alt="">
  52. <div class="detail">
  53. <span>用户名字</span>
  54. <span>留言内容: php中文网,是一个有温度,有思想的学习平台</span>
  55. <div>
  56. <span>2019-12-12 15:34:23发表</span>
  57. <span><i class="iconfont icon-dianzan"></i>回复</span>
  58. </div>
  59. </div>
  60. </div>
  61. <div>
  62. <img src="../../../static/images/user.jpg" alt="">
  63. <div class="detail">
  64. <span>用户名字</span>
  65. <span>留言内容: php中文网,是一个有温度,有思想的学习平台</span>
  66. <div>
  67. <span>2019-12-12 15:34:23发表</span>
  68. <span><i class="iconfont icon-dianzan"></i>回复</span>
  69. </div>
  70. </div>
  71. </div>
  72. <div>
  73. <img src="../../../static/images/user.jpg" alt="">
  74. <div class="detail">
  75. <span>用户名字</span>
  76. <span>留言内容: php中文网,是一个有温度,有思想的学习平台</span>
  77. <div>
  78. <span>2019-12-12 15:34:23发表</span>
  79. <span><i class="iconfont icon-dianzan"></i>回复</span>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </body>
  86. </html>

运行效果如下:

引入公共样式表后的效果:

  1. /*初始化样式表*/
  2. @import "../public_reset.css";

按照效果图完成CSS样式部分代码的编写(做好注释方便后期修改):

  1. /*初始化样式表*/
  2. @import "../public_reset.css";
  3. /*评论样式开始*/
  4. .public-comment-reply {
  5. padding: 15px;
  6. /*防止撑大*/
  7. box-sizing: border-box;
  8. background-color: #fff;
  9. /*转为flex*/
  10. display: flex;
  11. /*按列方向显示*/
  12. flex-direction: column;
  13. }
  14. /*定义头像的样式*/
  15. .public-comment-reply img {
  16. width: 60px;
  17. height: 60px;
  18. /*圆角*/
  19. border-radius: 5px;
  20. /*加边框和阴影*/
  21. box-shadow: 1px 1px 3px #cccccc;
  22. }
  23. .public-comment-reply > .public-comment {
  24. display: flex;
  25. flex-direction: column;
  26. }
  27. .public-comment-reply > .public-comment h3 {
  28. padding: 20px 0;
  29. /*在顶部加一个线条分隔*/
  30. border-top: 1px solid #cccccc;
  31. }
  32. .public-comment-reply > .public-comment > div {
  33. padding: 15px;
  34. height: 150px;
  35. display: flex;
  36. /*按行显示,不允许换行*/
  37. flex-flow: row nowrap;
  38. }
  39. .public-comment-reply > .public-comment > div img {
  40. /*单独定义头像的对齐方式*/
  41. align-self: flex-start;
  42. }
  43. .public-comment-reply > .public-comment > div textarea {
  44. height: 150px;
  45. margin-left: 20px;
  46. /*不允许拉伸文本框*/
  47. resize: none;
  48. flex: auto;
  49. }
  50. /*文本框加阴影*/
  51. .public-comment-reply > .public-comment > div textarea:hover {
  52. box-shadow: 0 0 8px #888888;
  53. }
  54. .public-comment-reply > .public-comment > button {
  55. font-size: 14px;
  56. background-color: #f64c59;
  57. /*去掉按钮边框*/
  58. border: none;
  59. color: white;
  60. width: 150px;
  61. height: 40px;
  62. /*定义按钮的对齐位置*/
  63. align-self: end;
  64. }
  65. /*鼠标经过按钮时的效果*/
  66. .public-comment-reply > .public-comment > button:hover {
  67. background-color: #178cee;
  68. box-shadow: 0 0 8px #888888;
  69. /*给鼠标变一个指针(小手)*/
  70. cursor: pointer;
  71. }
  72. /*回复区*/
  73. .public-comment-reply > .public-reply {
  74. display: flex;
  75. /*列对齐*/
  76. flex-direction: column;
  77. padding: 15px 0;
  78. }
  79. .public-comment-reply > .public-reply > h3 {
  80. padding: 20px 0;
  81. }
  82. .public-comment-reply > .public-reply > div {
  83. display: flex;
  84. margin-top: 30px;
  85. }
  86. /*图片居中*/
  87. .public-comment-reply > .public-reply > div > img {
  88. align-self: center;
  89. }
  90. .public-comment-reply > .public-reply > div > .detail {
  91. flex: auto;
  92. display: flex;
  93. flex-direction: column;
  94. margin-left: 20px;
  95. height: 90px;
  96. /*两端对齐*/
  97. justify-content: space-between;
  98. }
  99. .public-comment-reply > .public-reply > div > .detail > div {
  100. display: flex;
  101. /*两端对齐*/
  102. justify-content: space-between;
  103. }
  104. .public-comment-reply > .public-reply > div > .detail > div i {
  105. color: red;
  106. /*默认字体的1.2倍*/
  107. font-size: 1.2rem;
  108. margin-right: 5px;
  109. }

最终运行效果

此效果中将 “发表评论” 的按钮靠右侧对齐,和参照图有所区别,更美观,代码如下:

  1. /*定义按钮的对齐位置*/
  2. align-self: end;
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议