博客列表 >用flex弹性盒子仿一个淘宝手机端练习

用flex弹性盒子仿一个淘宝手机端练习

emagic
emagic原创
2020年06月28日 17:16:231257浏览

0624作业

仿一个网站(就拿手机淘宝练习一下,对标业界标杆)

源网站图片

  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>flex弹性盒子布局仿站(淘宝)</title>
  7. <link rel="stylesheet" href="static/css/iconfont.css" />
  8. <link rel="stylesheet" href="simulator.css" />
  9. </head>
  10. <body>
  11. <!-- 页眉 -->
  12. <header>
  13. <a href=""><img src="static/images/logo.png" alt="" /></a>
  14. <input type="text" placeholder="寻找店铺宝贝" value="" />
  15. <a href=""><span class="iconfont"></span></a>
  16. <a href=""><span class="iconfont"></span></a>
  17. </header>
  18. <!-- 轮播广告图 -->
  19. <section class="slider">
  20. <img src="static/images/banner.jpg" alt="" />
  21. </section>
  22. <!-- 导航区 -->
  23. <nav>
  24. <div>
  25. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  26. <a href=""><img src="static/images/nav1.png" alt="" /></a>
  27. <a href="">天狗新品</a>
  28. </div>
  29. <div>
  30. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  31. <a href=""><img src="static/images/nav2.png" alt="" /></a>
  32. <a href="">今日爆款</a>
  33. </div>
  34. <div>
  35. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  36. <a href=""><img src="static/images/nav3.png" alt="" /></a>
  37. <a href="">天狗国际</a>
  38. </div>
  39. <div>
  40. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  41. <a href=""><img src="static/images/nav4.png" alt="" /></a>
  42. <a href="">流口水么</a>
  43. </div>
  44. <div>
  45. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  46. <a href=""><img src="static/images/nav5.png" alt="" /></a>
  47. <a href="">天狗超市</a>
  48. </div>
  49. <div>
  50. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  51. <a href=""><img src="static/images/nav6.png" alt="" /></a>
  52. <a href="">充值中心</a>
  53. </div>
  54. <div>
  55. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  56. <a href=""><img src="static/images/nav7.png" alt="" /></a>
  57. <a href="">机票酒店</a>
  58. </div>
  59. <div>
  60. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  61. <a href=""><img src="static/images/nav8.png" alt="" /></a>
  62. <a href="">金币矿场</a>
  63. </div>
  64. <div>
  65. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  66. <a href=""><img src="static/images/nav9.png" alt="" /></a>
  67. <a href="">阿狗拍卖</a>
  68. </div>
  69. <div>
  70. <!-- 注意,图片是可以点击的,所以要放到a标签里面去 -->
  71. <a href=""><img src="static/images/nav10.png" alt="" /></a>
  72. <a href="">吃货品尝</a>
  73. </div>
  74. </nav>
  75. <!-- 火爆商品区域 -->
  76. <div class="hot-goods">
  77. <div class="goods-img">
  78. <h2>
  79. 聚划算
  80. </h2>
  81. <div class="list-goods">
  82. <div>
  83. <a href="">品牌折扣</a>
  84. <a href=""><img src="static/images/sec1.webp" alt="" /></a>
  85. </div>
  86. <div>
  87. <a href="">去抢福利</a>
  88. <a href=""><img src="static/images/sec2.webp" alt="" /></a>
  89. </div>
  90. </div>
  91. </div>
  92. <div class="goods-img">
  93. <h2>
  94. 有好货
  95. </h2>
  96. <div class="list-goods">
  97. <div>
  98. <a>全民口碑推荐</a>
  99. <a href=""><img src="static/images/sec3.webp" alt="" /></a>
  100. </div>
  101. <div>
  102. <a href=""> </a>
  103. <a href=""><img src="static/images/sec4.webp" alt="" /></a>
  104. </div>
  105. </div>
  106. </div>
  107. <div class="goods-img">
  108. <h2>
  109. 淘抢购
  110. </h2>
  111. <div class="list-goods">
  112. <div>
  113. <a href="">限时半价</a>
  114. <a href=""><img src="static/images/sec5.webp" alt="" /></a>
  115. </div>
  116. <div>
  117. <a href="">天天特卖</a>
  118. <a href=""><img src="static/images/sec6.webp" alt="" /></a>
  119. </div>
  120. </div>
  121. </div>
  122. <div class="goods-img">
  123. <h2>
  124. 狗狗直播
  125. </h2>
  126. <div class="list-goods">
  127. <div>
  128. <a href="">夏日满减</a>
  129. <a href=""><img src="static/images/sec7.webp" alt="" /></a>
  130. </div>
  131. <div>
  132. <a href=""> </a>
  133. <a href=""><img src="static/images/sec8.webp" alt="" /></a>
  134. </div>
  135. </div>
  136. </div>
  137. </div>
  138. <!-- 猜你喜欢 -->
  139. <h2 class="iconfont maybelike">
  140.  你可能还喜欢 
  141. </h2>
  142. <div class="list-goods">
  143. <div class="goods-desc">
  144. <a href=""><img src="static/images/goods1.jpg_.webp" alt="" /></a>
  145. <a href=""
  146. >实木相框挂墙
  147. <span class="iconfont hotboom" style="vertical-align: middle;"
  148. ></span
  149. >
  150. </a>
  151. </div>
  152. <div class="goods-desc">
  153. <a href=""><img src="static/images/goods2.jpg_.webp" alt="" /></a>
  154. <a href=""
  155. >啥地方感叹号挂墙
  156. <span class="iconfont hotboom" style="vertical-align: middle;"
  157. ></span
  158. >
  159. </a>
  160. </div>
  161. <div class="goods-desc">
  162. <a href=""><img src="static/images/goods3.jpg_.webp" alt="" /></a>
  163. <a href=""
  164. >布艺沙发<span
  165. class="iconfont hotboom"
  166. style="vertical-align: middle;"
  167. ></span
  168. >
  169. </a>
  170. </div>
  171. <div class="goods-desc">
  172. <a href=""><img src="static/images/goods4.jpg_.webp" alt="" /></a>
  173. <a href=""
  174. >家具饰品<span
  175. class="iconfont hotboom"
  176. style="vertical-align: middle;"
  177. ></span
  178. >
  179. </a>
  180. </div>
  181. </div>
  182. <div class="apptop">
  183. <!-- app和返回顶部 -->
  184. <span class="iconfont"></span>
  185. <span class="iconfont"></span>
  186. </div>
  187. <!-- 页脚 -->
  188. <footer>
  189. <a href="">
  190. <!-- 因为图片是可以点进去的,所以要放入a标签内 -->
  191. <span class="iconfont hotboom"></span>
  192. <span>首页</span>
  193. </a>
  194. <a href="">
  195. <span class="iconfont hotboom"></span>
  196. <span>购物车</span>
  197. </a>
  198. <a href="">
  199. <span class="iconfont hotboom"></span>
  200. <span>消息</span>
  201. </a>
  202. <a href="">
  203. <span class="iconfont hotboom"></span>
  204. <span>订单列表</span>
  205. </a>
  206. <a href="">
  207. <span class="iconfont hotboom"></span>
  208. <span>我的淘宝</span>
  209. </a>
  210. </footer>
  211. </body>
  212. </html>
  1. /* 样式初始化三板斧 */
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. box-sizing: border-box;
  6. }
  7. /* a链接初始化双雄 */
  8. a {
  9. text-decoration: none;
  10. color: #666;
  11. }
  12. /* html中要设置宽度自适应视口 */
  13. html {
  14. /* vw:可视区宽度,100指的是100份 ,vh是可视区的高度*/
  15. width: 100vw;
  16. height: 100vh;
  17. font-size: 15px;
  18. color: #666;
  19. }
  20. body {
  21. display: flex;
  22. flex-flow: column nowrap;
  23. min-width: 380px;
  24. }
  25. body > header {
  26. /* 文本设置为白色 */
  27. color: white;
  28. background-color: orange;
  29. height: 30px;
  30. display: flex;
  31. align-items: center;
  32. justify-content: space-between;
  33. /* 头部滚动条滚动的时候不要动 */
  34. position: fixed;
  35. /* 注意,固定定位后会缩回去,所以要设置一下宽度 */
  36. width: 100vw;
  37. padding: 10px;
  38. }
  39. body > header > a:first-of-type {
  40. display: flex;
  41. justify-content: center;
  42. }
  43. body > header > input {
  44. border-radius: 5px;
  45. margin: 0 2px;
  46. cursor: pointer;
  47. opacity: 0.8;
  48. }
  49. body > header span {
  50. color: orangered;
  51. }
  52. .slider {
  53. height: 180px;
  54. }
  55. section > img {
  56. width: 100vw;
  57. padding: 20px 0px;
  58. }
  59. /* 主导航区 */
  60. nav {
  61. height: 150px;
  62. /* margin-top: 3px; */
  63. display: flex;
  64. /* 允许换行,转为多行容器 */
  65. flex-flow: row wrap;
  66. align-content: space-around;
  67. }
  68. nav div {
  69. /* 每排5个,一个就是20% */
  70. width: 20vw;
  71. display: flex;
  72. flex-flow: column nowrap;
  73. align-items: center;
  74. }
  75. /* 图片是第一个a标签 */
  76. nav > div > a:first-of-type {
  77. text-align: center;
  78. }
  79. nav > div > a:last-of-type {
  80. text-align: center;
  81. font-size: 0.8rem;
  82. }
  83. nav > div img {
  84. width: 75%;
  85. }
  86. /* 火爆商品区 */
  87. .hot-goods {
  88. border: 2px solid lightgrey;
  89. border-radius: 10px;
  90. box-sizing: border-box;
  91. /* border: radius 10px; */
  92. margin: 3px 5px;
  93. font-size: 0.8rem;
  94. display: flex;
  95. /* 水平多行容器 */
  96. flex-flow: row wrap;
  97. align-content: center;
  98. padding: 10px;
  99. }
  100. .hot-goods .goods-img {
  101. /* 每排2组,一个就是不到50% */
  102. width: 45vw;
  103. display: flex;
  104. flex-flow: row wrap;
  105. align-items: center;
  106. /* padding: 15px; */
  107. align-content: space-around;
  108. }
  109. .hot-goods > .goods-img > .list-goods {
  110. display: flex;
  111. flex-flow: row nowrap;
  112. }
  113. .goods-img > .list-goods > div > a > img {
  114. width: 20vw;
  115. }
  116. /* 列表中每个项目的样式,加些间距 */
  117. .goods-img > .list-goods > div > a {
  118. padding: 5px;
  119. box-sizing: border-box;
  120. }
  121. .goods-img > .list-goods > div > a:first-of-type {
  122. color: red;
  123. font-size: 0.8rem;
  124. }
  125. /* .list-goods > div {
  126. display: flex;
  127. flex-flow: column nowrap;
  128. } */
  129. /* 你可能还喜欢 */
  130. .maybelike {
  131. color: coral;
  132. text-align: center;
  133. }
  134. /* 每个项目也转为flex */
  135. .list-goods > .goods-desc {
  136. margin: 10px 0;
  137. display: flex;
  138. }
  139. .list-goods .goods-desc {
  140. width: 35vw;
  141. display: flex;
  142. flex-flow: row wrap;
  143. align-items: center;
  144. align-content: space-between;
  145. }
  146. .list-goods > .goods-desc img {
  147. width: 100%;
  148. }
  149. /* 列表中每个项目的样式,加些间距 */
  150. .list-goods > .goods-desc > a {
  151. padding: 10px;
  152. box-sizing: border-box;
  153. }
  154. .list-goods > .goods-desc > a:last-of-type {
  155. padding: 10px;
  156. box-sizing: border-box;
  157. color: #777;
  158. }
  159. .apptop {
  160. position: fixed;
  161. height: 70px;
  162. width: 35px;
  163. bottom: 150px;
  164. right: 15px;
  165. color: coral;
  166. }
  167. .apptop > span {
  168. font-size: 1.8rem;
  169. }
  170. body > footer {
  171. color: #666;
  172. background-color: #efefef;
  173. border-top: 1px solid #ccc;
  174. height: 60px;
  175. /* 页脚要固定显示,否则滚动下来就不见了 */
  176. position: fixed;
  177. bottom: 0;
  178. width: 100vw;
  179. display: flex;
  180. justify-content: space-around;
  181. /* 这里用space-evently也行 */
  182. }
  183. body > footer a {
  184. margin-top: 5px;
  185. /* font-size: 0.8rem; */
  186. display: flex;
  187. /* 垂直排列不换行 */
  188. flex-flow: column nowrap;
  189. /* 交叉轴项目居中显示 */
  190. align-items: center;
  191. }
  192. body > footer a > span:first-of-type {
  193. font-size: 1.5rem;
  194. }

效果图

小结

弹性盒子flex的嵌套要灵活掌握,另外就是主轴方向是垂直还是水平,分别对应着不同的效果和分布参数,要多练习,另外部分效果还没有仿出来

比如交界处的弧形效果暂时没搞明白,后续继续仿

如:

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