angularjs实现的购物车效果代码实例

分享一段代码实例,它实现了购物车效果,能够实时计算商品的总价格。

并且选中产品的时候,有相关样式的变化,代码实例如下:

 
 
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
<!doctype html>
< html >
< head >
< meta  charset = "utf-8" >
< title >购物车效果</ title >
< script  type = "text/javascript"  src = "http://apps.bdimg.com/libs/angular.js/1.4.6/angular.js" ></ script >
< style  type = "text/css" >
* {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
}
html {
   font-size: 62.6%;
   font-family: "Microsoft Yahei",sans-serif;
   line-height: 1;
}
a {
   text-decoration: none;
   color: grey;
}
ul, li {
   list-style: none;
}
.checkbox {
   vertical-align: middle;
   display: inline-block;
   width: 20px;
   height: 20px;
   position: relative;
}
.checkbox .box {
   border-radius: 4px;
   position: absolute;
   display: block;
   width: 20px;
   height: 20px;
   border: 2px solid #ccc;
   transition: all 0.5s;
}
.checkbox input[type="checkbox"] {
   position: absolute;
   z-index: 10;
   opacity: 0;
}
.checkbox input:checked + .box {
   border-color: red;
}
.checkbox input:checked + .box::after {
   position: absolute;
   display: block;
   content: ' ';
   width: 10px;
   height: 10px;
   background-color: red;
   top: 3px;
   left: 3px;
}
body {
   font-size: 1.4rem;
}
.wrap {
   margin-top: 3rem;
}
.wrap ul {
   display: block;
   width: 100%;
   margin-bottom: 3rem;
}
.wrap li {
   margin: 1rem auto;
   text-align: center;
   width: 20rem;
   height: 16rem;
   padding: 1rem;
   border: 0.4rem solid #ccc;
   border-radius: 0.8rem;
   transition: all 0.5s;
}
.wrap li.active {
   border-color: rgb(218, 30, 30);
}
.contentW {
   font-size: 1.6rem;
   height: 3.2rem;
   text-align: left;
}
.buttonW a {
   display: inline-block;
   padding: 0.8rem 0.6rem;
   background-color: rgb(218, 30, 30);
   color: #fff;
   font-size: 1.6rem;
   border-radius: 0.4rem;
}
.buttonW .addc {
   vertical-align: middle;
   display: inline-block;
}
a.calc {
   display: inline-block;
   vertical-align: middle;
   border: 1px solid #999;
   width: 22px;
   height: 22px;
   text-align: center;
}
.contentW span {
   display: inline-block;
   vertical-align: middle;
}
.buttonW a.cancel {
   background-color: #FFE47C;
}
.price {
   display: table;
   margin: 0 auto;
}
.price p {
   font-size: 1.8rem;
}
.price a {
   display: inline-block;
   background: rgb(218,30,30);
   padding: 0.8rem 2rem;
   margin-left: 1rem;
   color: #fff;
   border-radius: 0.8rem;
}
a.nobuy {
   border-color: #eee;
   background-color: #ccc;
}
</ style >
</ head >
< body >
   < div  class = "container"  ng-app = "cartApp"  ng-controller = "cartCtrl" >
     < div  class = "wrap" >
       < ul >
         < li  class = "{{fruit.buy?'active':''}}"  ng-repeat = "fruit in fruits"  item={{fruit.id}}>
           < p  class = "contentW" >
             名称:{{fruit.name}}
           </ p >
           < p  class = "contentW" >
             单价:{{fruit.price}}
           </ p >
           < p  class = "contentW" >
             数量:
             < a  href = "javascript:;"  class = "calc {{fruit.num<=1?'nobuy':''}}"  ng-click = "overdue(fruit.id)" >-</ a >
             < span >{{fruit.num}}</ span >
             < a  href = "javascript:;"  class="calc {{fruit.num>=10?'nobuy':''}}" ng-click="add(fruit.id)">+</ a >
           </ p >
           < div  class = "buttonW" >
             < div  class = "checkbox" >
               < input  type = "checkbox"  id = "{{fruit.id}}"  ng-model = "fruit.buy" >
               < span  class = "box" ></ span >
             </ div >
             < label  class = "addc"  for = "{{fruit.id}}" >添加到购物车</ label >
           </ div >
         </ li >
  
       </ ul >
       < div  class = "price" >
         < p >
           总价:{{totalPrice()}}
  
           < a  href = "javascript:;" >去结算</ a >
         </ p >
       </ div >
     </ div >
   </ div >
< script  type = "text/javascript" >
var fruits = [{
   id: 'fruit1',
   name: "苹果",
   price: "10",
   num: 1,
   buy: false
}, {
   id: 'fruit2',
   name: "香蕉",
   price: "1",
   num: 1,
   buy: false
}, {
   id: 'fruit3',
   name: "橘子",
   price: "20",
   num: 1,
   buy: false
}];
  
angular.module('cartApp', [])
   .controller('cartCtrl', function($scope) {
     //取出水果
     $scope.fruits = fruits;
  
     //点击-事件
  
  
     $scope.overdue = function(id) {
       angular.forEach($scope.fruits, function(value, key) {
         if (value.id == id && value.num > 1) {
           $scope.fruits[key].num--;
         }
       });
     }
  
     $scope.add = function(id) {
       angular.forEach($scope.fruits, function(value, key) {
         if (value.id == id && value.num < 10) {
           $scope.fruits[key].num++;
         }
       });
     }
  
     $scope.totalPrice = function() {
       $scope.total = 0;
       angular.forEach($scope.fruits, function(value, key) {
         if (value.buy) {
           $scope.total = $scope.total + value.price * value.num;
         }
       });
       console.log($scope.total)
       return $scope.total;
     }
   });
</ script >
</ body >
</ html >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值