博主信息
博文 20
粉丝 0
评论 1
访问量 17427
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
轮播图自动播放和购物车自动计算的完善
zg的php学习
原创
706人浏览过

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

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

轮播图自动播放

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. }
批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学