Blogger Information
Blog 13
fans 0
comment 0
visits 6512
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
双色球,选项卡,购物车
shooter
Original
436 people have browsed it

双色球

效果图

代码

  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. button {
  27. margin-left: 100px;
  28. margin-top: 100px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="box"></div>
  34. <button onclick="suiji()">随机</button>
  35. <script>
  36. function suiji() {
  37. // 临时数组: 放红球
  38. let arr = [];
  39. // 中奖数组,最后应该有7个数,6个红球,1个蓝球
  40. let res = [];
  41. // 1. 生成1-33个红球
  42. for (let i = 0; i <= 33; i++) {
  43. arr.push(i);
  44. }
  45. console.log(arr);
  46. // 2. 从33个红球中取出6个
  47. for (let i = 0; i < 6; i++) {
  48. // 应该每取一个就从原始数组中删除一个
  49. let index = Math.floor(Math.random() * arr.length);
  50. res.push(arr[index]);
  51. arr.splice(index, 1);
  52. }
  53. //排序
  54. res.sort((a, b) => a - b);
  55. console.log(res);
  56. // 3. 生成一个蓝球, 并追加到中奖数组中
  57. // 必须是: 1-16之间,并包括 1, 16
  58. let blue = Math.floor(Math.random() * 16) + 1;
  59. console.log(blue);
  60. res.push(blue);
  61. console.log(res);
  62. //将中奖号码显示到页面中
  63. const box = document.querySelector('.box');
  64. let box1 = box.children.length;
  65. if (box1 !== 0) {
  66. box.innerHTML = '';
  67. }
  68. res.forEach((item) => {
  69. const ball = document.createElement('div');
  70. ball.textContent = item;
  71. box.append(ball);
  72. });
  73. }
  74. window.onload = suiji;
  75. </script>
  76. </body>
  77. </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. }
  21. .box li a {
  22. text-decoration: none;
  23. }
  24. .box li:hover {
  25. cursor: pointer;
  26. }
  27. .box .content {
  28. background-color: lightcoral;
  29. display: none;
  30. place-self: center start;
  31. }
  32. .box .content.active {
  33. display: block;
  34. }
  35. .box .menu li.active {
  36. background-color: lightcoral;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div class="box">
  42. <ul class="menu" onclick="show()">
  43. <li data-index="1" class="active">本省</li>
  44. <li data-index="2">全国</li>
  45. <li data-index="3">防疫</li>
  46. </ul>
  47. <!-- 2. 内容 -->
  48. <ul class="content active" data-index="1">
  49. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  50. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  51. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  52. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  53. <li><a href="">刚刚通报!2021安徽GDP预计破4万亿元!</a></li>
  54. </ul>
  55. <ul class="content" data-index="2">
  56. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  57. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  58. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  59. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  60. <li><a href="">总书记关心北京冬奥会,冬残奥会筹办纪实</a></li>
  61. </ul>
  62. <ul class="content" data-index="3">
  63. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  64. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  65. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  66. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  67. <li><a href="">坚持“动态清零”不动摇(人民论坛)</a></li>
  68. </ul>
  69. </div>
  70. <script>
  71. function show() {
  72. // 事件绑定者
  73. // 事件主体
  74. let ul = event.currentTarget;
  75. let li = event.target;
  76. //将原高亮的标签去掉active,并把当前的标签设置为active
  77. [...ul.children].forEach((li) => li.classList.remove('active'));
  78. li.classList.add('active');
  79. //根据标签的data-index进行查询,获取与它对应的列表
  80. content = document.querySelectorAll('.content');
  81. content.forEach((li) => li.classList.remove('active'));
  82. [...content]
  83. // 用find进行简化, find就是获取filter结果数组中的第一个
  84. .find((ul) => ul.dataset.index === li.dataset.index)
  85. .classList.add('active');
  86. }
  87. </script>
  88. </body>
  89. </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: aliceblue;
  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. <input
  63. type="checkbox"
  64. class="check-all"
  65. name="check-all"
  66. onchange="checkAll()"
  67. checked
  68. />
  69. <label for="check-all">全选</label>
  70. </div>
  71. <ul class="list">
  72. <li>
  73. <span>选择</span><span>品名</span><span>数量</span><span>单价</span
  74. ><span>金额</span>
  75. </li>
  76. <li>
  77. <input type="checkbox" onchange="checkItems()" checked />
  78. <span class="content">手机</span>
  79. <input type="number" value="1" min="1" class="num" />
  80. <span class="price">100</span>
  81. <span class="amount">0</span>
  82. </li>
  83. <li>
  84. <input type="checkbox" onchange="checkItems()" checked />
  85. <span class="content">电脑</span>
  86. <input type="number" value="1" min="1" class="num" />
  87. <span class="price">200</span><span class="amount">0</span>
  88. </li>
  89. <li>
  90. <input type="checkbox" onchange="checkItems()" checked />
  91. <span class="content">相机</span>
  92. <input type="number" value="1" min="1" class="num" />
  93. <span class="price">300</span>
  94. <span class="amount">0</span>
  95. </li>
  96. <li>
  97. <span>总计:</span>
  98. <span class="total-num">0</span>
  99. <span class="total-amount">0</span>
  100. </li>
  101. </ul>
  102. <button class="account">结算</button>
  103. </div>
  104. <script>
  105. //全选
  106. function checkAll() {
  107. //全选按钮状态
  108. let status = event.target.checked;
  109. //子商品状态
  110. document
  111. .querySelectorAll(".list li input[type='checkbox']")
  112. .forEach((item) => (item.checked = status));
  113. autoCalculate();
  114. }
  115. function checkItems() {
  116. //全部商品
  117. let items = document.querySelectorAll(
  118. ".list li input[type='checkbox']"
  119. );
  120. //判断状态
  121. let status = [...items].every((item) => item.checked === true);
  122. document.querySelector('.check-all').checked = status;
  123. autoCalculate();
  124. }
  125. const nums = document.querySelectorAll('.num');
  126. //商品是否被选中
  127. function goodStatus(numArr) {
  128. let items = document.querySelectorAll(
  129. ".list li input[type='checkbox']"
  130. );
  131. return numArr.map((num, index) => {
  132. if (items[index].checked === false) {
  133. return (num = 0);
  134. } else {
  135. return num;
  136. }
  137. });
  138. }
  139. //计算总数量
  140. function getTotalNum(numArr) {
  141. numArr = goodStatus(numArr);
  142. return numArr.reduce((acc, cur) => acc + cur);
  143. }
  144. //计算每个商品金额
  145. function getAmount(numArr, priceArr) {
  146. return numArr.map((num, index) => num * priceArr[index]);
  147. }
  148. //总金额
  149. function getTotalAmount(amountArr) {
  150. amountArr = goodStatus(amountArr);
  151. return amountArr.reduce((acc, cur) => acc + cur);
  152. }
  153. //自动计算
  154. function autoCalculate() {
  155. //数量数组
  156. const numArr = [...nums].map((num) => parseInt(num.value));
  157. //单价数组
  158. const prices = document.querySelectorAll('.price');
  159. const pricesArr = [...prices].map((price) =>
  160. parseInt(price.textContent)
  161. );
  162. //金额数组
  163. const amountArr = getAmount(numArr, pricesArr);
  164. document.querySelector('.total-num').textContent = getTotalNum(numArr);
  165. document.querySelector('.total-amount').textContent =
  166. getTotalAmount(amountArr);
  167. //为每个商品填充金额
  168. document
  169. .querySelectorAll('.amount')
  170. .forEach((amount, index) => (amount.textContent = amountArr[index]));
  171. }
  172. //自动加载
  173. window.onload = autoCalculate();
  174. //数量变化自动计算
  175. [...nums].forEach((num) => (num.onchange = autoCalculate));
  176. </script>
  177. </body>
  178. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!