博客列表 >用flex布局尝试模拟一个静态的移动端京东页面

用flex布局尝试模拟一个静态的移动端京东页面

祥子弟弟
祥子弟弟原创
2020年12月25日 18:13:061521浏览

模拟静态的京东页面的总体思路

可以大致的将京东首页页面看成是由三部分组成,分别是页眉区、内容区还有页脚区。

页眉和页脚区的布局思路一样,它们的显示优先级都要大于内容区。然后可以注意到的是它的页眉和页脚区域都有许多的字体图标,这个可以在阿里巴巴矢量图标库找到相似的下载下来引用就行了。另外,所有用到图片需要自己准备哦,所有的图片在京东主页上都可以扒下来。

首先是一个整体的布局思路,可以将主页的整体先弄好,剩下的亿点点细节可以再去部署。页眉和页脚的宽度可以登录到京东首页移动端,然后使用元素查看器查看高度设置。

对于页眉,页脚还有主内容区位置这块我用了绝对定位去弄的,后期去写内容区的细节的时候不知道是什么原因,它老是被覆盖,只能重新去使用绝对定位去写,所以还得计算布局的位置,比较麻烦。

分内容去写 css,这样各个区出了问题,代码处理量就很少,方便去修改。

下边是所有的代码:

整个页面框架的 html 代码如下:(由于推荐内容区的东西结构一样且太多了,所以我就只在 html 中只写了四个,能看清效果就行)

  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. <!-- 首页 -->
  7. <link rel="stylesheet" href="static/css/index.css" />
  8. <!-- 页眉 -->
  9. <link rel="stylesheet" href="static/css/header.css" />
  10. <!-- 字体图标 -->
  11. <link rel="stylesheet" href="static/iconfont/iconfont.css" />
  12. <!-- 幻灯片 -->
  13. <link rel="stylesheet" href="static/css/slide.css" />
  14. <!-- 导航区 -->
  15. <link rel="stylesheet" href="static/css/nav.css" />
  16. <!-- 正品直邮 -->
  17. <link rel="stylesheet" href="static/css/mail.css" />
  18. <!-- 秒杀 -->
  19. <link rel="stylesheet" href="static/css/ms.css" />
  20. <!-- 猜你喜欢 -->
  21. <link rel="stylesheet" href="static/css/xh.css" />
  22. <!-- 页脚 -->
  23. <link rel="stylesheet" href="static/css/footer.css" />
  24. <title>页脚</title>
  25. </head>
  26. <body>
  27. <!-- 页眉 -->
  28. <div class="header">
  29. <!-- 字体图标菜单 -->
  30. <a href=""><div class="menu iconfont icon-menu"></div></a>
  31. <div class="search">
  32. <div class="logo iconfont icon-jingdong"></div>
  33. <div class="zoom iconfont icon-sousuo"></div>
  34. <input type="text" class="words" value="oppo手机" />
  35. </div>
  36. <a href="" class="login">登录</a>
  37. </div>
  38. <!-- 主体 -->
  39. <div class="main">
  40. <!-- 幻灯片 -->
  41. <div class="slide">
  42. <div class="box"></div>
  43. <a href=""><img src="static/img/bg/bg-3.jpg" alt="" /></a>
  44. </div>
  45. <!-- 跳转 -->
  46. <div class="jump">
  47. <a href=""><div class="jd iconfont icon-jingdongsaomazhifu"></div></a>
  48. <a href="">打开APP</a>
  49. </div>
  50. <ul class="nav">
  51. <li>
  52. <a href=""><img src="static/img/nav/nav-1.png" alt="" /></a>
  53. <a href="">京东超市</a>
  54. </li>
  55. <li>
  56. <a href=""><img src="static/img/nav/nav-2.png" alt="" /></a>
  57. <a href="">数码电器</a>
  58. </li>
  59. <li>
  60. <a href=""><img src="static/img/nav/nav-3.png" alt="" /></a>
  61. <a href="">京东服饰</a>
  62. </li>
  63. <li>
  64. <a href=""><img src="static/img/nav/nav-4.png" alt="" /></a>
  65. <a href="">京东生鲜</a>
  66. </li>
  67. <li>
  68. <a href=""><img src="static/img/nav/nav-5.png" alt="" /></a>
  69. <a href="">京东到家</a>
  70. </li>
  71. <li>
  72. <a href=""><img src="static/img/nav/nav-6.png" alt="" /></a>
  73. <a href="">充值缴费</a>
  74. </li>
  75. <li>
  76. <a href=""><img src="static/img/nav/nav-7.png" alt="" /></a>
  77. <a href="">9.9拼</a>
  78. </li>
  79. <li>
  80. <a href=""><img src="static/img/nav/nav-8.png" alt="" /></a>
  81. <a href="">领券</a>
  82. </li>
  83. <li>
  84. <a href=""><img src="static/img/nav/nav-9.png" alt="" /></a>
  85. <a href="">借钱</a>
  86. </li>
  87. <li>
  88. <a href=""><img src="static/img/nav/nav-10.png" alt="" /></a>
  89. <a href="">PLUS会员</a>
  90. </li>
  91. </ul>
  92. <ul class="zp">
  93. <li>
  94. <a href="">正品直邮</a>
  95. <a href=""><img src="static/img/zp/zp1.jpg" alt="" /></a>
  96. </li>
  97. <li>
  98. <a href="">来电好货</a>
  99. <a href="">3C大放价</a>
  100. <a href=""><img src="static/img/zp/zp2.jpg" alt="" /></a>
  101. </li>
  102. <li>
  103. <a href="">国潮风尚</a>
  104. <a href="">国货正当时</a>
  105. <a href=""><img src="static/img/zp/zp3.jpg" alt="" /></a>
  106. </li>
  107. <li>
  108. <a href="">都是你爱的</a>
  109. <a href=""><img src="static/img/zp/zp4.jpg" alt="" /></a>
  110. </li>
  111. </ul>
  112. <div class="ms">
  113. <!-- 头部区 -->
  114. <div class="ms-top">
  115. <div class="left">
  116. <div class="title">京东秒杀</div>
  117. <div class="notice">
  118. <div class="tips">22点专场</div>
  119. <div class="time">01:22:11</div>
  120. </div>
  121. </div>
  122. <div class="right">更多秒杀</div>
  123. </div>
  124. <!-- 秒杀主体区 -->
  125. <ul class="ms-body">
  126. <li class="item">
  127. <a href=""><img src="static/img/ms/ms-1.jpg" alt="" /></a>
  128. <div class="iconfont icon-renminbi1688">338</div>
  129. <div class="iconfont icon-renminbi1688">558</div>
  130. </li>
  131. <li class="item">
  132. <a href=""><img src="static/img/ms/ms-2.jpg" alt="" /></a>
  133. <div class="iconfont icon-renminbi1688">1338</div>
  134. <div class="iconfont icon-renminbi1688">1558</div>
  135. </li>
  136. <li class="item">
  137. <a href=""><img src="static/img/ms/ms-3.jpg" alt="" /></a>
  138. <div class="iconfont icon-renminbi1688">2338</div>
  139. <div class="iconfont icon-renminbi1688">2558</div>
  140. </li>
  141. <li class="item">
  142. <a href=""><img src="static/img/ms/ms-4.jpg" alt="" /></a>
  143. <div class="iconfont icon-renminbi1688">33</div>
  144. <div class="iconfont icon-renminbi1688">58</div>
  145. </li>
  146. <li class="item">
  147. <a href=""><img src="static/img/ms/ms-5.jpg" alt="" /></a>
  148. <div class="iconfont icon-renminbi1688">138</div>
  149. <div class="iconfont icon-renminbi1688">158</div>
  150. </li>
  151. <li class="item">
  152. <a href=""><img src="static/img/ms/ms-6.jpg" alt="" /></a>
  153. <div class="iconfont icon-renminbi1688">2238</div>
  154. <div class="iconfont icon-renminbi1688">2658</div>
  155. </li>
  156. </ul>
  157. </div>
  158. <!-- 推荐区 -->
  159. <h2>猜你喜欢</h2>
  160. <ul class="xh">
  161. <li class="item">
  162. <a href=""><img src="static/img/xh/xh1.webp" alt="" /></a>
  163. <p>
  164. 芝华仕 头等舱 转角科技布 布艺沙发 现代简约 组合家具 客厅小户型 50397
  165. 玛雅灰面向沙发左脚位30-60天发货
  166. </p>
  167. <div class="price">
  168. <div class="iconfont icon-renminbi1688">4088</div>
  169. <div>看相似</div>
  170. </div>
  171. </li>
  172. <li class="item">
  173. <a href=""><img src="static/img/xh/xh2.webp" alt="" /></a>
  174. <p>
  175. 米家 小米电动牙刷 声波震动 进口纤密软毛 30天超长续航 IPX7防水 T100
  176. 蓝色
  177. </p>
  178. <div class="price">
  179. <div class="iconfont icon-renminbi1688">38</div>
  180. <div>看相似</div>
  181. </div>
  182. </li>
  183. <li class="item">
  184. <a href=""><img src="static/img/xh/xh3.webp" alt="" /></a>
  185. <p>飞利浦(PHILIPS)电动剃须刀刮胡刀自动研磨便携式胡须刀PQ182</p>
  186. <div class="price">
  187. <div class="iconfont icon-renminbi1688">88</div>
  188. <div>看相似</div>
  189. </div>
  190. </li>
  191. <li class="item">
  192. <a href=""><img src="static/img/xh/xh4.webp" alt="" /></a>
  193. <p>
  194. 超媛加绒加厚中长款打底衫女冬季新款显瘦大码女装上衣立领外穿连衣裙FF-A015-C#9916你
  195. 黑色 S
  196. </p>
  197. <div class="price">
  198. <div class="iconfont icon-renminbi1688">188</div>
  199. <div>看相似</div>
  200. </div>
  201. </li>
  202. </ul>
  203. </div>
  204. <!-- 页脚 -->
  205. <div class="footer">
  206. <div>
  207. <a href=""><div class="iconfont icon-jingdong1"></div></a>
  208. <a href="">首页</a>
  209. </div>
  210. <div>
  211. <a href=""><div class="iconfont icon-2"></div></a>
  212. <a href="">分类</a>
  213. </div>
  214. <div>
  215. <a href=""><div class="iconfont icon-tx"></div></a>
  216. <a href="">京喜</a>
  217. </div>
  218. <div>
  219. <a href=""><div class="iconfont icon-gouwuche"></div></a>
  220. <a href="">购物车</a>
  221. </div>
  222. <div>
  223. <a href=""><div class="iconfont icon-lujingbeifen2"></div></a>
  224. <a href="">未登录</a>
  225. </div>
  226. </div>
  227. </div>
  228. </body>
  229. </html>

样式初始化代码 reset.css

  1. /* 样式初始化 */
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. /* 去掉列表的样式 */
  8. li {
  9. list-style: none;
  10. }
  11. /* 去掉链接下划线 */
  12. a {
  13. color: #7b7b7b;
  14. text-decoration: none;
  15. }
  16. /* 设置主题颜色 */
  17. body {
  18. background-color: #f6f6f6;
  19. }
  20. html {
  21. font-size: 10px;
  22. }
  23. @media screen and (min-width: 480px) {
  24. html {
  25. font-size: 12px;
  26. }
  27. }
  28. @media screen and (min-width: 640px) {
  29. html {
  30. font-size: 14px;
  31. }
  32. }
  33. @media screen and (min-width: 720px) {
  34. html {
  35. font-size: 16px;
  36. }
  37. }
  38. @media screen and (min-width: 800px) {
  39. html {
  40. font-size: 18px;
  41. }
  42. }
  43. @media screen and (min-width: 880px) {
  44. html {
  45. font-size: 18px;
  46. }
  47. }
  48. @media screen and (min-width: 960px) {
  49. html {
  50. font-size: 20px;
  51. }
  52. }
  53. @media screen and (min-width: 1040px) {
  54. html {
  55. font-size: 22px;
  56. }
  57. }
  58. @media screen and (min-width: 1120px) {
  59. html {
  60. font-size: 24px;
  61. }
  62. }
  63. @media screen and (min-width: 1200px) {
  64. html {
  65. font-size: 26px;
  66. }
  67. }
  68. @media screen and (min-width: 1280px) {
  69. html {
  70. font-size: 28px;
  71. }
  72. }

主体区代码 index.css

  1. /* 导入初始化样式表 */
  2. @import "reset.css";
  3. /* 页眉 */
  4. .header {
  5. background-color: #e43130;
  6. color: white;
  7. height: 4.4rem;
  8. z-index: 100;
  9. position: fixed;
  10. top: 0;
  11. right: 0;
  12. left: 0;
  13. }
  14. /* body {
  15. position: relative;
  16. } */
  17. /* 主体 */
  18. .main {
  19. position: absolute;
  20. top: 4.4rem;
  21. bottom: 4.4rem;
  22. left: 0;
  23. right: 0;
  24. /* background-color: rgb(58, 66, 58); */
  25. /* min-height: 100rem; */
  26. font-size: 1.4rem;
  27. }
  28. /* 页脚 */
  29. .footer {
  30. background-color: #fafafa;
  31. height: 4.4rem;
  32. color: #666;
  33. z-index: 100;
  34. box-shadow: 0 0 3px #999;
  35. position: fixed;
  36. bottom: 0;
  37. right: 0;
  38. left: 0;
  39. font-size: 1.4rem;
  40. }

页眉区的代码 header.css

  1. .header {
  2. display: flex;
  3. align-items: center;
  4. }
  5. .header a:first-of-type .menu {
  6. font-size: 2.5rem;
  7. flex: 1;
  8. color: white;
  9. /* 设置文本居中 */
  10. text-align: center;
  11. }
  12. /* 搜索框 */
  13. .header .search {
  14. flex: 6;
  15. background-color: white;
  16. padding: 0.5rem;
  17. border-radius: 3rem;
  18. display: flex;
  19. align-items: center;
  20. }
  21. .header .search .logo {
  22. color: #e43130;
  23. flex: 0 1 4rem;
  24. /* margin-left: 1rem; */
  25. /* justify-content: flex-end; */
  26. /* 水平垂直居中 */
  27. text-align: center;
  28. line-height: 2rem;
  29. font-size: 1.5rem;
  30. }
  31. /* 放大镜 */
  32. .header .search .zoom {
  33. font-size: 1.5rem;
  34. color: #ccc;
  35. flex: 0 1 4rem;
  36. border-left: 1px solid;
  37. text-align: center;
  38. line-height: 2rem;
  39. }
  40. /* 搜索框 */
  41. .header .search .words {
  42. font-size: 1.5rem;
  43. flex: auto;
  44. outline: none;
  45. border: none;
  46. color: #aaa;
  47. }
  48. .header .login {
  49. font-family: Helvetica;
  50. font-size: 1.5rem;
  51. flex: 1;
  52. color: white;
  53. text-align: center;
  54. }

幻灯片部分代码 slide.css(这个没有 js 处理,只能先做成静态的):

  1. /* 幻灯片,静态 */
  2. .main .solide {
  3. background-color: white;
  4. min-height: 14rem;
  5. }
  6. .main .slide .box {
  7. height: 11rem;
  8. background-color: #e43130;
  9. border-bottom-left-radius: 5rem;
  10. border-bottom-right-radius: 5rem;
  11. z-index: 1;
  12. }
  13. .main .slide a {
  14. position: absolute;
  15. left: 50%;
  16. top: 0;
  17. transform: translateX(-50%);
  18. }
  19. .main .slide img {
  20. /* width: 35rem; */
  21. height: 14rem;
  22. border-radius: 1rem;
  23. }
  24. /* 跳转栏 */
  25. .jump {
  26. height: 3rem;
  27. display: flex;
  28. background-color: #e43130;
  29. padding: 0.2rem;
  30. z-index: 3;
  31. position: fixed;
  32. right: 0;
  33. top: 5rem;
  34. border-top-left-radius: 2.5rem;
  35. border-bottom-left-radius: 2.5rem;
  36. justify-content: space-around;
  37. /* opacity: 0.8; */
  38. }
  39. .jump a:first-of-type {
  40. line-height: 2.5rem;
  41. }
  42. .jump .jd {
  43. width: 2.5rem;
  44. height: 2.5rem;
  45. font-size: 2.5rem;
  46. background-color: white;
  47. /* border: 1px solid #fff; */
  48. border-radius: 50%;
  49. color: #e43130;
  50. margin-right: 0.5rem;
  51. }
  52. .jump a:last-of-type {
  53. color: white;
  54. font-size: 1.5rem;
  55. line-height: 3rem;
  56. }

主导航区代码 nav.css:

  1. /* 主导航区 */
  2. .main .nav {
  3. /* background-color: white; */
  4. display: flex;
  5. flex-flow: row wrap;
  6. position: absolute;
  7. top: 14rem;
  8. justify-content: space-between;
  9. }
  10. .main .nav img {
  11. height: 4rem;
  12. width: 4rem;
  13. }
  14. .main .nav li {
  15. /* 每一行显示五个 */
  16. flex: 1 1 20%;
  17. display: flex;
  18. flex-flow: column nowrap;
  19. align-items: center;
  20. }

正品直邮区代码 mail.css:

  1. .main .zp {
  2. display: flex;
  3. background-color: white;
  4. position: absolute;
  5. top: 29rem;
  6. left: 0;
  7. right: 0;
  8. padding: 1rem;
  9. margin: 1rem;
  10. justify-content: space-between;
  11. align-items: flex-end;
  12. }
  13. .main .zp img {
  14. height: 6rem;
  15. width: 6rem;
  16. }
  17. .main .zp li {
  18. display: flex;
  19. flex: 1 1 25%;
  20. flex-flow: column nowrap;
  21. align-items: flex-start;
  22. }
  23. .main .zip li a {
  24. color: #000;
  25. }
  26. .main .zp li:nth-of-type(2) a:first-of-type,
  27. .main .zp li:nth-of-type(3) a:first-of-type {
  28. font-weight: bold;
  29. font-size: 1.8rem;
  30. color: #000;
  31. }

秒杀区代码 ms.css:

  1. /* 秒杀区 */
  2. .main .ms {
  3. background-color: white;
  4. position: absolute;
  5. top: 43rem;
  6. padding: 1rem;
  7. margin: 1rem;
  8. right: 0;
  9. left: 0;
  10. border-radius: 1rem;
  11. }
  12. .main .ms .ms-top {
  13. font-size: 1.3rem;
  14. height: 3rem;
  15. display: flex;
  16. justify-content: space-between;
  17. }
  18. .main .ms .ms-top .left {
  19. display: flex;
  20. }
  21. .main .ms .ms-top .left .notice {
  22. font-size: 1.1rem;
  23. height: 2rem;
  24. border: 1px solid #e43130;
  25. border-radius: 2rem;
  26. margin-left: 1rem;
  27. display: flex;
  28. justify-content: center;
  29. align-items: center;
  30. }
  31. .main .ms .ms-top .left .notice .tips {
  32. background-color: #e43130;
  33. color: white;
  34. border-radius: 2rem;
  35. padding: 0.3rem 0.5rem;
  36. }
  37. .main .ms .ms-top .left .notice .time {
  38. padding: 0.3rem 0.5rem;
  39. }
  40. .main .ms .ms-top .right {
  41. color: #e43130;
  42. }
  43. /* 主体区 */
  44. .main .ms .ms-body {
  45. display: flex;
  46. justify-content: space-between;
  47. }
  48. .main .ms .ms-body img {
  49. height: 5.5rem;
  50. width: 5.5rem;
  51. }
  52. /* 秒杀价 */
  53. .main .ms .ms-body .item div:first-of-type {
  54. color: #e43130;
  55. font-weight: bold;
  56. }
  57. /* 原价 */
  58. .main .ms .ms-body .item div:last-of-type {
  59. text-decoration: line-through;
  60. color: #666666;
  61. text-decoration-color: #666;
  62. }

推荐喜欢区的代码 xh.css:

  1. .main h2 {
  2. position: absolute;
  3. top: 58rem;
  4. left: 0;
  5. right: 0;
  6. text-align: center;
  7. line-height: 4rem;
  8. margin: 2rem 0;
  9. font-weight: 500;
  10. }
  11. .main .xh {
  12. position: absolute;
  13. font-size: 1rem;
  14. display: flex;
  15. flex-flow: row wrap;
  16. top: 64rem;
  17. left: 0;
  18. right: 0;
  19. }
  20. .main .xh .item {
  21. flex: 1 1 calc(50% - 3rem);
  22. background-color: white;
  23. border-radius: 1rem;
  24. overflow: hidden;
  25. display: flex;
  26. flex-flow: column nowrap;
  27. padding-bottom: 1rem;
  28. /* text-overflow: hidden; */
  29. }
  30. .main .xh .item:nth-of-type(odd) {
  31. margin-left: 1rem;
  32. margin-bottom: 1rem;
  33. }
  34. .main .xh .item:nth-of-type(even) {
  35. margin: 0 1rem;
  36. margin-bottom: 1rem;
  37. }
  38. .main .xh .item img {
  39. width: 100%;
  40. height: 100%;
  41. padding: 1rem;
  42. }
  43. .main .xh .item p {
  44. font-size: 1.3rem;
  45. height: 3rem;
  46. overflow: hidden;
  47. padding: 1rem;
  48. }
  49. .main .xh .item .price {
  50. height: 3rem;
  51. display: flex;
  52. justify-content: space-between;
  53. align-items: center;
  54. }
  55. .main .xh .item .price div:first-of-type {
  56. color: #e43130;
  57. }
  58. .main .xh .item .price div:last-of-type {
  59. height: 2rem;
  60. color: #666;
  61. background-color: #ddd;
  62. border-radius: 1rem;
  63. padding: 0.2rem 1rem;
  64. }
  65. .main .xh .item div:last-of-type {
  66. height: 1rem;
  67. }

页脚区代码 footer.css:

  1. .footer {
  2. display: flex;
  3. justify-content: space-around;
  4. }
  5. .footer > div {
  6. display: flex;
  7. flex-flow: column nowrap;
  8. align-items: center;
  9. justify-content: flex-end;
  10. }
  11. .footer div a:first-of-type div {
  12. font-size: 1.5rem;
  13. }

最后的显示效果如下:
显示效果 1:

显示效果 2:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议