Blogger Information
Blog 22
fans 0
comment 0
visits 11503
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue写购物车
没耐心的渔翁
Original
445 people have browsed it

vue写购物车

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>购物车</title>
  7. <script src="https://unpkg.com/vue@next"></script>
  8. </head>
  9. <style>
  10. .box {
  11. width: 22em;
  12. height: 2em;
  13. }
  14. .list>li {
  15. height: 1.6em;
  16. background-color: #efefef;
  17. display: grid;
  18. grid-template-columns: repeat(5, 3em);
  19. gap: 1em;
  20. place-items: center right;
  21. border-bottom: 1px solid #ccc;
  22. }
  23. .list>li:first-of-type {
  24. background-color: lightseagreen;
  25. color: white;
  26. }
  27. .list>li:hover:not(:first-of-type) {
  28. cursor: pointer;
  29. background-color: lightcyan;
  30. }
  31. .list>li input[type='number'] {
  32. width: 3em;
  33. border: none;
  34. outline: none;
  35. text-align: center;
  36. font-size: 1em;
  37. background-color: transparent;
  38. }
  39. .list>li:last-of-type span.total-num,
  40. .list>li:last-of-type span.total-amount {
  41. grid-column: span 2;
  42. place-self: center right;
  43. color: lightseagreen;
  44. }
  45. .account {
  46. float: right;
  47. background-color: lightseagreen;
  48. color: white;
  49. border: none;
  50. outline: none;
  51. width: 4.5em;
  52. height: 1.8em;
  53. }
  54. .account:hover {
  55. background-color: coral;
  56. cursor: pointer;
  57. }
  58. </style>
  59. <body>
  60. <div class="app">
  61. <div class="box">
  62. <div class="selectAll">
  63. <!-- checkAll(): 当全选按钮的状态发生变化化,触发该事件 change -->
  64. <input type="checkbox" class="check-all" name="check-all" @change="checkAll($event)" checked />
  65. <label for="check-all">全选</label>
  66. </div>
  67. <ul class="list">
  68. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
  69. <li>
  70. <input type="checkbox" @change="checkItems($event)" checked />
  71. <span class="content">手机</span>
  72. <input type="number" min="1" v-model="number1" />
  73. <span class="price">{{price1}}</span>
  74. <span class="amount1">{{payAmount1}}</span>
  75. </li>
  76. <li>
  77. <input type="checkbox" @change="checkItems($event)" checked />
  78. <span class="content">电脑</span>
  79. <input type="number" min="1" v-model="number2" />
  80. <span class="price">{{price2}}</span>
  81. <span class="amount">{{payAmount2}}</span>
  82. </li>
  83. <li>
  84. <input type="checkbox" @change="checkItems($event)" checked />
  85. <span class="content">相机</span>
  86. <input type="number" min="1" v-model="number3" />
  87. <span class="price">{{price3}}</span>
  88. <span class="amount">{{payAmount3}}</span>
  89. </li>
  90. <li>
  91. <span>总计:</span>
  92. <span class="total-num">{{totalNum}}</span>
  93. <span class="total-amount">{{totalAmount}}<span>
  94. </li>
  95. </ul>
  96. <button class="account">结算</button>
  97. </div>
  98. </div>
  99. </body>
  100. <script>
  101. const app = Vue.createApp({
  102. data() {
  103. return {
  104. number1: 0,
  105. price1: 100,
  106. number2: 0,
  107. price2: 200,
  108. number3: 0,
  109. price3: 300,
  110. }
  111. },
  112. //计算器属性
  113. computed: {
  114. payAmount1() {
  115. return this.number1 * this.price1;
  116. },
  117. payAmount2() {
  118. return this.number2 * this.price2;
  119. },
  120. payAmount3() {
  121. return this.number3 * this.price3;
  122. },
  123. totalNum() {
  124. return this.number1 + this.number2 + this.number3;
  125. // return jieguo;
  126. },
  127. totalAmount() {
  128. return this.payAmount1 + this.payAmount2 + this.payAmount3
  129. }
  130. },
  131. //侦听器属性
  132. watch: {
  133. },
  134. methods: {
  135. //全选
  136. checkAll(ev) {
  137. let checkArr = document.querySelectorAll(
  138. '.list li input[type="checkbox"]'
  139. );
  140. let status = ev.target.checked;
  141. checkArr.forEach((item) => (item.checked = status))
  142. if (status) {
  143. this.number1 = 1;
  144. this.number2 = 1;
  145. this.number3 = 1;
  146. }else{
  147. this.number1 = 0;
  148. this.number2 = 0;
  149. this.number3 = 0;
  150. }
  151. },
  152. //单选
  153. checkItems(ev) {
  154. let checkArr = document.querySelectorAll(
  155. '.list li input[type="checkbox"]'
  156. );
  157. let check = document.querySelector(
  158. '.selectAll input'
  159. );
  160. console.log('下面的三个按钮的数组', checkArr);
  161. console.log('全选那一个按钮', check);
  162. check.checked = [...checkArr].every((item) => (item.checked));
  163. let numbers = document.querySelectorAll(
  164. '.list li input[type="number"]'
  165. );
  166. },
  167. }
  168. }).mount('.app');
  169. </script>
  170. </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