Blogger Information
Blog 18
fans 0
comment 0
visits 7932
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
双色球, 选项卡,购物车
只如初见
Original
607 people have browsed it

双色球

代码

  1. <div class="box"></div>
  2. <script>
  3. // 临时数组: 放红球
  4. let arr = [];
  5. // 中奖数组,最后应该有7个数,6个红球,1个蓝球
  6. let result = [];
  7. // 1. 生成1-33个红球
  8. for (let i = 1; i <= 33; i++) {
  9. arr.push(i);
  10. }
  11. //console.log(arr);
  12. // 2. 从33个红球中随机取出6个
  13. for (let i = 0; i < 6; i++) {
  14. let index = Math.floor(Math.random() * arr.length);
  15. result.push(arr[index]);
  16. arr.splice(index, 1);
  17. }
  18. //排序
  19. result.sort((a, b) => a - b);
  20. console.log(result);
  21. // 3. 生成蓝色球
  22. let blue = Math.floor(Math.random() * 16) + 1;
  23. result.push(blue);
  24. // 将生成的双色球放入页面显示
  25. const box = document.querySelector(".box");
  26. result.forEach(function (item) {
  27. const twoball = document.createElement('div');
  28. twoball.textContent = item;
  29. box.append(twoball);
  30. });
  31. </script>
  32. <style>
  33. .box {
  34. display: grid;
  35. grid-template-columns: repeat(auto-fill, 30px);
  36. grid-auto-rows: 30px;
  37. gap: 5px;
  38. }
  39. .box>div {
  40. border-radius: 50%;
  41. display: grid;
  42. place-items: center;
  43. background-color: red;
  44. color: white;
  45. box-shadow: 2px 2px 2px #666;
  46. }
  47. .box>div:last-of-type {
  48. background-color: blue;
  49. }
  50. </style>

效果图

选项卡

代码

  1. <div class="box">
  2. <!-- 1. 标签 -->
  3. <!-- 子元素上的点击事件会冒泡到父元素,利用这个特点,只需要给父元素添加点击事件就可以了 -->
  4. <ul class="menu" onclick="show()">
  5. <!-- 先给默认显示的标签和对应的内容添加 class="active"处于激活状态/可见 -->
  6. <!-- 使用自定义属性data-index使标签和与之对应的内容进行绑定 -->
  7. <li data-index="1" class="active">本省</li>
  8. <li data-index="2">全国</li>
  9. <li data-index="3">防疫</li>
  10. </ul>
  11. <!-- 2. 内容 -->
  12. <ul class="content active" data-index="1">
  13. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  14. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  15. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  16. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  17. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  18. </ul>
  19. <ul class="content" data-index="2">
  20. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  21. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  22. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  23. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  24. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  25. </ul>
  26. <ul class="content" data-index="3">
  27. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  28. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  29. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  30. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  31. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  32. </ul>
  33. </div>
  34. <script>
  35. function show() {
  36. // 事件绑定着
  37. const listUl = event.currentTarget;
  38. // 事件主体
  39. const listLi = event.target;
  40. // 1. 将原高亮的标签去掉active,并把当前的标签设置为active
  41. [...listUl.children].forEach(function (item) {
  42. item.classList.remove('active');
  43. });
  44. listLi.classList.add('active');
  45. // 2. 根据标签的data-index进行查询,获取与它对应的列表
  46. const content = document.querySelectorAll('.content');
  47. content.forEach(function (item) {
  48. item.classList.remove('active');
  49. });
  50. //关联对应的内容区块,并显示
  51. [...content].find(listUl => listUl.dataset.index === listLi.dataset.index).classList.add('active');
  52. }
  53. </script>

效果图

购物车

代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>购物车</title>
  8. <style>
  9. .box {
  10. width: 22em;
  11. height: 2em;
  12. }
  13. .list>li {
  14. height: 1.6em;
  15. background-color: #efefef;
  16. display: grid;
  17. grid-template-columns: repeat(5, 3em);
  18. gap: 1em;
  19. place-items: center right;
  20. border-bottom: 1px solid #ccc;
  21. }
  22. .list>li:first-of-type {
  23. background-color: lightseagreen;
  24. color: white;
  25. }
  26. .list>li:hover:not(:first-of-type) {
  27. cursor: pointer;
  28. background-color: lightcyan;
  29. }
  30. .list>li input[type='number'] {
  31. width: 3em;
  32. border: none;
  33. outline: none;
  34. text-align: center;
  35. font-size: 1em;
  36. background-color: transparent;
  37. }
  38. .list>li:last-of-type span.total-num,
  39. .list>li:last-of-type span.total-amount {
  40. grid-column: span 2;
  41. place-self: center right;
  42. color: lightseagreen;
  43. }
  44. .account {
  45. float: right;
  46. background-color: lightseagreen;
  47. color: white;
  48. border: none;
  49. outline: none;
  50. width: 4.5em;
  51. height: 1.8em;
  52. }
  53. .account:hover {
  54. background-color: coral;
  55. cursor: pointer;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="box">
  61. <div class="selectAll">
  62. <!-- checkAll(): 当全选按钮的状态发生变化化,触发该事件 change -->
  63. <input type="checkbox" class="check-all" name="check-all" onchange="checkAll()" checked />
  64. <label for="check-all">全选</label>
  65. </div>
  66. <ul class="list">
  67. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
  68. <li>
  69. <input type="checkbox" onchange="checkItems()" checked />
  70. <span class="content">手机</span>
  71. <input type="number" value="1" min="1" class="num" />
  72. <span class="price">100</span>
  73. <span class="amount">0</span>
  74. </li>
  75. <li>
  76. <input type="checkbox" onchange="checkItems()" checked />
  77. <span class="content">电脑</span>
  78. <input type="number" value="2" min="1" class="num" />
  79. <span class="price">200</span><span class="amount">0</span>
  80. </li>
  81. <li>
  82. <input type="checkbox" onchange="checkItems()" checked />
  83. <span class="content">相机</span>
  84. <input type="number" value="3" min="1" class="num" />
  85. <span class="price">300</span>
  86. <span class="amount">0</span>
  87. </li>
  88. <li>
  89. <span>总计:</span>
  90. <span class="total-num">0</span>
  91. <span class="total-amount">0</span>
  92. </li>
  93. </ul>
  94. <button class="account">结算</button>
  95. </div>
  96. <script>
  97. // 1. 全选
  98. function checkAll() {
  99. // 1. 全选按钮状态
  100. let status = event.target.checked;
  101. // console.log(status);
  102. // 2. 根据状态动态设置所有商品的状态
  103. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  104. items.forEach(item => (item.checked = status));
  105. //autoCalculate();
  106. // document
  107. // .querySelectorAll('.list li input[type="checkbox"]')
  108. // .forEach(item => (item.checked = event.target.checked));
  109. }
  110. // 2. 根据用户选择来动态设置全选状态
  111. function checkItems() {
  112. // 1. 拿到全部商品
  113. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  114. // 2. 判断状态,只有全部被选择,才需要设置全选为true, array.every
  115. let status = [...items].every(item => item.checked === true);
  116. // console.log(status);
  117. // 3. 将这个新状态设置到全选按钮
  118. document.querySelector('.check-all').checked = status;
  119. autoCalculate();
  120. // document.querySelector('.check-all').checked = [
  121. // ...document.querySelectorAll('.list li input[type="checkbox"]'),
  122. // ].every(item => item.checked === true);
  123. }
  124. // 商品自动计算
  125. // 所有计算都是基于商品数量的变化
  126. const nums = document.querySelectorAll('.num');
  127. //console.log(nums);
  128. // nums.forEach(item => console.log(item.value));
  129. // 1. 计算总数量
  130. function getTotalNum(numArr) {
  131. numArr = goodStatus(numArr);
  132. return numArr.reduce((acc, cur) => acc + cur);
  133. }
  134. // 2. 计算每个商品的金额
  135. function getAmount(numArr, priceArr) {
  136. // 金额 = 数量 * 单价
  137. return numArr.map((num, index) => num * priceArr[index]);
  138. }
  139. // console.log(getAmount([1, 2, 3], [100, 200, 300]));
  140. // 3. 计算总金额
  141. function getTotalAmount(amountArr) {
  142. amountArr = goodStatus(amountArr);
  143. return amountArr.reduce((acc, cur) => acc + cur);
  144. }
  145. //判断商品是否被选中,获取到选中的商品
  146. function goodStatus(numArr) {
  147. let items = document.querySelectorAll(
  148. ".list li input[type='checkbox']"
  149. );
  150. return numArr.map((num, index) => {
  151. if (items[index].checked === false) {
  152. return (num = 0);
  153. } else {
  154. return num;
  155. }
  156. });
  157. }
  158. // 4. 自动计算
  159. function autoCalculate() {
  160. // 商品数量数组
  161. const numArr = [...nums].map(num => parseInt(num.value));
  162. //console.log(numArr);
  163. // 单价数组
  164. const prices = document.querySelectorAll('.price');
  165. const priceArr = [...prices].map(price => parseInt(price.textContent));
  166. //console.log(priceArr);
  167. // 金额数组
  168. const amountArr = getAmount(numArr, priceArr);
  169. //console.log(amountArr);
  170. // 总数量
  171. console.log(getTotalNum(numArr));
  172. document.querySelector('.total-num').textContent = getTotalNum(numArr);
  173. // 总金额
  174. document.querySelector('.total-amount').textContent = getTotalAmount(amountArr);
  175. // 为每个商品填充金额
  176. document.querySelectorAll('.amount').forEach((amount, index) => (amount.textContent = amountArr[index]));
  177. }
  178. // 当页面加载的时候自动计算
  179. window.onload = autoCalculate;
  180. // 当数量更新时,自动计算所有数据
  181. nums.forEach(num => (num.onchange = autoCalculate));
  182. // 作业 : 只计算选中商品的金额,同时更新总数量和总金额
  183. // vue: 计算属性, 侦听器,可以极大的简化以上过程
  184. </script>
  185. </body>
  186. </html>

效果

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