博客列表 >JS实现购物车自动结算和背景切换

JS实现购物车自动结算和背景切换

leverWang
leverWang原创
2020年08月25日 16:49:39816浏览
背景切换

change-bg.js

  1. var bg_item = document.querySelectorAll(".bg-items>.item");
  2. bg_item.forEach(function (item) {
  3. item.addEventListener("click", change_bg, false);
  4. });
  5. function change_bg(e) {
  6. url = e.target.src;
  7. document.body.style.cssText ="background: url(" + url + ") no-repeat center top;";
  8. }
购物车自动结算

select-all.js

  1. /** @format */
  2. var selectAllBtn = document.querySelector("#check-all");
  3. //获取每个选项的复选框
  4. var itemBtns = document.querySelectorAll("input[name=itemId]");
  5. //获取所有单价
  6. prices = document.querySelectorAll("table>tbody>tr>td:nth-of-type(5)");
  7. //获取所有商品的数量
  8. numbers = document.querySelectorAll("table>tbody>tr>td>input[type=number]");
  9. // 获取商品单价数组
  10. var unitprice_array = getArrar(prices, "innerText");
  11. // 获取商品数量数组
  12. var counts = getArrar(numbers, "value");
  13. //初始化 默认为全选状态
  14. init();
  15. function init() {
  16. selectAllBtn.checked = true;
  17. checkAll();
  18. }
  19. //监听全选按钮状态
  20. selectAllBtn.addEventListener("change", checkAll, false);
  21. //添加状态刷新,当全选按钮状态改变时重新渲染页面
  22. selectAllBtn.addEventListener("change", refresh, false);
  23. function refresh() {
  24. autoCalculate();
  25. }
  26. //设置复选框的取消和选中状态并添加默认值
  27. function checkAll() {
  28. itemBtns.forEach(function (btn) {
  29. btn.checked = selectAllBtn.checked;
  30. if (!selectAllBtn.checked) {
  31. btn.parentNode.parentNode.children[5].children.counter.value = 0;
  32. } else {
  33. btn.parentNode.parentNode.children[5].children.counter.value = 1;
  34. }
  35. });
  36. }
  37. itemBtns.forEach(function (btn) {
  38. btn.addEventListener("change", checkAllStatus, false);
  39. });
  40. function checkAllStatus() {
  41. //获取兄弟节点value值,实现取消选中自动重新计算
  42. item_num = this.parentNode.parentNode.querySelector("td>input[type=number]").value;
  43. if (this.checked == false && item_num > 0) {
  44. this.parentNode.parentNode.querySelector("td>input[type=number]").value = 0;
  45. } else {
  46. this.parentNode.parentNode.querySelector("td>input[type=number]").value = 1;
  47. }
  48. changeStatus();
  49. autoCalculate();
  50. }
  51. //设置复选框状态
  52. function changeStatus() {
  53. var num = 0;
  54. itemBtns.forEach(function (btn) {
  55. if (btn.checked == true) {
  56. num++;
  57. }
  58. });
  59. // console.log(num);
  60. selectAllBtn.checked = itemBtns.length == num;
  61. }
  62. //将传入伪数组转换为新的数组
  63. function getArrar(array, attr_name) {
  64. return Array.from(array).map((item) => parseInt(item[attr_name]));
  65. }
  66. //每个项目的金额
  67. var amounts = getAmounts(unitprice_array, counts);
  68. //将每个项目的金额渲染到表格中
  69. let subtotal = document.querySelectorAll("tbody>tr>td:last-of-type");
  70. //forEach的第二个参数为当前item的索引
  71. subtotal.forEach((item, index) => (item.innerText = amounts[index]));
  72. //计算每个项目的金额,返回一个新的数组
  73. function getAmounts(unitprice_array, counts) {
  74. let res = [];
  75. for (var i = 0; i < counts.length; i++) {
  76. let amount = unitprice_array[i] * counts[i];
  77. res.push(amount);
  78. }
  79. return res;
  80. }
  81. //计算总金额并渲染到页面
  82. var totalNum = (document.querySelector("#total-num").innerText = getSum(counts));
  83. var totalAmount = (document.querySelector("#total-amount").innerText = getSum(amounts));
  84. //使用reduce方法计算数组每项的和
  85. function getSum(array) {
  86. return array.reduce((total, num) => total + num, 0);
  87. }
  88. //为数量控件添加change事件
  89. numbers.forEach((item) => item.addEventListener("input", autoCalculate));
  90. numbers.forEach((item) => item.addEventListener("input", autoSelect));
  91. //当input[type=number]值改变时设置复选框选中,重新计算金额
  92. function autoSelect() {
  93. this.parentNode.parentNode.querySelector(
  94. "td>input[name=itemId]"
  95. ).checked = true;
  96. // 重新计算所有被选中复选框的数量
  97. changeStatus();
  98. }
  99. //自动计算函数
  100. function autoCalculate() {
  101. counts = getArrar(numbers, "value");
  102. amounts = getAmounts(unitprice_array, counts);
  103. subtotal.forEach((item, index) => (item.innerText = amounts[index]));
  104. totalNum = document.querySelector("#total-num").innerText = getSum(counts);
  105. totalAmount = document.querySelector("#total-amount").innerText = getSum(amounts);
  106. }

index.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. <title>购物车全选</title>
  7. <link rel="stylesheet" href="css/check-all.css" />
  8. </head>
  9. <body>
  10. <div class="bg-items">
  11. <div class='item'> <img src="img/1.jpg" alt=""></div>
  12. <div class='item'><img src="img/2.jpg" alt=""></div>
  13. <div class='item'> <img src="img/3.jpg" alt=""></div>
  14. </div>
  15. <table>
  16. <caption>
  17. 购物车
  18. </caption>
  19. <thead>
  20. <tr>
  21. <th>
  22. <input type="checkbox" name="checkAll" id="check-all" /><label
  23. for="check-all">全选</label>
  24. </th>
  25. <th>ID</th>
  26. <th>品名</th>
  27. <th>单位</th>
  28. <th>单价/元</th>
  29. <th>数量</th>
  30. <th>金额/元</th>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. <tr>
  35. <td>
  36. <input type="checkbox" name="itemId" value="SN-1010" />
  37. </td>
  38. <td>SN-1010</td>
  39. <td>MacBook Pro电脑</td>
  40. <td></td>
  41. <td>18999</td>
  42. <td>
  43. <input type="number" name="counter" value="1" min="1" step="1" />
  44. </td>
  45. <td></td>
  46. </tr>
  47. <tr>
  48. <td>
  49. <input type="checkbox" name="itemId" value="SN-1020" />
  50. </td>
  51. <td>SN-1020</td>
  52. <td>iPhone手机</td>
  53. <td></td>
  54. <td>4999</td>
  55. <td>
  56. <input type="number" name="counter" value="2" min="1" step="1" />
  57. </td>
  58. <td></td>
  59. </tr>
  60. <tr>
  61. <td>
  62. <input type="checkbox" name="itemId" value="SN-1030" />
  63. </td>
  64. <td>SN-1030</td>
  65. <td>智能AI音箱</td>
  66. <td></td>
  67. <td>399</td>
  68. <td>
  69. <input type="number" name="counter" value="3" min="1" step="1" />
  70. </td>
  71. <td></td>
  72. </tr>
  73. <tr>
  74. <td>
  75. <input type="checkbox" name="itemId" value="SN-1040" />
  76. </td>
  77. <td>SN-1040</td>
  78. <td>SSD移动硬盘</td>
  79. <td></td>
  80. <td>888</td>
  81. <td>
  82. <input type="number" name="counter" value="4" min="1" step="1" />
  83. </td>
  84. <td></td>
  85. </tr>
  86. <tr>
  87. <td>
  88. <input type="checkbox" name="itemId" value="SN-1050" />
  89. </td>
  90. <td>SN-1050</td>
  91. <td>黄山毛峰</td>
  92. <td></td>
  93. <td>999</td>
  94. <td>
  95. <input type="number" name="counter" value="5" min="1" step="1" />
  96. </td>
  97. <td></td>
  98. </tr>
  99. </tbody>
  100. <tfoot>
  101. <tr>
  102. <td colspan="5">总计:</td>
  103. <td id="total-num"></td>
  104. <td id="total-amount"></td>
  105. </tr>
  106. <tr>
  107. <td colspan="7" style="border:none;">
  108. <div style=" margin: 10px auto;">
  109. <button style="float: right; width: 100px;">结算</button>
  110. </div>
  111. </td>
  112. </tr>
  113. </tfoot>
  114. </table>
  115. <script src="js/change-bg.js"></script>
  116. <script src="js/select-all.js"></script>
  117. </body>
  118. </html>
总结:通过这个小练习,进一步熟悉了前面的知识,尝试使用了一下箭头函数来简化代码,在写JS的时候发现思路很重要,只要思路清晰,只需要使用对应的JS来一步步实现。
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议