Vue实现购物车 | 世外天堂

Vue实现购物车

主要实现的功能有:商品的单价,商品的总价,商品的单选和全选,商品数量的修改,商品的删除等功能。
该小型项目采用Vue框架作为主要操作DOM的手段,代码完全原创
实现效果如下:
avatar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
html {
font-size: 100px;
}

body {
font-size: 16px;
}

* {
margin: 0;
padding: 0;
}

li {
list-style-type: none;
}

#box {
margin-top: 0.4rem;
}

.shop-item {
height: 1.3rem;
display: flex;
justify-content: space-between;
align-items: center;
background: rgb(252, 252, 252);
margin-top: 0.1rem;
position: relative;
}
.shop-item:nth-last-of-type(1){
margin-bottom: 0.6rem;
}

.shop-item {
border: 0.01rem solid #e5e5e5;
}

.delete {
height: 100%;
width: 0.6rem;
color: white;
outline: none;
background: rgb(221, 0, 27);
border: none;
}

.delete:after {
content: '删除';
}

a:link {
text-decoration: none;
}

.shop-item img {
margin-left: 0.05rem;
max-height: 0.8rem;
max-width: 0.8rem;
}

.shop-item a {
color: #3c3c3c;
}

.shop-name {
font-size: 0.12rem;
overflow: hidden;
height: 0.5rem;
}

.shop-name a:hover {
color: #f40;
text-decoration: underline;
}

.size {
color: #9c9c9c;
font-size: 0.12rem;
overflow: hidden;
}

.item-main {
box-sizing: border-box;
padding: 0 0 0 0.05rem;
height: 0.8rem;
width: 1.5rem;
}

.price {
color: #3c3c3c;
font-size: 0.12rem;
font-weight: 700;
font-family: Verdana, Tahoma, arial;
}

.count {
outline: none;
box-sizing: border-box;
height: 0.25rem;
line-height: 0.15rem;
border: 0.01rem solid #aaa;
color: #343434;
width: 0.4rem;
text-align: center;
padding: 0.04rem 0.05rem;
background-color: #fff;
background-position: -0.75rem -3.75rem;
float: left;
}
.reduce,.add {
float: left;
height: 0.25rem;
width: 0.15rem;
border: 0.01rem solid #e5e5e5;
background: #f0f0f0;
text-align: center;
line-height: 0.23rem;
color: #444;
top: 0;
outline: none;
}

#countbox {
display: flex;
flex-flow: nowrap;
}

.item-sum {
text-align: center;
color: #f40;
font-size: 0.12rem;
font-weight: 700;
}

.total .item-total {
font-size: 0.12rem;
color: #f40;
font-weight: 700;
right: 100px;
}

.head {
height: 0.3rem;
background: rgb(221, 0, 27);
width: 100%;
display: flex;
justify-content: center;
align-items: center;
color: white;
position: fixed;
top: 0;
z-index: 999;
}

.foot {
z-index: 999;
height: 0.5rem;
background: #E5E5E5;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
color: white;
position: fixed;
bottom: 0;

}

.total {
display: flex;
flex-flow: nowrap;
align-items: center;
color: black;
height: 100%;
}

.head:after {
content: '购物车';
}

.footse {
align-items: center;
margin-left: 0.1rem;
display: flex;
flex-flow: nowrap;
}

.foottext {
color: black;
font-size: 14px;
white-space: nowrap;
}

.foottext:after {
content: '全选';
}

.submit {
border: none;
outline: none;
height: 100%;
width: 0.6rem;
color: white;
background: rgb(221, 0, 27);
}

.submit:after {
content: '结算';
}

.singleselect {
margin-left: 0.1rem;
}
.nodata{
display: flex;
margin: 0 auto;
height: 3rem;
width: 3rem;
background-image: url(img/nodata02.png);
background-repeat: no-repeat;
justify-content: center;
align-items: flex-end;
background-size: cover;
font-size: 14px;
color: #6d757a;
}
.totaltxt{
padding-right: 0.1rem;
}
</style>
</head>
<body>
<header>
<div class="head"></div>
</header>
<div id="box">
<div v-if="items.length">
<main>
<template v-for="(item,index) in items">
<div class="shop-item">
<input v-model="items[index].checked" type="checkbox" name="" class="singleselect" value="" />
<img :src="item.src">
<div class="item-main">
<div class="shop-name"><a href="#">{{item.name}}</a></div>
<div class="size">{{item.size}}</div>
</div>
<div class="price">¥{{item.price}}</div>
<div id="countbox">
<button type="button" @click="countReduce(index)" class="reduce">-</button>
<input v-model="item.count" type="text" name="" class="count" value="" />
<button type="button" @click="countAdd(index)" class="add">+</button>
</div>
<div class="item-sum">¥{{item.price*item.count}}</div>
<button type="button" v-on:click="bookRemove" class="delete"></button>
</div>
</template>
</main>
<footer>
<div class="foot">
<div class="footse">
<input v-model="checked" @click="all" type="checkbox" name="" id="allselect" value="" />
<label for="allselect" class="foottext"></label>
</div>
<div class="total">
<div class="totaltxt">
<span style="font-size: 14px;white-space: nowrap;">合计:</span><span class="item-total">¥{{sum}}</span>
</div>
<button type="button" class="submit"></button></div>
</div>
</footer>
</div>
<div v-else>
<main>
<div class="nodata">
<p>你还暂时没有商品哦~</p>
</div>
</main>
<footer>
<div class="foot">
<div class="footse">
</div>
</div>
</footer>
</div>
</div>
</body>
<script type="text/javascript">
var app = new Vue({
el: '#box',
data: {
keywords:'',
checked: false,
items: [{
src: 'https://img.alicdn.com/bao/uploaded/i1/2200798231128/O1CN01OpBz5w1KCfK7ZIb5g_!!2200798231128.jpg_80x80.jpg',
name: '优质舰队collection舰娘响手办响爷二次元动漫美少女机箱精品摆件',
size: '尺寸:精品现货送海报',
price: 79,
count: 1,
checked: false
},
{
src: 'https://img.alicdn.com/bao/uploaded/i3/1085315961/O1CN013r8Hhq1tuBWw3AoNp_!!0-item_pic.jpg_80x80.jpg',
name: '【年货价】Razer雷蛇黑寡妇蜘蛛竞技幻彩版V2电竞游戏87机械键盘RGB背光吃鸡',
size: '套餐类型:官方标配 颜色分类:87键(绿轴)',
price: 749,
count: 1,
checked: false
}
]
},
mounted: function() {},
methods: {
all: function() {
if (!this.checked) {
for (var i = 0; i < this.items.length; i++) {
this.items[i].checked=true;
}
}
else{
for (var i = 0; i < this.items.length; i++) {
this.items[i].checked=false;
}
}
},
countAdd: function(index) {
this.items[index].count++;

},
countReduce: function(index) {
if (this.items[index].count == 1) {
return;
}
this.items[index].count--;
},
bookRemove: function() {
for (var i = 0; i < this.items.length; i++) {
this.items.splice(i, 1);
}
},
search:function(keywords){
var newList=[];
for(var i=0;i<this.items.length;i++){
if(this.items[i].name.indexOf(keywords)!==-1){
newList.push(this.items[i])
}
}
return newList
}

},
computed: {
sum: function() {
var total = 0;
for (var i = 0; i < this.items.length; i++) {
if(this.items[i].checked==true){
total += this.items[i].price * this.items[i].count;}
}
return total;
}
}
});
</script>
</html>