博客列表 >第十期商城购物车和结算页面实战(2019-12-31)

第十期商城购物车和结算页面实战(2019-12-31)

齐齐
齐齐原创
2020年01月04日 16:11:37865浏览

1.商城购物车

作业要求:商城购物车部分页面中包括全选,单选按钮,并选中的状态。数量按钮可以增加和减少,加入购物车的产品为多个。底部需要有结算按钮,数量和总金额。

1.1 对页面结构进行分析,总体分为五部分。用一个容器(content)将全部内容包裹,方便水平居中显示。

1.2 整个购物车的布局技术采用flex,先设置主轴方向为列。然后再对每一个部分进行下一步的处理。
1.3第一部分比较简单,为一张图片。第二部分,转成flex容器的中的项目,在水平方向两端对齐。第三部分,flex容器水平方向分散对齐。
1.4第四部分,设为flex容器后,修改主轴改为列方向。再对每一个产品详情进行设置,也转为flex容器,方便设置每一个行内块的宽度。再用相对定位技术,让每一小块对齐相应的信息栏。

1.5最后一部分,现将容器转为flex,水平方向靠右对齐,垂直方向居中对齐。然后再对每一个小细节,进行相应的css设置。

html代码部分

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>购物车</title>
  6. <link rel="stylesheet" href="shop_cart.css">
  7. </head>
  8. <body>
  9. <!--导入头部组件-->
  10. <div class="public-head">
  11. <a href="">网站首页</a>
  12. <a href="">专题</a>
  13. <a href="">网站导航</a>
  14. <a href="">二手商品</a>
  15. <a href="">讨论区</a>
  16. <span>
  17. <a href=""><i class="iconfont icon-huiyuan2"></i> 登陆</a>
  18. <a href="">免费注册</a>
  19. </span>
  20. </div>
  21. <!--购物车-->
  22. <div class="shop-cart">
  23. <div class="content">
  24. <div class="cart-logo"><img src="../../static/images/logo.jpg" alt=""></div>
  25. <div class="cart-num">
  26. <span>全部商品 3</span>
  27. <div class="cart-address">
  28. <span>配送至:</span>
  29. <select name="cart-address">
  30. <option value="0">重庆xx号街道</option>
  31. <option value="1">沿河东路南段文星路314号</option>
  32. <option value="0">重庆市渝中区邹容路100号重庆时代广场B2</option>
  33. <option value="1" selected>重庆渝中区大溪沟人和街华福巷大厦1F(靠大礼堂300米)</option>
  34. </select>
  35. </div>
  36. </div>
  37. <div class="cart-top">
  38. <span><input type="checkbox" id="all" checked><label for="all">全选</label></span>
  39. <span>商品信息</span>
  40. <span>单价</span>
  41. <span>数量</span>
  42. <span>金额</span>
  43. <span>操作</span>
  44. </div>
  45. <div class="cart-list">
  46. <div class="cart-infos">
  47. <span><input type="checkbox" name="select" checked></span>
  48. <span class="pic-infos">
  49. <a href=""><img src="../../static/images/lbx.jpg" alt=""></a>
  50. <a href="">买2送1七天量】黄金搭档中老年型多种复合维生素片年货礼盒保健品</a>
  51. </span>
  52. <span>¥288.00</span>
  53. <span><input type="number" name="conunt" value="1" min="1"/></span>
  54. <span>¥288.00</span>
  55. <span>
  56. <a href="">删除</a>
  57. </span>
  58. </div>
  59. <div class="cart-infos">
  60. <span><input type="checkbox" name="select" checked></span>
  61. <span class="pic-infos">
  62. <a href=""><img src="../../static/images/bx.jpg" alt=""></a>
  63. <a href="">海尔出品Leader/统帅 BC/BD-427TD 427升大容量商用冷藏冷冻冰柜</a>
  64. </span>
  65. <span>¥2299.00</span>
  66. <span><input type="number" name="conunt" value="1" min="1"/></span>
  67. <span>¥2299.00</span>
  68. <span>
  69. <a href="">删除</a>
  70. </span>
  71. </div>
  72. <div class="cart-infos">
  73. <span><input type="checkbox" name="select" checked></span>
  74. <span class="pic-infos">
  75. <a href=""><img src="../../static/images/bx1.jpg" alt=""></a>
  76. <a href="">海尔出品Leader/统帅 BCD-335WLDPC法式多门风冷无霜节能冰箱家用</a>
  77. </span>
  78. <span>¥3099.00</span>
  79. <span><input type="number" name="conunt" value="1" min="1"/></span>
  80. <span>¥3099.00</span>
  81. <span>
  82. <a href="">删除</a>
  83. </span>
  84. </div>
  85. </div>
  86. <div class="cart-bottom">
  87. <span>数量:3</span>
  88. <span>合计:<i>¥5686</i></span>
  89. <button>去结算</button>
  90. </div>
  91. </div>
  92. <!--导入底部组件-->
  93. <footer class="public-footer">
  94. <div>
  95. <a href="">简介</a>
  96. <a href="">联系我们</a>
  97. <a href="">招聘信息</a>
  98. <a href="">友情链接</a>
  99. <a href="">用户服务协议</a>
  100. <a href="">隐私权声明</a>
  101. <a href="">法律投诉声明</a>
  102. </div>
  103. <div><span>LOGO</span></div>
  104. <div>
  105. <p>2019 fengniao.com. All rights reserved . 安徽闹着玩有限公司(无聊网)版权所有</p>
  106. <p>皖ICP证150110号 京ICP备14323013号-2 皖公网安备110108024357788号</p>
  107. <p>违法和不良信息举报电话: 0551-1234567 举报邮箱: admin@baidu.com</p>
  108. </div>
  109. <div>
  110. <p>关注公众号</p>
  111. <img src="../../static/images/erwei-code.png" alt="">
  112. </div>
  113. </footer>
  114. </body>
  115. </html>

css部分

  1. @import "../public_reset.css";
  2. @import "../public_head/public_head.css";
  3. @import "../public_footer/public_footer.css";
  4. .shop-cart>.content{
  5. /*设置主体为1200px,居中对齐,背景为白色*/
  6. width: 1200px;
  7. margin: 20px auto;
  8. padding:20px;
  9. background: white;
  10. }
  11. .shop-cart>.content>.cart-num{
  12. display: flex;
  13. justify-content: space-between;
  14. margin: 20px 0;
  15. border-bottom: 2px solid #e6e6e6;
  16. line-height: 50px;
  17. }
  18. .shop-cart>.content>.cart-num>span{
  19. font-size: 16px;
  20. color: #f60;
  21. }
  22. .shop-cart>.content>.cart-top{
  23. display: flex;
  24. justify-content: space-around;
  25. height: 40px;
  26. align-items: center;
  27. }
  28. .shop-cart>.content>.cart-list{
  29. margin: 30px 0;
  30. }
  31. .shop-cart>.content>.cart-list>.cart-infos{
  32. display: flex;
  33. justify-content: space-around;
  34. align-items: center;
  35. height: 100px;
  36. }
  37. .shop-cart>.content>.cart-list>.cart-infos:hover{
  38. background:wheat;
  39. }
  40. .shop-cart>.content>.cart-list>.cart-infos>.pic-infos{
  41. width: 280px;
  42. display: flex;
  43. align-items: center;
  44. }
  45. .shop-cart>.content>.cart-list>.cart-infos>.pic-infos>a:hover{
  46. color: #f60;
  47. }
  48. .shop-cart>.content>.cart-list>.cart-infos>.pic-infos img{
  49. width: 60px;
  50. height: 60px;
  51. margin-right: 10px;
  52. }
  53. .shop-cart>.content>.cart-list>.cart-infos>:nth-of-type(4) input{
  54. width: 40px;
  55. }
  56. .shop-cart>.content>.cart-list>.cart-infos>:nth-of-type(1){
  57. position: relative;
  58. left: 20px;
  59. }
  60. .shop-cart>.content>.cart-list>.cart-infos>:nth-of-type(2){
  61. position: relative;
  62. left: -30px;
  63. }
  64. .shop-cart>.content>.cart-list>.cart-infos>:nth-of-type(3){
  65. position: relative;
  66. left: -100px;}
  67. .shop-cart>.content>.cart-list>.cart-infos>:nth-of-type(4){
  68. position: relative;
  69. left: -75px;}
  70. .shop-cart>.content>.cart-list>.cart-infos>:nth-last-of-type(2){
  71. position: relative;
  72. left: -55px;}
  73. .shop-cart>.content>.cart-list>.cart-infos>:last-of-type{
  74. position: relative;
  75. left: -22px;}
  76. .shop-cart>.content>.cart-bottom{
  77. display: flex;
  78. justify-content: end;
  79. align-items: center;
  80. }
  81. .shop-cart>.content>.cart-bottom>:not(:last-child){
  82. width:195px ;
  83. }
  84. .shop-cart>.content>.cart-bottom>span>i{
  85. color: red;
  86. font-size: 16px;
  87. font-style: normal;
  88. }
  89. .shop-cart>.content>.cart-bottom>:last-child{
  90. width: 120px;
  91. height: 40px;
  92. border:none;
  93. background: red;
  94. color: white;
  95. cursor: pointer;
  96. font-size: 16px;
  97. }
  98. .shop-cart>.content>.cart-bottom>:last-child:hover{
  99. background: #178cee;
  100. }

整体效果展示

2.结算页

作业要求:商城购物车有收货地址和提交按钮。

2.1 对防的结算页面,去掉不需要部分将其分为了七部分。也用到一个容器(content)将全部内容包裹,让整体水平居中显示。

2.2 第二部分信息地址部分,用的无序列表,让容器转为flex后,再对里面每一个地址栏在进行转为flex容器,里面的内容为垂直显示,水平项目的对齐方式为分散。

2.3 第四部分和第五部分,沿用了购物车的思想来实现。第六部分实现,让一个区块靠右对齐,再转为flex容器,让主轴方向为垂直分散对齐,交叉轴为靠右对齐。

html代码部分

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>结算页面</title>
  6. <!--导入当前组件css样式表-->
  7. <link rel="stylesheet" href="shop_settlement.css">
  8. </head>
  9. <body>
  10. <!--导入头部组件-->
  11. <div class="public-head">
  12. <a href="">网站首页</a>
  13. <a href="">专题</a>
  14. <a href="">网站导航</a>
  15. <a href="">二手商品</a>
  16. <a href="">讨论区</a>
  17. <span>
  18. <a href=""><i class="iconfont icon-huiyuan2"></i> 登陆</a>
  19. <a href="">免费注册</a>
  20. </span>
  21. </div>
  22. <div class="shop-settlenment">
  23. <div class="content">
  24. <h5>收货地址</h5>
  25. <!--收货地址-->
  26. <div class="shop-address">
  27. <ul>
  28. <li class="select">
  29. <span>重庆(张先生收)</span>
  30. <p>重庆渝中区大溪沟人和街华福巷大厦1F(靠大礼堂300米)</p>
  31. <a href="">修改</a>
  32. </li>
  33. <li>
  34. <span>四川(张先生收)</span>
  35. <p>重庆市渝中区邹容路100号重庆时代广场B2</p>
  36. </li>
  37. <li>
  38. <span>重庆(张先生收)</span>
  39. <p>重庆渝中区大溪沟人和街华福巷大厦1F(靠大礼堂300米)</p>
  40. </li>
  41. <li>
  42. <span>四川(张先生收)</span>
  43. <p>重庆市渝中区邹容路100号重庆时代广场B2</p>
  44. </li>
  45. </ul>
  46. </div>
  47. <!--订单信息-->
  48. <h5>确认订单信息</h5>
  49. <div class="cart-top">
  50. <span>商品信息</span>
  51. <span>单价</span>
  52. <span>数量</span>
  53. <span>金额</span>
  54. </div>
  55. <div class="settlenment-infos">
  56. <div class="cart-infos">
  57. <span class="pic-infos">
  58. <a href=""><img src="../../static/images/lbx.jpg" alt=""></a>
  59. <a href="">买2送1七天量】黄金搭档中老年型多种复合维生素片年货礼盒保健品</a>
  60. </span>
  61. <span>¥288.00</span>
  62. <span>1</span>
  63. <span>¥288.00</span>
  64. </div>
  65. <div class="cart-infos">
  66. <span class="pic-infos">
  67. <a href=""><img src="../../static/images/bx.jpg" alt=""></a>
  68. <a href="">海尔出品Leader/统帅 BC/BD-427TD 427升大容量商用冷藏冷冻冰柜</a>
  69. </span>
  70. <span>¥2299.00</span>
  71. <span>1</span>
  72. <span>¥2299.00</span>
  73. </div>
  74. <div class="cart-infos">
  75. <span class="pic-infos">
  76. <a href=""><img src="../../static/images/bx1.jpg" alt=""></a>
  77. <a href="">海尔出品Leader/统帅 BCD-335WLDPC法式多门风冷无霜节能冰箱家用</a>
  78. </span>
  79. <span>¥3099.00</span>
  80. <span>1</span>
  81. <span>¥3099.00</span>
  82. </div>
  83. </div>
  84. <!--用户确定信息-->
  85. <div class="shop—certain">
  86. <span>实付款:¥<i>5686.00</i></span>
  87. <span>寄送至: 重庆渝中区大溪沟人和街华福巷大厦1F(靠大礼堂300米)</span>
  88. <span>收货人:张先生 13xxxxx45219</span>
  89. </div>
  90. <button>提交订单</button>
  91. </div>
  92. <!--导入底部组件-->
  93. <footer class="public-footer">
  94. <div>
  95. <a href="">简介</a>
  96. <a href="">联系我们</a>
  97. <a href="">招聘信息</a>
  98. <a href="">友情链接</a>
  99. <a href="">用户服务协议</a>
  100. <a href="">隐私权声明</a>
  101. <a href="">法律投诉声明</a>
  102. </div>
  103. <div><span>LOGO</span></div>
  104. <div>
  105. <p>2019 fengniao.com. All rights reserved . 安徽闹着玩有限公司(无聊网)版权所有</p>
  106. <p>皖ICP证150110号 京ICP备14323013号-2 皖公网安备110108024357788号</p>
  107. <p>违法和不良信息举报电话: 0551-1234567 举报邮箱: admin@baidu.com</p>
  108. </div>
  109. <div>
  110. <p>关注公众号</p>
  111. <img src="../../static/images/erwei-code.png" alt="">
  112. </div>
  113. </footer>
  114. </div>
  115. </body>
  116. </html>

css部分

  1. /*导入样式重置文档*/
  2. @import "../public_reset.css";
  3. @import "../public_head/public_head.css";
  4. @import "../public_footer/public_footer.css";
  5. .shop-settlenment>.content{
  6. /*设置主体为1200px,居中对齐,背景为白色*/
  7. width: 1200px;
  8. margin: 20px auto;
  9. padding:20px;
  10. box-sizing: border-box;
  11. background: white;
  12. /*将容器转为flex容器*/
  13. display: flex;
  14. /*主轴方向为垂直方向*/
  15. flex-direction: column;
  16. }
  17. .shop-settlenment>.content h5{
  18. font-size: 14px;
  19. color:#333;
  20. line-height: 40px;
  21. }
  22. .shop-settlenment>.content>.shop-address>ul{
  23. /*将容器转为flex容器*/
  24. display: flex;
  25. }
  26. .shop-settlenment>.content>.shop-address>ul>li{
  27. width: 237px;
  28. height: 106px;
  29. padding: 10px 15px;
  30. margin-right: 14px;
  31. margin-bottom: 14px;
  32. box-sizing: border-box;
  33. /*设置收货地址背景图*/
  34. background-image: url("../../static/images/shop/default.png");
  35. /*将容器转为flex容器*/
  36. display: flex;
  37. flex-direction: column;
  38. justify-content: space-around;
  39. }
  40. .shop-settlenment>.content>.shop-address>ul>li:hover,.shop-settlenment>.content>.shop-address>ul>li.select{
  41. /*设置选中收货地址背景图*/
  42. background-image: url("../../static/images/shop/hover.png");
  43. }
  44. .shop-settlenment>.content>.shop-address>ul>li.select>a{
  45. color:#c97;
  46. }
  47. .shop-settlenment>.content>.shop-address>ul>li>span{
  48. /*设置底边框线*/
  49. border-bottom: 1px solid #f2f2f2;
  50. line-height: 30px;
  51. }
  52. .shop-settlenment>.content>.cart-top{
  53. display: flex;
  54. justify-content: space-around;
  55. border-bottom: 1px solid #f2f2f2;
  56. align-items: center;
  57. height: 40px;
  58. }
  59. .shop-settlenment>.content>.settlenment-infos{
  60. display: flex;
  61. /*设置主轴的方向*/
  62. flex-direction: column;
  63. justify-content: space-around;
  64. }
  65. .shop-settlenment>.content>.settlenment-infos>.cart-infos{
  66. margin-top: 20px;
  67. height: 100px;
  68. display: flex;
  69. justify-content: space-around;
  70. align-items: center;
  71. }
  72. .shop-settlenment>.content>.settlenment-infos>.cart-infos>.pic-infos{
  73. width:280px;
  74. display: flex;
  75. align-items: center;
  76. }
  77. .shop-settlenment>.content>.settlenment-infos>.cart-infos>.pic-infos>a:hover{
  78. color: #ff0000;
  79. }
  80. .shop-settlenment>.content>.settlenment-infos>.cart-infos>.pic-infos img{
  81. width: 60px;
  82. height: 60px;
  83. margin-right: 10px;
  84. }
  85. .shop-settlenment>.content>.settlenment-infos>.cart-infos>:nth-of-type(1){
  86. position: relative;
  87. left: -50px;
  88. }
  89. .shop-settlenment>.content>.settlenment-infos>.cart-infos>:nth-of-type(2){
  90. position: relative;
  91. left: -150px;
  92. }
  93. .shop-settlenment>.content>.settlenment-infos>.cart-infos>:nth-of-type(3){
  94. position: relative;
  95. left: -80px;
  96. }
  97. .shop-settlenment>.content>.settlenment-infos>.cart-infos>:nth-of-type(4){
  98. position: relative;
  99. left: -20px;
  100. }
  101. .shop-settlenment>.content>.shopcertain{
  102. /*设置确定信息的边框线*/
  103. border:1px solid #ff0036;
  104. width: 320px;
  105. padding: 10px;
  106. height: 80px;
  107. /*靠右对齐*/
  108. margin-left: auto;
  109. /*转为flex容器*/
  110. display: flex;
  111. /*主轴水平方向*/
  112. flex-direction: column;
  113. /*主轴分散对齐*/
  114. justify-content: space-around;
  115. /*交叉轴靠右对齐*/
  116. align-items: end;
  117. }
  118. .shop-settlenment>.content>.shopcertain>span>i{
  119. font-size: 26px;
  120. color:#ff0036;
  121. }
  122. .shop-settlenment>.content>button{
  123. /*去掉边框*/
  124. border: none;
  125. width: 180px;
  126. height: 40px;
  127. /*字体样式*/
  128. color:#f2f2f2;
  129. background: #ff0036;
  130. align-self: end;
  131. }
  132. .shop-settlenment>.content>button:hover{
  133. background: #f60;
  134. }

知识小结:

1.flex容器单行垂直方向对齐用align-items,多行容器用align-content,水平方向对齐只有一个justift-content属性。
2.flex虽然是一维,在细节处理非常棒。简单轻松实现了水平垂直居中的效果,是传统布局不能轻易实现的。
3.构造dom布局的时候,考虑的不是很全面。不知道怎么划分比较合适,思路上还有欠缺。例如结算页,写完发现,第一部分可以和第二部分合并成一个区块,第三也可以和第四合并在一起。什么时候用flex好,什么时候用grid,还是迷糊中。怎么构造源代码结构,最让人头疼的地方。

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