Blogger Information
Blog 47
fans 1
comment 0
visits 40473
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
双色球与选项卡与购物车
新手1314
Original
347 people have browsed it

双色球

HTML

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. display: grid;
  11. grid-template-columns: repeat(auto-fill, 30px);
  12. grid-auto-rows: 30px;
  13. gap: 5px;
  14. }
  15. .box > div {
  16. border-radius: 50%;
  17. display: grid;
  18. place-items: center;
  19. background-color: red;
  20. color: white;
  21. box-shadow: 2px 2px 2px #666;
  22. }
  23. .box > div:last-of-type {
  24. background-color: blue;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="box"></div>
  30. <script>
  31. // 红球: 1-33=>6,不重复,排序
  32. // 蓝球: 1-16=>1, 可以和红球重复
  33. // 思路:
  34. // 1. 先取红球, 放在一个中奖号数组中, 排序
  35. // 2. 然后把蓝球追加到中奖数组中
  36. // 3. 最后还要马中奖数组渲染到页面: dom
  37. // 红球
  38. let red = [];
  39. // 中奖数组
  40. let list = [];
  41. // 1.生成1-33个红球
  42. for (let i = 1; i <= 33; i++) {
  43. red.push(i);
  44. }
  45. console.log(red);
  46. // 2.从33个红球中取出6个
  47. for (let i = 0; i < 6; i++) {
  48. let index = Math.floor(Math.random() * red.length);
  49. list.push(red[index]);
  50. red.splice(index, 1);
  51. }
  52. list.sort((a, b) => a - b);
  53. console.log(list);
  54. // 3、生成一个篮球,并追加到中奖数组中。
  55. let blue = Math.ceil(Math.random() * 16);
  56. list.push(blue);
  57. console.log(list);
  58. // 4.将中奖号码显示到页面中
  59. let box = document.querySelector(".box");
  60. list.forEach(news => {
  61. let ball = document.createElement("div");
  62. ball.textContent = news;
  63. box.append(ball);
  64. });
  65. </script>
  66. </body>
  67. </html>

选项卡

HTML

  1. <!DOCTYPE html>
  2. <html lang="en">
  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: 28em;
  11. display: grid;
  12. grid-template-columns: 3em 1fr;
  13. }
  14. .box ul {
  15. margin: 0;
  16. padding: 0;
  17. }
  18. .box li {
  19. list-style: none;
  20. /* height: 2em; */
  21. }
  22. .box li a {
  23. text-decoration: none;
  24. }
  25. .box li:hover {
  26. cursor: pointer;
  27. }
  28. .box .content {
  29. background-color: lightgreen;
  30. display: none;
  31. place-self: center start;
  32. }
  33. .box .content.active {
  34. display: block;
  35. }
  36. .box .menu li.active {
  37. background-color: lightgreen;
  38. }
  39. </style>
  40. </head>
  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. // console.log(event.currentTarget);
  78. let ul = event.currentTarget;
  79. let li = event.target;
  80. // ul.children:返回所有子元素也就是返回所有Li元素
  81. console.log(ul.children);
  82. //点击li元素去掉高亮
  83. [...ul.children].forEach(li => li.classList.remove("active"));
  84. //将点击的li元素赋予class值,显示高亮
  85. li.classList.add("active");
  86. let content = document.querySelectorAll(".content");
  87. console.log(content);
  88. //点击li元素,遍历所有ul里的content,去掉样式active
  89. content.forEach(li => li.classList.remove("active"));
  90. //找到数组里第一个ul里的index值与li里index相同的ul元素,并赋予它active样式
  91. [...content].find(ul => ul.dataset.index === li.dataset.index).classList.add("active");
  92. // console.log([...content].filter(ul => ul.dataset.index === li.dataset.index));
  93. // console.log(ul.dataset.index);
  94. }
  95. </script>
  96. </body>
  97. </html>

购物车

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>
  68. <span>选择</span>
  69. <span>品名</span>
  70. <span>数量</span>
  71. <span>单价</span>
  72. <span>金额</span>
  73. </li>
  74. <li>
  75. <input type="checkbox" onchange="checkItems()" checked />
  76. <span class="content">手机</span>
  77. <input type="number" value="1" min="1" class="num" />
  78. <span class="price">100</span>
  79. <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="2" min="1" class="num" />
  85. <span class="price">200</span>
  86. <span class="amount">0</span>
  87. </li>
  88. <li>
  89. <input type="checkbox" onchange="checkItems()" checked />
  90. <span class="content">相机</span>
  91. <input type="number" value="3" min="1" class="num" />
  92. <span class="price">300</span>
  93. <span class="amount">0</span>
  94. </li>
  95. <li>
  96. <span>总计:</span>
  97. <span class="total-num">0</span>
  98. <span class="total-amount">0</span>
  99. </li>
  100. </ul>
  101. <button class="account">结算</button>
  102. </div>
  103. <script>
  104. // 全选状态动态改变商品状态
  105. function checkAll() {
  106. //确定点击全选的返回值
  107. console.log(event.target);
  108. // 把全选的状态赋值于变量
  109. let status = event.target.checked;
  110. //确认全选按钮状态
  111. console.log(status);
  112. // 读取商品选择的状态
  113. let item = document.querySelectorAll(".list li input[type='checkbox']");
  114. // 遍历所有商品选择状态,并把全选的状态动态赋予商品的状态
  115. item.forEach(item => (item.checked = status));
  116. autoCalculate();
  117. }
  118. // 商品状态动态改变全选状态
  119. function checkItems() {
  120. // 获取所有商品的选择状态
  121. let item = document.querySelectorAll(".list li input[type='checkbox']");
  122. // 判断所有商品状态是否为true
  123. let status = [...item].every(item => item.checked === true);
  124. //将所有商品的状态赋予全选按钮,全部商品全选中则为true,有一个没选中则为false
  125. document.querySelector(".check-all").checked = status;
  126. autoCalculate();
  127. }
  128. // 获取所有商品元素
  129. let num = document.querySelectorAll(".num");
  130. function goodStatus(numArr) {
  131. let items = document.querySelectorAll(".list li input[type='checkbox']");
  132. return numArr.map((num, index) => {
  133. if (items[index].checked === false) {
  134. return (num = 0);
  135. } else {
  136. return num;
  137. }
  138. });
  139. }
  140. console.log(num);
  141. // 总数量
  142. function getTotalNum(numArr) {
  143. numArr = goodStatus(numArr);
  144. return numArr.reduce((acc, list) => acc + list);
  145. }
  146. // console.log(getTotalNum([1, 2, 3]));
  147. // 每个商品的金额
  148. function getAmount(numArr, priceArr) {
  149. return numArr.map((num, index) => num * priceArr[index]);
  150. }
  151. // console.log(getAmount([1, 2, 3], [100, 200, 300]));
  152. // 总金额
  153. function getTotalAmount(amountArr) {
  154. amountArr = goodStatus(amountArr);
  155. return amountArr.reduce((acc, list) => acc + list);
  156. }
  157. // 自动计算
  158. function autoCalculate() {
  159. // 获取商品数量数组
  160. let numArr = [...num].map(num => parseInt(num.value));
  161. console.log(numArr);
  162. // 获取单价数组元素
  163. let price = document.querySelectorAll(".price");
  164. // console.log(price);
  165. //将单价数组元素的值赋予变量
  166. let priceAll = [...price].map(price => parseInt(price.textContent));
  167. console.log(priceAll);
  168. // 金额数组
  169. // 调用上面的getAmount函数并传入商品数量与单价元素
  170. let amountAll = getAmount(numArr, priceAll);
  171. // 总数量
  172. console.log(getTotalNum(numArr));
  173. document.querySelector(".total-num").textContent = getTotalNum(numArr);
  174. //总金额
  175. document.querySelector(".total-amount").textContent = getTotalAmount(amountAll);
  176. // 为每个商品加载时自动计算
  177. document.querySelectorAll(".amount").forEach((amount, index) => (amount.textContent = amountAll[index]));
  178. }
  179. // console.log(autoCalculate());
  180. window.onload = autoCalculate;
  181. num.forEach(num => (num.onchange = autoCalculate));
  182. </script>
  183. </body>
  184. </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