博客列表 >双色球、购物车js实战

双色球、购物车js实战

P粉317509817
P粉317509817原创
2022年04月11日 15:40:03365浏览

双色球

代码

  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. .winning-box{
  10. height: 80px;
  11. display: none;
  12. grid-template-columns: repeat(7,50px);
  13. gap:20px;
  14. margin-top: 20px;
  15. place-content: center;
  16. place-items: center;
  17. }
  18. .winning-box .redNum{
  19. width: 50px;
  20. height: 50px;
  21. border-radius: 50%;
  22. display: grid;
  23. place-items: center;
  24. background-color: #DC1E0F;
  25. padding: auto;
  26. box-shadow: 3px 3px 2px gray;
  27. }
  28. .winning-box .blueNum{
  29. width: 50px;
  30. height: 50px;
  31. border-radius: 50%;
  32. display: grid;
  33. place-items: center;
  34. background-color: #2C8EFF;
  35. padding: auto;
  36. box-shadow: 3px 3px 2px gray;
  37. }
  38. .winning-box .redNum:hover,
  39. .winning-box .blueNum:hover{
  40. cursor: pointer;
  41. width: 60px;
  42. height: 60px;
  43. transition: 0.5s;
  44. }
  45. .form{
  46. display: flex;
  47. gap: 20px;
  48. place-content: center;
  49. }
  50. .form>li{
  51. list-style: none;
  52. }
  53. span{
  54. display: flex;
  55. margin-top: 20px;
  56. place-content: center ;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div>
  62. <div class="winning-box">
  63. </div>
  64. </div>
  65. <span class="s">请输入您要选的号码</span>
  66. <ul class="form">
  67. <li><input type="number" name="" id="" class="number" max="33" min="0"></li>
  68. <li><input type="number" name="" id="" class="number" max="33" min="0"></li>
  69. <li><input type="number" name="" id="" class="number" max="33" min="0"></li>
  70. <li><input type="number" name="" id="" class="number" max="33" min="0"></li>
  71. <li><input type="number" name="" id="" class="number" max="33" min="0"></li>
  72. <li><input type="number" name="" id="" class="number" max="33" min="0"></li>
  73. <li><input type="number" name="" id="" class="number" max="16" min="0"></li>
  74. <button onclick="getDate()">Submit</button>
  75. </ul>
  76. <script>
  77. // 红球数组
  78. let red = [];
  79. // 蓝球数组
  80. let blue = [];
  81. // 中奖数组
  82. let Award_Winning = [];
  83. // 生成1-33的红球数组
  84. for(let a=1;a<=33;a++){
  85. red.push(a);
  86. }
  87. // console.log(red);
  88. // 生成1-16的蓝球数组
  89. for(let a = 1;a<=16;a++){
  90. blue.push(a);
  91. }
  92. // console.log(blue);
  93. // 随机取红球
  94. for (let a = 0;a < 6;a++){
  95. let index = Math.floor(Math.random()*red.length);
  96. Award_Winning.push(red[index]);
  97. red.splice(index,1);
  98. }
  99. Award_Winning.sort((a,b)=> a-b);
  100. let index = Math.floor(Math.random()*blue.length);
  101. Award_Winning.push(blue[index]);
  102. console.log(Award_Winning);
  103. const winning = document.querySelector('.winning-box');
  104. Award_Winning.forEach((item,index)=>{
  105. if(index<=5){
  106. let div = document.createElement('div');
  107. div.className='redNum';
  108. div.textContent=Award_Winning[index];
  109. winning.append(div);
  110. }else {
  111. let div = document.createElement('div');
  112. div.className='blueNum';
  113. div.textContent=Award_Winning[index];
  114. winning.append(div);
  115. }
  116. });
  117. const ul =document.querySelector('.form');
  118. const box = document.querySelector('.winning-box');
  119. function getDate(){
  120. // 用户数组
  121. let arr = [];
  122. const li =document.querySelectorAll('.number');
  123. li.forEach(a=>arr.push(parseInt(a.value)));
  124. // console.log(arr)
  125. // 对比中将数组输出是否中奖
  126. let flag=0;
  127. for (let i = 0;i<7;i++){
  128. for(let j=0;j<7;j++){
  129. if(arr[i]===Award_Winning[j]){
  130. flag++;
  131. break;
  132. // console.log(flag);
  133. }
  134. }
  135. }
  136. if(flag===7){
  137. let span=document.createElement('span');
  138. span.className='conguratulate';
  139. span.textContent='恭喜中奖';
  140. ul.insertAdjacentElement('afterend',span);
  141. box.style.display='grid';
  142. }
  143. else if(flag===0){
  144. let span=document.createElement('span');
  145. span.className='conguratulate';
  146. span.textContent=`很遗憾没有中奖!
  147. 本期中奖号码如上`;
  148. ul.insertAdjacentElement('afterend',span);
  149. box.style.display='grid';
  150. }
  151. }
  152. </script>
  153. </body>
  154. </html>

效果:

购物车

  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. function checkAll() {
  98. //全选按钮状态
  99. let status = event.target.checked;
  100. //子商品状态
  101. document
  102. .querySelectorAll(".list li input[type='checkbox']")
  103. .forEach((item) => (item.checked = status));
  104. autoCalculate();
  105. }
  106. function checkItems() {
  107. //全部商品
  108. let items = document.querySelectorAll(
  109. ".list li input[type='checkbox']"
  110. );
  111. //判断状态
  112. let status = [...items].every((item) => item.checked === true);
  113. document.querySelector(".check-all").checked = status;
  114. autoCalculate();
  115. }
  116. const nums = document.querySelectorAll(".num");
  117. //商品是否被选中
  118. function goodStatus(numArr) {
  119. let items = document.querySelectorAll(
  120. ".list li input[type='checkbox']"
  121. );
  122. return numArr.map((num, index) => {
  123. if (items[index].checked === false) {
  124. return (num = 0);
  125. } else {
  126. return num;
  127. }
  128. });
  129. }
  130. //计算总数量
  131. function getTotalNum(numArr) {
  132. numArr = goodStatus(numArr);
  133. return numArr.reduce((acc, cur) => acc + cur);
  134. }
  135. //计算每个商品金额
  136. function getAmount(numArr, priceArr) {
  137. return numArr.map((num, index) => num * priceArr[index]);
  138. }
  139. //总金额
  140. function getTotalAmount(amountArr) {
  141. amountArr = goodStatus(amountArr);
  142. return amountArr.reduce((acc, cur) => acc + cur);
  143. }
  144. //自动计算
  145. function autoCalculate() {
  146. //数量数组
  147. const numArr = [...nums].map((num) => parseInt(num.value));
  148. //单价数组
  149. const prices = document.querySelectorAll(".price");
  150. const pricesArr = [...prices].map((price) =>
  151. parseInt(price.textContent)
  152. );
  153. //金额数组
  154. const amountArr = getAmount(numArr, pricesArr);
  155. document.querySelector(".total-num").textContent = getTotalNum(numArr);
  156. document.querySelector(".total-amount").textContent =
  157. getTotalAmount(amountArr);
  158. //为每个商品填充金额
  159. document
  160. .querySelectorAll(".amount")
  161. .forEach((amount, index) => (amount.textContent = amountArr[index]));
  162. }
  163. //自动加载
  164. window.onload = autoCalculate();
  165. //数量变化自动计算
  166. [...nums].forEach((num) => (num.onchange = autoCalculate));
  167. </script>
  168. </body>
  169. </html>

效果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议