博客列表 >javascript选项卡,轮播图与购物车的实现

javascript选项卡,轮播图与购物车的实现

moon
moon原创
2022年01月11日 17:56:45686浏览

选项卡实现

  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>Document</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. <ul class="menu" onclick="show()">
  44. <li data-index="1" class="active">本省</li>
  45. <li data-index="2" >全国</li>
  46. <li data-index="3" >防疫</lifang>
  47. </ul>
  48. <div 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. </div>
  55. <div 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. </div>
  62. <div 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. </div>
  69. </div>
  70. <script>
  71. function show(){
  72. console.log(event);
  73. let target=event.target;
  74. console.log(target);
  75. let menu=document.querySelectorAll(".menu li");
  76. let content=document.querySelectorAll(".content");
  77. console.log(menu);
  78. console.log(content);
  79. menuarray=[...menu];
  80. contentarray=[...content];
  81. console.log(menuarray);
  82. menuarray.forEach(li => {
  83. li.classList.remove("active");
  84. });
  85. contentarray.forEach(ele => ele.classList.remove("active"));
  86. target.classList.add("active");
  87. contentarray.forEach((li,index)=>{
  88. if (li.dataset.index===target.dataset.index)
  89. {
  90. li.classList.add("active");
  91. }
  92. })
  93. }
  94. </script>
  95. </body>
  96. </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>Document</title>
  8. <style>
  9. /* ! 3. 轮播图 */
  10. .slider {
  11. max-width: 750px;
  12. min-width: 320px;
  13. margin: auto;
  14. padding: 0 10px;
  15. }
  16. .slider .imgs {
  17. /* 图片容器必须要有高度,否则下面图片不能正常显示 */
  18. height: 150px;
  19. }
  20. .slider .imgs img {
  21. /* 图片完全充满父级空间显示 */
  22. height: 100%;
  23. width: 100%;
  24. /* 图片带有圆角 */
  25. border-radius: 10px;
  26. /* 默认图片全部隐藏,只有有active的图片才显示 */
  27. display: none;
  28. }
  29. /* 默认显示第一张 */
  30. .slider .imgs img.active {
  31. display: block;
  32. }
  33. /* 轮播图按钮组 */
  34. .slider .btns {
  35. /* 按钮水平一排显示,用flex,且水平居中 */
  36. display: flex;
  37. place-content: center;
  38. }
  39. .slider .btns span {
  40. /* 按钮宽高相同,确定显示成一个正圆 */
  41. width: 8px;
  42. height: 8px;
  43. /* 加上红色背景和数字是为了布局时可以看到,一会更去掉 */
  44. background-color: rgba(255, 255, 255, 0.4);
  45. /* 50%可确保显示为正圆 */
  46. border-radius: 50%;
  47. /* 按钮上外边距负值,可将它上移,可移动到图片中下方 */
  48. margin: -12px 3px 5px;
  49. }
  50. .slider .btns span.active {
  51. background-color: #fff;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <div class="slider">
  57. <!-- 图片容器 -->
  58. <div class="imgs">
  59. <!-- 轮播图默认从第一张开始显示 -->
  60. <a href=""><img src="./images/banner1.jpg" alt="" data-index="1" class="active" /></a>
  61. <a href=""><img src="./images/banner2.jpg" alt="" data-index="2" /></a>
  62. <a href=""><img src="./images/banner3.png" alt="" data-index="3" /></a>
  63. </div>
  64. <!-- 切换按钮数量与图片数量必须一致 -->
  65. <div class="btns" >
  66. <span data-index="1" class="active" onclick="setActive()"></span>
  67. <span data-index="2" onclick="setActive()"></span>
  68. <span data-index="3" onclick="setActive()"></span>
  69. </div>
  70. </div>
  71. <script>
  72. let btns=document.querySelectorAll(".btns span");
  73. let imgs=document.querySelectorAll(".imgs a img");
  74. function setActive(){
  75. console.log(event.target);
  76. console.log(btns);
  77. console.log(imgs);
  78. let btnsarray=[...btns];
  79. let imgsarray=[...imgs];
  80. btnsarray.forEach(element => {
  81. element.classList.remove("active");
  82. });
  83. imgsarray.forEach(ele=>ele.classList.remove("active"));
  84. event.target.classList.add("active");
  85. imgsarray.forEach(ele=>{
  86. if (ele.dataset.index===event.target.dataset.index)
  87. {
  88. ele.classList.add("active");
  89. }
  90. });
  91. }
  92. // setInterval支持第3个参数,就是传给回调的参数
  93. console.log(btns);
  94. console.log(Object.keys(btns));
  95. setInterval(
  96. function (arr) {
  97. // 使这个数组首尾相接实现无限循环效果
  98. let i = arr.shift();
  99. console.log(i);
  100. btns[i].dispatchEvent(new Event("click"));
  101. arr.push(i);
  102. },
  103. 2000,
  104. Object.keys(btns)
  105. );
  106. </script>
  107. </body>
  108. </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>实战2: 购物车</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. <input type="checkbox" class="check-all" name="check-all" onchange="checkAll()" checked />
  63. <label for="check-all">全选</label>
  64. </div>
  65. <ul class="list">
  66. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
  67. <li>
  68. <input data-index="1" type="checkbox" onchange="checkItems()" checked />
  69. <span class="content">手机</span>
  70. <input data-index="1" type="number" value="1" min="1" class="num" />
  71. <span data-index="1" class="price">100</span>
  72. <span data-index="1" class="amount">0</span>
  73. </li>
  74. <li>
  75. <input data-index="2" type="checkbox" onchange="checkItems()" checked />
  76. <span class="content">电脑</span>
  77. <input data-index="2" type="number" value="2" min="1" class="num" />
  78. <span data-index="2" class="price">200</span><span data-index="2" class="amount">0</span>
  79. </li>
  80. <li>
  81. <input data-index="3" type="checkbox" onchange="checkItems()" checked />
  82. <span class="content">相机</span>
  83. <input data-index="3" type="number" value="3" min="1" class="num" />
  84. <span data-index="3" class="price">300</span>
  85. <span data-index="3" class="amount">0</span>
  86. </li>
  87. <li>
  88. <span>总计:</span>
  89. <span class="total-num">0</span>
  90. <span class="total-amount">0</span>
  91. </li>
  92. </ul>
  93. <button class="account">结算</button>
  94. </div>
  95. <script>
  96. // 1. 全选
  97. function checkAll() {
  98. // 1. 获取当前全选框的状态
  99. let status = event.target.checked;
  100. // 2. 根据这个状态,来动态的设置所有商品的状态
  101. const items = document.querySelectorAll(".list li input[type=checkbox]");
  102. items.forEach(item => (item.checked = status));
  103. }
  104. // 2. 根据用户选择的每个商品状态来动态设置全选状态
  105. function checkItems() {
  106. // 1. 获取全部的商品
  107. const items = document.querySelectorAll(".list li input[type=checkbox]");
  108. // 2. 判断状态,只有全部为true,才返回true, array.every
  109. let status = [...items].every(item => item.checked === true);
  110. // 3. 将这个状态添加到全选上
  111. document.querySelector(".check-all").checked = status;
  112. }
  113. // ------------ 商品的自动计算 -------------------
  114. // 购物车所有的数据计算的依据是: 基于商品的"数量"的变化
  115. const nums = document.querySelectorAll(".num");
  116. const checkbox=document.querySelectorAll(".list li input[type=checkbox]");
  117. // 1. 计算总数量
  118. function getTotalNum(numArr) {
  119. return numArr.reduce((acc, cur) => acc + cur);
  120. }
  121. // 2. 计算每个商品的金额
  122. function getAmount(numArr, priceArr) {
  123. // 金额 = 数量 * 单价
  124. return numArr.map((num, index) => num * priceArr[index]);
  125. }
  126. // 3. 计算总金额
  127. function getTotalAmount(amountArr) {
  128. return amountArr.reduce((acc, cur) => acc + cur);
  129. }
  130. // 4. 自动计算
  131. function autoCalculate() {
  132. const numArr = [...nums].map(num => parseInt(num.value));
  133. console.log(numArr);
  134. // 单价数组
  135. const prices = document.querySelectorAll(".price");
  136. const priceArr = [...prices].map(price => parseInt(price.textContent));
  137. // 金额数组
  138. const amountArr = getAmount(numArr, priceArr);
  139. console.log(amountArr);
  140. // 1. 总数量
  141. let allnum,allmoney;
  142. allnum=getTotalNum(numArr);
  143. allmoney= getTotalAmount(amountArr);
  144. // 数量数组
  145. checkboxarray=[...checkbox];
  146. console.log(checkboxarray);
  147. console.log("开始遍历");
  148. checkboxarray.map(
  149. function (ele){
  150. console.log(ele.checked);
  151. if (ele.checked==false)
  152. {
  153. [...nums].forEach(function (num){
  154. if (num.dataset.index==ele.dataset.index)
  155. {
  156. allnum=allnum-num.value;
  157. allmoney=allmoney-amountArr[parseInt(num.dataset.index)-1];
  158. console.log("所有数量:"+allnum+"当前减去:"+num.value+"当前总价格:"+allmoney);
  159. }
  160. });
  161. }
  162. }
  163. )
  164. // arr.filter(item => item >= 3)[0] -> find
  165. document.querySelectorAll(".amount").forEach((amount, index) => (amount.textContent = amountArr[index]));
  166. document.querySelector(".total-num").textContent = allnum;
  167. document.querySelector(".total-amount").textContent = allmoney;
  168. }
  169. // 当购物车加载时,初始化,要自动计算
  170. window.onload = autoCalculate;
  171. // 当数量更新时,触发自动计算
  172. nums.forEach(num => (num.onchange = autoCalculate));
  173. checkbox.forEach(ele=>(ele.onchange=autoCalculate));
  174. </script>
  175. </body>
  176. </html>

上述代码实现了简易的购物车:只计算选中的商品, 未选中商品的数量和金额自动从总计中减去

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