Blogger Information
Blog 20
fans 0
comment 1
visits 13097
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播图自动播放和购物车自动计算的完善
zg的php学习
Original
512 people have browsed it

轮播图自动播放和购物车自动计算的完善

这是课后作业,完整代码太长了,所以只贴作业实现部分。

轮播图自动播放

HTML 代码:

  1. <body>
  2. <div class="container">
  3. <!-- 1. 图片组 -->
  4. <div class="img-group"></div>
  5. <!-- 2. 和图片数量对应的小按钮 -->
  6. <div class="btn-group"></div>
  7. <!-- 3. 向前和向后的翻页按钮 -->
  8. <div class="skip">
  9. <a class="prev" href="" onclick="prevImg(event)">&lt;</a>
  10. <a class="next" href="" onclick="nextImg(event)">&gt;</a>
  11. </div>
  12. </div>
  13. </body>

JS 代码:

  1. window.onload = () => {
  2. // 1. 生成所有图片
  3. createImgs(imgGroup, banners.length);
  4. // 2. 生成与图片对应的小按钮
  5. createBtns(btnGroup, banners.length);
  6. //作业 :每2秒自动切换,使用 setInterval + 事件派发器完成
  7. //******作业开始******
  8. let id = autoPlay();
  9. //鼠标移入图片区时停止播放
  10. document.querySelector(".container").onmouseover = () => {
  11. clearInterval(id);
  12. };
  13. //鼠标移出图片区时重新自动播放
  14. document.querySelector(".container").onmouseout = () => {
  15. id = autoPlay();
  16. };
  17. };
  18. //自动播放函数
  19. function autoPlay() {
  20. return setInterval(() => {
  21. const a = document.querySelector(".next");
  22. a.addEventListener("autoEvent", nextImg);
  23. a.dispatchEvent(new Event("autoEvent"));
  24. a.removeEventListener("autoEvent", nextImg);
  25. }, 2000);
  26. }
  27. //******作业结束******

购物车自动计算的完善部分

  1. // 作业 : 当选择某个商品或去掉某个商品时, 能自动计算
  2. //******作业开始 1******
  3. //给所有checkbox添加监听事件,点击时能自动重新计算
  4. checkAll.addEventListener("change", autoCalculate);
  5. checkItems.forEach(function (item) {
  6. item.addEventListener("change", autoCalculate);
  7. });
  8. //******作业结束 1******
  9. function autoCalculate() {
  10. // 1. 获取每个商品的金额: 数量 * 单价
  11. const numbers = document.querySelectorAll('input[type="number"]');
  12. const numArr = [...numbers].map(function (num) {
  13. return num.value * 1;
  14. });
  15. const prices = document.querySelectorAll("tbody .price");
  16. const priceArr = [...prices].map(function (num) {
  17. return num.textContent * 1;
  18. });
  19. // 2. 计算出每个商品的金额(数组)
  20. const amountArr = [priceArr, numArr].reduce(function (prev, curr) {
  21. return prev.map(function (item, key) {
  22. return item * curr[key];
  23. });
  24. });
  25. //******作业开始 2******
  26. // 改造autoCalculate自动计算函数中总金额和总数量的计算过程:
  27. //因为reduce的index是从1开始,循环次数是n-1次,
  28. //所以需要单独处理一下checkItems[0]是否选中的情况
  29. // 3. 总金额
  30. // let total = amountArr.reduce(function (prev, curr) {
  31. // return prev + curr;
  32. // });
  33. let total = amountArr.reduce(function (prev, curr, index) {
  34. if (index == 1) {
  35. if (!checkItems[0].checked) prev = 0;
  36. }
  37. if (checkItems[index].checked) {
  38. return prev + curr;
  39. } else {
  40. return prev;
  41. }
  42. });
  43. // 4. 总数量
  44. // let sum = numArr.reduce(function (prev, curr) {
  45. // return prev + curr;
  46. // });
  47. let sum = numArr.reduce(function (prev, curr, index) {
  48. if (index == 1) {
  49. if (!checkItems[0].checked) prev = 0;
  50. }
  51. if (checkItems[index].checked) {
  52. return prev + curr;
  53. } else {
  54. return prev;
  55. }
  56. });
  57. //******作业结束 2******
  58. // 5. 将所有数据渲染到页面中
  59. document.querySelectorAll(".amount").forEach(function (item, index) {
  60. item.textContent = amountArr[index];
  61. });
  62. document.querySelector("#total-amount").textContent = total;
  63. document.querySelector("#sum").textContent = sum;
  64. }
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post