博客列表 >双色球, 选项卡、购物车

双色球, 选项卡、购物车

Blackeye
Blackeye原创
2022年04月09日 18:18:53435浏览

part1

hw1

part2

hw2

part3

hw3

  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. </body>
  31. <script>
  32. // 1. 生成6红球ballListArr
  33. // 临时存放1~33数组
  34. let temp = [];
  35. // 结果数组
  36. let ballListArr = [];
  37. // 初始化数组
  38. for(let i=1;i<=33;i++){
  39. temp[i-1] = i;
  40. }
  41. // 设置红球
  42. for(let i=0;i<6;i++){
  43. let index = Math.floor(Math.random()*temp.length);
  44. ballListArr.push(temp[index]);
  45. // 移除index元素,规避重复
  46. temp.splice(index,1);
  47. }
  48. // 2. ballListArr排序
  49. ballListArr.sort((a,b)=>a-b);
  50. // 3. 加入一个蓝色球在ballListArr中
  51. let blue = Math.floor(Math.random()*temp.length+1);
  52. ballListArr.push(blue);
  53. // 4. 将ballListArr渲染至页面中
  54. const box = document.querySelector(".box");
  55. // ballListArr.forEach(item=>{
  56. // const div = document.createElement("div");
  57. // div.textContent = item;
  58. // box.append(div);
  59. // });
  60. for(let i=0;i<ballListArr.length;i++){
  61. const div = document.createElement("div");
  62. div.innerHTML = ballListArr[i];
  63. box.append(div);
  64. }
  65. </script>
  66. </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. </body>
  76. <script>
  77. function show() {
  78. // 1.清理active class
  79. // 获取标签NodeList
  80. const Lilist = event.currentTarget.children;
  81. [...Lilist].forEach((item) => {
  82. item.classList.remove("active");
  83. });
  84. // 获取内容NodeList
  85. const contentList = document.querySelectorAll(".content");
  86. contentList.forEach((item) => {
  87. item.classList.remove("active");
  88. });
  89. // 2.添加active class
  90. // 实际点击元素
  91. const clickOption = event.target;
  92. const index = clickOption.dataset.index;
  93. clickOption.classList.add("active");
  94. [...contentList].find(item=>index===item.dataset.index).classList.add("active");
  95. }
  96. </script>
  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
  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. </body>
  106. <script>
  107. // 全局全选元素
  108. let selectAll = true;
  109. // 获取items元素
  110. const itemList = document.querySelectorAll(
  111. ".list li input[type='checkbox']"
  112. );
  113. // 获取全局numArr
  114. const numArr = document.querySelectorAll(".num");
  115. // 1.用户全选设置
  116. function checkAll() {
  117. // 修改全选元素
  118. selectAll = event.target.checked;
  119. // 设置items选中状态
  120. [...itemList].forEach((item) => (item.checked = selectAll));
  121. autoCalculate();
  122. }
  123. // 2.商品影响全选属性
  124. function checkItems() {
  125. event.preventDefault();
  126. event.stopPropagation();
  127. event.target.checked = event.target.checked;
  128. const out = [...document
  129. .querySelectorAll(".list li input[type='checkbox']")]
  130. .every(item => item.checked === true);
  131. if(out === false){
  132. document.querySelector(".check-all").checked = false;
  133. }else{
  134. document.querySelector(".check-all").checked = true;
  135. }
  136. autoCalculate();
  137. // 选中触发事件;
  138. }
  139. // console.log(itemList.forEach(item=>console.log(item.checked)));
  140. // 转换Helper
  141. function getReatil(arr) {
  142. return [...arr].map((item) => {
  143. if (item.value === undefined) {
  144. return parseInt(item.textContent);
  145. }
  146. return parseInt(item.value);
  147. });
  148. }
  149. // 将为选中的内容置换为0
  150. function getStatus(num, ck = 0) {
  151. if (ck === 1) {
  152. return num.map((item, index) => {
  153. if (itemList[index].checked === false) {
  154. return (item = 0);
  155. } else {
  156. return (item = item);
  157. }
  158. });
  159. }
  160. return getReatil(num).map((item, index) => {
  161. if (itemList[index].checked === false) {
  162. return (item = 0);
  163. } else {
  164. return (item = item);
  165. }
  166. });
  167. }
  168. // 计算金额
  169. function getItemPrice(num, price) {
  170. price = getReatil(price);
  171. return getReatil(num).map((item, index) => item * price[index]);
  172. }
  173. // 总计:总数量
  174. function getTotalNums(num) {
  175. return getStatus(num).reduce((acc, index) => acc + index);
  176. }
  177. // 总计:总金额
  178. function getTotalPrice(prices) {
  179. return getStatus(prices, 1).reduce((acc, index) => acc + index);
  180. }
  181. // 计算及填充
  182. function autoCalculate() {
  183. // 给number增加事件
  184. document
  185. .querySelectorAll(".num")
  186. .forEach((item) => (item.onchange = autoCalculate));
  187. const prices = getItemPrice(
  188. document.querySelectorAll(".num"),
  189. document.querySelectorAll(".price")
  190. );
  191. const TotalNum = getTotalNums(document.querySelectorAll(".num"));
  192. const TotalPrice = getTotalPrice(prices);
  193. //金额填充
  194. const amout = document.querySelectorAll(".amount");
  195. amout.forEach((item, index) => (item.innerHTML = prices[index]));
  196. //总计:总数量填充
  197. document.querySelector(".total-num").innerHTML = TotalNum;
  198. //总计:总金额填充
  199. document.querySelector(".total-amount").innerHTML = TotalPrice;
  200. }
  201. window.onload = autoCalculate;
  202. </script>
  203. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议