Blogger Information
Blog 11
fans 0
comment 0
visits 6419
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js-基础(七)双色球、选项卡、购物车
Technology has temperature
Original
397 people have browsed it

1.双色球

  1. <body>
  2. <div class="box"></div>
  3. <script>
  4. let redarr = [];
  5. let resultarr = [];
  6. //1.生成1-33个红球
  7. for(let i = 1;i <= 33;i++){
  8. redarr.push(i);
  9. }
  10. console.log(redarr);
  11. // 2.从33个红球中取出6个
  12. for(let i = 0;i < 6; i++){
  13. let index = Math.floor(Math.random() * redarr.length);
  14. resultarr.push(redarr[index]);
  15. // 会出现重复
  16. redarr.splice(index,1);
  17. }
  18. resultarr.sort((a,b)=>a-b);
  19. console.log(resultarr);
  20. //3. 生成一个蓝球, 并追加到中奖数组中
  21. let blue = Math.floor(Math.random() * 16)+1;
  22. console.log(blue);
  23. resultarr.push(blue);
  24. console.log(resultarr);
  25. // 4. 将中奖号码显示到页面中
  26. const box = document.querySelector(".box");
  27. resultarr.forEach(item=>{
  28. const ball = document.createElement('div');
  29. ball.textContent = item;
  30. box.append(ball);
  31. });
  32. </script>
  33. <style>
  34. .box {
  35. display: grid;
  36. grid-template-columns: repeat(auto-fill, 30px);
  37. grid-auto-rows: 30px;
  38. gap: 5px;
  39. }
  40. .box>div {
  41. border-radius: 50%;
  42. display: grid;
  43. place-items: center;
  44. background-color: red;
  45. color: white;
  46. box-shadow: 2px 2px 2px #666;
  47. }
  48. .box>div:last-of-type {
  49. background-color: blue;
  50. }
  51. </style>
  52. </body>

2.选项卡

  1. function show(){
  2. // 事件绑定者
  3. //console.log(event.currentTarget);
  4. //事件主体
  5. //console.log(event.target);
  6. const ul = event.currentTarget;
  7. const li = event.target;
  8. // 1. 将原高亮的标签去掉active,并把当前的标签设置为active
  9. [...ul.children].forEach(li=>
  10. li.classList.remove('active'));
  11. li.classList.add('active');
  12. // 2. 根据标签的data-index进行查询,获取与它对应的列表
  13. const content = document.querySelectorAll('.content');
  14. content.forEach(li=>li.classList.remove('active'));
  15. [...content].find(ul=>ul.dataset.index === li.dataset.index ).classList.add('active');
  16. }

3.购物车

  1. <script>
  2. // 一. 全选
  3. function checkAll(){
  4. //1.全选按钮状态
  5. let status = document.querySelector('.check-all').checked;
  6. console.log(status);
  7. // 2. 根据状态动态设置所有商品的状态
  8. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  9. console.log(items);
  10. items.forEach(item=>(item.checked=status));
  11. autoCalculate();
  12. }
  13. // 二. 根据用户选择来动态设置全选状态
  14. function checkItems(){
  15. // 1. 拿到全部商品
  16. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  17. // 2. 判断状态,只有全部被选择,才需要设置全选为true, array.every
  18. let status = [...items].every(item=>item.checked === true);
  19. console.log(status);
  20. // 3. 将这个新状态设置到全选按钮
  21. document.querySelector('.check-all').checked = status;
  22. autoCalculate();
  23. }
  24. //三、商品自动计算 所有计算都是基于商品数量的变化
  25. const nums = document.querySelectorAll('.num');
  26. console.log(nums);
  27. nums.forEach(item=>console.log(item.value));
  28. //1.判断选中
  29. function selectstatus(numArr){
  30. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  31. console.log(items);
  32. return numArr.map((num, index) => {
  33. if(items[index].checked === false){
  34. return num = 0;
  35. }else{
  36. return num;
  37. }
  38. })
  39. }
  40. //2.计算总数量
  41. function getTotalNum(numArr){
  42. numArr = selectstatus(numArr);
  43. console.log(numArr);
  44. return numArr.reduce((acc,cur)=>acc+cur);
  45. }
  46. //3. 计算每个商品的金额
  47. function getAmount(numArr, priceArr){
  48. //金额=数量*单价
  49. return numArr.map((num,index)=>num*priceArr[index]);
  50. }
  51. //console.log(getTotalNum([1,2,3]));
  52. // console.log(getAmount([1, 2, 3], [100, 200, 300]));
  53. //4.计算总金额
  54. function getTatalAmount(amountArr){
  55. amountArr = selectstatus(amountArr);
  56. return amountArr.reduce((acc,cur)=>acc+cur);
  57. }
  58. //5.自动计算
  59. function autoCalculate(){
  60. //商品数量数组
  61. const numArr = [...nums].map(num=>parseInt(num.value));
  62. console.log(numArr);
  63. //单价数组
  64. const prices = document.querySelectorAll('.price');
  65. console.log(prices);
  66. const priceArr = [...prices].map(price=>parseInt(price.textContent));
  67. console.log(priceArr);
  68. //金额数组
  69. const amountArr = getAmount(numArr,priceArr);
  70. console.log(amountArr);
  71. //总数量
  72. console.log(getTotalNum(numArr));
  73. document.querySelector('.total-num').textContent = getTotalNum(numArr);
  74. //总金额
  75. document.querySelector('.total-amount').textContent = getTatalAmount(amountArr);
  76. //// 为每个商品填充金额
  77. document.querySelectorAll('.amount').forEach((amount,index)=>(amount.textContent = amountArr[index]));
  78. }
  79. //6.当页面加载的时候自动计算
  80. window.onload = autoCalculate;
  81. //7.当数量更新时,自动计算所有数据
  82. nums.forEach(num=>(num.onchange = autoCalculate));
  83. </script>
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