Blogger Information
Blog 18
fans 0
comment 2
visits 8738
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
双色球、选项卡、购物车实例
弦知音的博客
Original
510 people have browsed it

1 双色球

  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. </head>
  9. <style>
  10. .box {
  11. display: grid;
  12. grid-template-columns: repeat(auto-fill, 30px);
  13. grid-auto-rows: 30px;
  14. gap: 5px;
  15. }
  16. .box > li {
  17. list-style: none;
  18. display: grid;
  19. border-radius: 50%;
  20. background-color: red;
  21. color: wheat;
  22. box-shadow: 2px 2px 2px #666;
  23. line-height: 30px;
  24. text-align: center;
  25. }
  26. .box > li:last-of-type {
  27. background-color: blue;
  28. }
  29. </style>
  30. <body>
  31. <div class="box"></div>
  32. <script>
  33. // 红球: 1-33=>6,不重复,排序
  34. // 篮球: 1-16=>1,可以和红球重复
  35. // 思路:
  36. // 1、先取红球,放在一个中奖号数组中,排序
  37. // 2、然后把蓝球追加到中奖数组中
  38. // 3.最后还要把中奖数组渲染到页面:dom
  39. // 临时数组
  40. let arr = [];
  41. // 中奖数组
  42. let res = [];
  43. // 1、生成1-33个红球
  44. for (let i = 1; i <= 33; i++) {
  45. arr.push(i);
  46. }
  47. console.log(arr);
  48. // 2、从33个红球中取出6个
  49. for (let i = 0; i < 6; i++) {
  50. // arr的下标取值范围:[0-32]
  51. // 问题:如何生成0-32这样的整数索引,用它来获取数组中的数据
  52. // Math.random(): 0-1之间的随机小数
  53. // 0-33之间的小数,但永远不会到33这个数
  54. // 0.0123, 32.99999,向下取整
  55. // 0.0123 => 0 , 32.99999 => 32
  56. // console.log(Math.random() * 33);
  57. // console.log(Math.random() * arr.length);
  58. // console.log(Math.floor(Math.random() * arr.length));
  59. // 索引是唯一的,为什么重复了
  60. // 索引不好重复,但是对应的值会重复
  61. // [1,2,3,2]
  62. // 0=>1,1=>2,2=>3,3=>2
  63. // 应该每取一个就从原数组中删除一个
  64. let index = Math.floor(Math.random() * arr.length);
  65. // console.log(Math.ceil(1.000001)); 向上取整 返回2
  66. res.push(arr[index]);
  67. }
  68. // 排序
  69. res.sort((a, b) => a - b);
  70. console.log(res);
  71. // 3、生成一个蓝球,并追加到中奖数组中
  72. // 必须是:1-16之间,并包括1,16
  73. let blue = Math.ceil(Math.random() * 16);
  74. console.log(blue);
  75. res.push(blue);
  76. console.log(res);
  77. // 4、将中奖号码显示到页面中
  78. const box = document.querySelector('.box');
  79. res.forEach((item) => {
  80. const ball = document.createElement('li');
  81. ball.textContent = item;
  82. box.append(ball);
  83. });
  84. </script>
  85. </body>
  86. </html>

2.选项卡

  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. </head>
  9. <style>
  10. .box {
  11. width: 28em;
  12. display: grid;
  13. grid-template-columns: 3em 1fr;
  14. }
  15. .box ul {
  16. margin: 0;
  17. padding: 0;
  18. }
  19. .box li {
  20. list-style: none;
  21. /* height: 2em; */
  22. }
  23. .box li a {
  24. text-decoration: none;
  25. }
  26. .box li:hover {
  27. cursor: pointer;
  28. }
  29. .box .content {
  30. background-color: lightgreen;
  31. display: none;
  32. place-self: center start;
  33. }
  34. .box .content.active {
  35. display: block;
  36. }
  37. .box .menu li.active {
  38. background-color: lightgreen;
  39. }
  40. </style>
  41. <body>
  42. <div class="box">
  43. <!-- 1. 标签 -->
  44. <!-- 子元素上的点击事件会冒泡到父元素,利用这个特点,只需要给父元素添加点击事件就可以了 -->
  45. <ul class="menu" onclick="show()">
  46. <!-- 先给默认显示的标签和对应的内容添加 class="active"处于激活状态/可见 -->
  47. <!-- 使用自定义属性data-index使标签和与之对应的内容进行绑定 -->
  48. <li data-index="1" class="active">本省</li>
  49. <li data-index="2">全国</li>
  50. <li data-index="3">防疫</li>
  51. </ul>
  52. <!-- 2. 内容 -->
  53. <ul class="content active" data-index="1">
  54. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  55. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  56. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  57. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  58. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  59. </ul>
  60. <ul class="content" data-index="2">
  61. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  62. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  63. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  64. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  65. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  66. </ul>
  67. <ul class="content" data-index="3">
  68. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  69. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  70. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  71. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  72. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  73. </ul>
  74. </div>
  75. <script>
  76. function show() {
  77. // 事件绑定者
  78. // console.log(event.currentTarget);
  79. // 事件主体
  80. // console.log(event.target);
  81. const ul = event.currentTarget;
  82. const li = event.target;
  83. // 1. 将原高亮的标签去掉active,并把当前的标签设置为active
  84. [...ul.children].forEach((li) => li.classList.remove('active'));
  85. li.classList.add('active');
  86. // 2. 根据标签的data-index进行查询,获取与它对应的列表
  87. const content = document.querySelectorAll('.content');
  88. // document.querySelectorAll()返回的是NodeList对象,上面已定义了forEach,所以不用转真数组
  89. console.log(content);
  90. content.forEach((li) => li.classList.remove('active'));
  91. console.log(
  92. [...content].filter((ul) => ul.dataset.index === li.dataset.index)[0]
  93. );
  94. // filter(ul => ul.dataset.index === li.dataset.index)[0]
  95. // 用find进行简化, find就是获取filter结果数组中的第一个
  96. [...content]
  97. .find((ul) => ul.dataset.index === li.dataset.index)
  98. .classList.add('active');
  99. }
  100. </script>
  101. </body>
  102. </html>

3.购物车(半成品)

  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
  64. type="checkbox"
  65. class="check-all"
  66. name="check-all"
  67. onchange="checkAll()"
  68. checked
  69. />
  70. <label for="check-all">全选</label>
  71. </div>
  72. <ul class="list">
  73. <li>
  74. <span>选择</span><span>品名</span><span>数量</span><span>单价</span
  75. ><span>金额</span>
  76. </li>
  77. <li>
  78. <input type="checkbox" onchange="checkItems()" checked />
  79. <span class="content">手机</span>
  80. <input type="number" value="1" min="1" class="num" />
  81. <span class="price">100</span>
  82. <span class="amount">0</span>
  83. </li>
  84. <li>
  85. <input type="checkbox" onchange="checkItems()" checked />
  86. <span class="content">电脑</span>
  87. <input type="number" value="2" min="1" class="num" />
  88. <span class="price">200</span><span class="amount">0</span>
  89. </li>
  90. <li>
  91. <input type="checkbox" onchange="checkItems()" checked />
  92. <span class="content">相机</span>
  93. <input type="number" value="3" min="1" class="num" />
  94. <span class="price">300</span>
  95. <span class="amount">0</span>
  96. </li>
  97. <li>
  98. <span>总计:</span>
  99. <span class="total-num">0</span>
  100. <span class="total-amount">0</span>
  101. </li>
  102. </ul>
  103. <button class="account">结算</button>
  104. </div>
  105. <script>
  106. // 1.全选
  107. function checkAll() {
  108. // 1.全选按钮状态
  109. let status = event.target.checked;
  110. // console.log(status);
  111. // 2.根据状态动态设置所有商品的状态
  112. let items = document.querySelectorAll(
  113. '.list li input[type="checkbox"]'
  114. );
  115. items.forEach((item) => (item.checked = status));
  116. }
  117. // 2.根据用户选择来动态设置全选状态
  118. function checkItems() {
  119. // 1.拿到全部商品
  120. let items = document.querySelectorAll(
  121. '.list li input[type="checkbox"]'
  122. );
  123. // 2.判断状态,只有全部被选中,才需要设置为全选true, array.every
  124. let status = [...items].every((item) => item.checked === true);
  125. // console.log(status);
  126. // 3.将这个状态设置到全选按钮
  127. document.querySelector('.check-all').checked = status;
  128. }
  129. // 商品自动计算
  130. // 所有计算都是基于商品数量的变化
  131. let nums = document.querySelectorAll('.num');
  132. console.log(nums);
  133. let items = document.querySelectorAll(".list li input[type='checkbox']");
  134. // nums.forEach((item) => console.log(item.value));
  135. //判断商品是否被选中,获取到选中的商品
  136. function goodStatus(numArr) {
  137. let items = document.querySelectorAll(
  138. ".list li input[type='checkbox']"
  139. );
  140. return numArr.map((num, index) => {
  141. if (items[index].checked === true) {
  142. return num;
  143. } else {
  144. return 0;
  145. }
  146. });
  147. }
  148. // 1. 计算总数量
  149. function getTotalNum(numArr) {
  150. return numArr.reduce((acc, cur) => acc + cur);
  151. }
  152. // 2. 计算每个商品的金额
  153. function getAmount(numArr, priceArr) {
  154. // 金额 = 数量 * 单价
  155. return numArr.map((num, index) => num * priceArr[index]);
  156. }
  157. // console.log(getAmount([1, 2, 4], [100, 200, 300]));
  158. // 3. 计算总金额
  159. function getTotalAmount(amountArr) {
  160. return amountArr.reduce((acc, cur) => acc + cur);
  161. }
  162. // 4. 自动计算
  163. function autoCalculate() {
  164. // 商品数量数组
  165. let numArr = [...nums].map((num) => parseInt(num.value));
  166. numArr = goodStatus(numArr);
  167. console.log(numArr);
  168. // 单价数组
  169. const prices = document.querySelectorAll('.price');
  170. const priceArr = [...prices].map((price) =>
  171. parseInt(price.textContent)
  172. );
  173. // 金额数组
  174. const amountArr = getAmount(numArr, priceArr);
  175. console.log(amountArr);
  176. // 总数量
  177. console.log(getTotalNum(numArr));
  178. document.querySelector('.total-num').textContent = getTotalNum(numArr);
  179. // 总金额
  180. document.querySelector('.total-amount').textContent =
  181. getTotalAmount(amountArr);
  182. // 为每个商品填充金额
  183. document
  184. .querySelectorAll('.amount')
  185. .forEach((amount, index) => (amount.textContent = amountArr[index]));
  186. }
  187. // 当页面加载的时候自动计算
  188. window.onload = autoCalculate;
  189. // 当数量更新时,自动计算所有数据
  190. nums.forEach((num) => (num.onchange = autoCalculate));
  191. // 计算选中状态商品数据
  192. items.forEach((item) => (item.onchange = autoCalculate));
  193. </script>
  194. </body>
  195. </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