今天从网上找了一个购物车的小例子,照着敲了一下,收获不少。下面的用一个小动图展示一下成果:
接下来上代码:
购物清单 全选 商品 数量 单价(元) 金额(元) 操作
|
{{item.gname}}
{{item.gbrand}} {{item.gplace}} {{item.gpurity}} {{item.gminnum}} {{item.gstore}} |
¥{{item.gprice}} | ¥{{item.gprice*item.gnum}} |
*{
padding: 0;
margin: 0;
}
html,body{
width: 100%;
overflow-x:hidden ;
}
#app{
width: 90%;
margin: 50px auto;
border: 1px solid gainsboro;
border-top: none;
}
h2{
display: block;
border-top: 4px solid dodgerblue;
font-size: 17px;
padding-left: 20px;
line-height: 50px;
color: dodgerblue;
}
.nav{
width: 100%;
height: 40px;
border:1px solid gainsboro ;
border-left:none ;
border-right: none;
}
.nav>div{
height: 100%;
float: left;
display: flex;
justify-content: center;
align-items: center;
}
.nav div:nth-child(1){
width: 15%;
}
.nav div:nth-child(2){
width:37%;
}
.nav div:nth-child(3){
width: 12%;
}
.nav div:nth-child(4){
width: 12%;
}
.nav div:nth-child(5){
width: 12%;
}
.nav div:nth-child(6){
width: 12%;
}
.noselected{
display: inline-block;
width: 17px;
height:17px;
margin-right: 5px;
background: url(../img/nocheck.png) no-repeat;
background-size: contain;
}
.goods{
width: 100%;
height: auto;
}
.goods tr{
width: 100%;
}
.goods tr td{
padding: 20px 0;
}
.goods tr>td:nth-child(1){
width: 17%;
text-align: center;
}
.goods tr>td:nth-child(2){
width: 35%;
}
.goods tr>td:nth-child(3){
width: 12%;
text-align: center;
}
.goods tr>td:nth-child(4){
width: 12%;
text-align: center;
}
.goods tr>td:nth-child(5){
width: 12%;
text-align: center;
}
.goods tr>td:nth-child(6){
width: 12%;
text-align: center;
}
.good{
width: 100%;
display: flex;
align-items: center;
}
.good img{
width:120px;
height: 120px;
float: left;
border: 2px solid gainsboro;
margin-right: 30px;
}
.good>div{
font-size: 13px;
line-height: 20px;
}
.good>div h3{
font-size: 11px;
margin-bottom: 5px;
}
.goods input[type=number]{
width: 50px;
}
.goods tr td:nth-child(4),.goods tr td:nth-child(5){
color: red;
}
button{
cursor: pointer;
border: none;
outline: none;
background-color: white;
}
.footer{
display: flex;
align-items: center;
width: 100%;
height: 50px;
background-color: #F7F7F7;
position: relative;
}
.footer button{
border: none;
background-color: #F7F7F7;
font-size: 15px;
}
.footer button:nth-child(1){
margin-left: 30px;
}
.footer button:nth-child(2){
margin-left: 60px;
}
.footer button:nth-child(4){
height: 100%;
position: absolute;
right: 0;
padding:0 20px;
background-color: orange;
}
.footer>span{
position: absolute;
right: 100px;
}
.footer>span span{
color: red;
}
.selected{
background: url(../img/check.png) no-repeat;
background-size: contain;
}
以上为所有的html和css文件代码。
【总结】
1、computed:此处用computed主要有两个作用。一是判断是否全选。如果全选则添加selected这一class,如果没有则不添加;二是计算选择的总商品数和总金额。当用户更改商品数量时,总商品数和总金额也随之改变。
2、return返回两个值:第一次接触function里边return的值是两个这种情况,这种要通过对象的属性访问方法。例如:
function add(a,b){
var sum;
var sub
return{
sum:a+b,
sub:a-b
}
}
var obj = add(5,2);
console.log(obj.sum);
console.log(obj.sub);
3、js的数组方法filter():
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
注意: filter() 不会对空数组进行检测。
注意: filter() 不会改变原始数组。
array.filter(function(currentValue,index,arr), thisValue)
- currentValue: 必须。当前元素的值
- index: 可选。当前元素的索引值
- arr:可选。当前元素属于的数组对象
- thisValue:可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,“this” 的值为 “undefined”
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



