Blogger Information
Blog 33
fans 0
comment 0
visits 17125
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
双色球,选项卡,购物车实例演示
lucaslwk
Original
368 people have browsed it

双色球,选项卡,购物车实例演示

1.双色球

第一种实现方式

双色球1

  1. //生成33个红球
  2. let redBall = [];
  3. for (let i = 1; i <= 33; i++) {
  4. redBall.push(i);
  5. }
  6. console.log(redBall);
  7. //从33个红球中随机取出不同的6个并asc排序
  8. let results = [];
  9. for (let i = 0; i < 6; i++) {
  10. let choose = Math.floor(Math.random() * redBall.length);
  11. results.push(redBall[choose]);
  12. redBall.splice(choose, 1);
  13. }
  14. results.sort((a, b) => a - b);
  15. //从16个蓝球中取出一个
  16. let blueBall = Math.floor(Math.random() * 16 + 1);
  17. //组成中奖数组
  18. results.push(blueBall);
  19. console.log(results);
  20. //显示在DOM中
  21. const box = document.querySelector(".box");
  22. results.forEach((item) => {
  23. const ball = document.createElement("div");
  24. ball.textContent = item;
  25. box.append(ball);
  26. });

第二种实现方式

双色球2

  1. //设置33个红球
  2. let redBallNum = 33;
  3. //设置红球数组
  4. let redBall = [];
  5. //判断是否包含,不包含则添加
  6. while (redBall.length < 6) {
  7. let num = Math.floor(Math.random() * redBallNum + 1);
  8. if (redBall.indexOf(num) == -1) {
  9. redBall.push(num);
  10. }
  11. }
  12. //排序
  13. redBall.sort((a, b) => a - b);
  14. //生成蓝球
  15. let blueBall = Math.floor(Math.random() * 16 + 1);
  16. let results = redBall.concat(blueBall);
  17. //显示在dom上
  18. const box = document.querySelector(".box");
  19. results.forEach((item) => {
  20. const ball = document.createElement("div");
  21. ball.textContent = item;
  22. box.append(ball);
  23. });

2.选项卡

选项卡

  1. function show() {
  2. //获取事件绑定者和事件发生者
  3. const ul = event.currentTarget;
  4. const li = event.target;
  5. //移除原active标签,给当前点击的标签添加active属性
  6. [...ul.children].forEach((item) => item.classList.remove("active"));
  7. li.classList.add("active");
  8. //获取内容列表
  9. const content = document.querySelectorAll(".content");
  10. //移除原active标签
  11. content.forEach((item) => item.classList.remove("active"));
  12. //找到自定义属性相同的元素并添加actice属性
  13. // const chooseContent = [...content].filter(
  14. // (item) => item.dataset.index === li.dataset.index
  15. // )[0];
  16. //简化为,filter只返回为真元素组成的数组,find返回满足条件的第一个
  17. const chooseContent = [...content].find(
  18. (item) => item.dataset.index === li.dataset.index
  19. );
  20. chooseContent.classList.add("active");
  21. }

3.购物车

购物车

  1. let items = document.querySelectorAll(".list li input[type='checkbox']");
  2. //获取各个物品的选中状态
  3. let itemStatus = [...items].map((item) => item.checked);
  4. //全选按钮
  5. function checkAll() {
  6. let status = event.target.checked;
  7. items.forEach((item) => (item.checked = status));
  8. //更新单个物品的状态
  9. itemStatus.forEach((item, index) => {
  10. itemStatus.splice(index, 1, status);
  11. });
  12. autoCalculate();
  13. }
  14. //单选按钮
  15. function checkItems() {
  16. let status = [...items].every((item) => item.checked === true);
  17. document.querySelector(".check-all").checked = status;
  18. //更新单个物品的状态
  19. itemStatus.splice([...items].indexOf(event.target), 1, event.target.checked);
  20. autoCalculate();
  21. }
  22. //取出页面上的数量与单价
  23. const nums = document.querySelectorAll(".list li .num");
  24. const prices = document.querySelectorAll(".list li .price");
  25. //计算总数量
  26. function getTotalNum(numArr) {
  27. let chooseNumArr = numArr.map((item, index) => {
  28. if (itemStatus[index] === false) {
  29. return 0;
  30. } else return item;
  31. });
  32. return chooseNumArr.reduce((a, b) => a + b);
  33. }
  34. //计算总金额
  35. function getTotalAmount(amountArr) {
  36. let chooseamountArr = amountArr.map((item, index) => {
  37. if (itemStatus[index] === false) {
  38. return 0;
  39. } else return item;
  40. });
  41. return chooseamountArr.reduce((a, b) => a + b);
  42. }
  43. //计算单个商品的金额
  44. function getAmount(numArr, priceArr) {
  45. return numArr.map((item, index) => item * priceArr[index]);
  46. }
  47. function autoCalculate() {
  48. //数量获取
  49. const numArr = [...nums].map((item) => parseInt(item.value));
  50. //价格获取
  51. const priceArr = [...prices].map((item) => parseInt(item.textContent));
  52. //单价计算
  53. const amountArr = getAmount(numArr, priceArr);
  54. //显示在页面上
  55. document.querySelector(".total-num").textContent = getTotalNum(numArr);
  56. document.querySelectorAll(".amount").forEach((item, index) => {
  57. item.textContent = amountArr[index];
  58. });
  59. document.querySelector(".total-amount").textContent =
  60. getTotalAmount(amountArr);
  61. }
  62. //加载时计算
  63. window.onload = autoCalculate;
  64. //数量更新时计算
  65. nums.forEach((item) => (item.onchange = autoCalculate));
Correcting teacher:PHPzPHPz

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