angularJS制作的购物车功能

前端之家收集整理的这篇文章主要介绍了angularJS制作的购物车功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <!DOCTYPEhtml>
  2. <htmllang="en"ng-app="myapp">
  3. <head>
  4. <Metacharset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. body,html{width:100%;height:100%;margin:0;padding:0;white-space:nowrap;font-family:Arial,"HiraginoSansGB","微软雅黑","宋体";}
  8. ul,li,h4,p{margin:0;padding:0;}
  9. li{list-style:none;}
  10. h4{margin-bottom:6px;}
  11. p{margin-bottom:6px;}
  12. #bg{width:100%;height:100%;background:#FAFB98;display:inline-block;}
  13. #owrap{width:800px;height:460px;position:relative;left:calc(50%-400px);top:calc(50%-230px);border-radius:30px;Box-shadow:010px28px#E0D272;transition:allease0.3s;-webkit-transition:allease0.3s;-moz-transition:allease0.3s;-ms-transition:allease0.3s;background:#fff;}
  14. #oBox{width:100%;height:460px;position:relative;transition:allease0.3s;-webkit-transition:allease0.3s;-moz-transition:allease0.3s;-ms-transition:allease0.3s;opacity:1;}
  15. #oul{width:100%;overflow:auto;position:relative;display:inline-block;}
  16. #oula{width:18px;height:18px;color:#909696;line-height:18px;border:1pxsolid#909696;margin-bottom:6px;text-align:center;text-decoration:none;display:inline-block;}
  17. #oulspan{margin:05px05px;}
  18. #oullidiv{width:110px;height:22px;border-radius:6px;text-align:center;line-height:22px;font-size:14px;cursor:pointer;background:#FF6F3C;color:#fff;}
  19. #oul.li_adds{border:5pxdotted#FF6F3C;}
  20. #oul.have_adds{background:#FCDD68;}
  21. #oulli{width:130px;height:116px;padding:10px0020px;list-style:none;border:5pxdotted#DBE3E4;float:left;margin:20px20px020px;border-radius:20px;}
  22. .oall{width:200px;height:50px;line-height:50px;text-align:center;font-size:20px;letter-spacing:10px;display:inline-block;position:absolute;left:50px;bottom:30px;transition:allease0.3s;-webkit-transition:allease0.3s;-moz-transition:allease0.3s;-ms-transition:allease0.3s;}
  23. .oconfirm{width:200px;height:50px;line-height:50px;text-align:center;font-size:25px;letter-spacing:10px;background:#FF6F3C;color:#fff;display:inline-block;text-decoration:none;border-radius:25px;position:absolute;left:550px;bottom:30px;transition:allease0.3s;-webkit-transition:allease0.3s;-moz-transition:allease0.3s;-ms-transition:allease0.3s;}
  24. #items{width:100%;overflow:hidden;padding-top:20px;}
  25. #itemsli{width:calc(100%-40px);height:30px;line-height:30px;color:#677A7A;padding:020px020px;position:relative;}
  26. #itemsli.each_all{width:200px;display:inline-block;position:absolute;left:150px;}
  27. .even{background:#DBE3E4;}
  28. </style>
  29. </head>
  30. <body>
  31. <divid="bg"ng-controller="ctrler">
  32. <divid="owrap">
  33. <divid="oBox"ng-view>
  34. </div>
  35. <spanid="ospan"class="oall"ng-bind-template="总金额:{{all_cal.num}}元"></span>
  36. <ahref="#go_buy"ng-click="go_cal($event)"class="oconfirm"ng-click="go_cal()">~去结算~</a>
  37. </div>
  38. </div>
  39. <scriptsrc="http://cdn.bootcss.com/angular.js/1.3.15/angular.min.js"></script>
  40. <scriptsrc="http://cdn.bootcss.com/angular.js/1.3.15/angular-route.min.js"></script>
  41. <scripttype="text/javascript">
  42. varm1=angular.module("myapp",['ngRoute']);
  43. m1.config(['$routeProvider',function($routeProvider){
  44. $routeProvider
  45. .when("/",{
  46. template:
  47. "<ulid='oul'>\
  48. <ling-repeat='fruitinifruit'ng-controller='eachCtrl'class='`li_border`'>\
  49. <h4ng-bind='fruit.name'></h4>\
  50. <p>单价:<spanng-bind='fruit.money'></span></p>\
  51. 数量<ahref='javascript:;'ng-click='oreduce()'>-</a>\
  52. <spanng-bind='fruit.num'></span>\
  53. <ahref='javascript:;'ng-click='oadd()'>+</a>\
  54. <divclass='`have_click`'ng-click='addTo($event)'ng-bind='ocon'></div></li></ul>"
  55. })
  56. .when("/go_buy",{
  57. template:
  58. "<ulid='items'>\
  59. <ling-repeat='ofruitinnew_arr'ng-class='{even:$even?true:false}'ng-hide='ofruit.num==0'>\
  60. <spanng-bind-template='{{ofruit.name}}:{{ofruit.money}}x{{ofruit.num}}'></span>\
  61. <spanclass='each_all'ng-bind-template='************************&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ofruit.money*ofruit.num}}'></span></li></ul>"
  62. })
  63. }]);
  64.  
  65. m1.controller("ctrler",["$scope","$timeout",function($scope,$timeout){
  66. $scope.all_cal={num:0};
  67. $scope.arr_fruit=[{name:"香蕉",money:5,num:1,buy:false},{name:"苹果",money:9,{name:"雪梨",money:6,{name:"西瓜",money:10,{name:"火龙果",money:8,{name:"荔枝",money:11,{name:"榴莲",money:18,{name:"哈密瓜",buy:false}];
  68. //点击去结算
  69. $scope.ifruit=$scope.arr_fruit;
  70. $scope.oturn=true;
  71. $scope.go_cal=function(event){
  72. $scope.new_arr=[];
  73. //筛选出已加入购物车的水果
  74. for(vari=0;i<$scope.arr_fruit.length;i++){
  75. if($scope.arr_fruit[i].buy==true){
  76. $scope.new_arr.push($scope.arr_fruit[i]);
  77. }
  78. }
  79. if($scope.oturn){
  80. event.target.href="#go_buy";
  81. event.target.innerHTML="返回";
  82. setStyle(event.target,{'height':'30px','left':'calc(50%-100px)','fontSize':'18px','lineHeight':'30px'});
  83. setStyle(event.target.parentNode,{'width':'400px','borderRadius':'0','left':'calc(50%-200px)'});
  84. setStyle(event.target.parentNode.children[0],{'opacity':0});
  85. setStyle(event.target.parentNode.children[1],{'left':'calc(50%-100px)','bottom':'60px'});//不知道为什么这里不能用childNodes[1]无效
  86. $scope.oturn=false;
  87. $scope.ifruit=[];
  88. }
  89. else{
  90. event.target.href="#/";
  91. event.target.innerHTML="~去结算~";
  92. setStyle(event.target,{'height':'50px','left':'550px','fontSize':'25px','lineHeight':'50px'});
  93. setStyle(event.target.parentNode,{'width':'800px','borderRadius':'30px','left':'calc(50%-400px)'});
  94. setStyle(event.target.parentNode.children[1],{'left':'50px','bottom':'30px'});
  95. $scope.oturn=true;
  96. //设置延迟出现以防瞬移突兀
  97. $timeout(function(){
  98. $scope.ifruit=$scope.arr_fruit;
  99. },400);
  100. }
  101. };
  102. }]);
  103.  
  104. m1.controller("eachCtrl",function($scope){
  105. varoindex=$scope.$index;
  106. if($scope.arr_fruit[oindex].buy){
  107. $scope.li_border="li_adds";
  108. $scope.ocon="取消购买";
  109. $scope.have_click="have_adds";
  110. }
  111. else{
  112. $scope.li_border="";
  113. $scope.ocon="添加到购物车";
  114. $scope.have_click="";
  115. }
  116. //点击-号时
  117. $scope.oreduce=function(){
  118. if($scope.arr_fruit[oindex].num==1){
  119. return;
  120. }
  121. $scope.arr_fruit[oindex].num=$scope.arr_fruit[oindex].num-1;
  122. if($scope.arr_fruit[oindex].buy){
  123. $scope.all_cal.num=$scope.all_cal.num-$scope.fruit.money;
  124. }
  125. };
  126. //点击+号时
  127. $scope.oadd=function(){
  128. if($scope.arr_fruit[oindex].num==99){
  129. return;
  130. }
  131. $scope.arr_fruit[oindex].num=$scope.arr_fruit[oindex].num+1;
  132. if($scope.arr_fruit[oindex].buy){
  133. $scope.all_cal.num=$scope.all_cal.num+$scope.fruit.money;
  134. }
  135. };
  136. //点击添加到购物车时
  137. $scope.addTo=function(event){
  138. if($scope.arr_fruit[oindex].buy){
  139. $scope.ocon="添加到购物车";
  140. $scope.all_cal.num=$scope.all_cal.num-$scope.fruit.money*$scope.arr_fruit[oindex].num;
  141. $scope.arr_fruit[oindex].buy=false;
  142. $scope.li_border="";
  143. $scope.have_click="";
  144. }
  145. else{
  146. $scope.ocon="取消购买";
  147. $scope.all_cal.num=$scope.all_cal.num+$scope.fruit.money*$scope.arr_fruit[oindex].num;
  148. $scope.arr_fruit[oindex].buy=true;
  149. $scope.li_border="li_adds";
  150. $scope.have_click="have_adds";
  151. }
  152. };
  153. }]);
  154. functionsetStyle(obj,json){
  155. for(attrinjson){
  156. obj.style[attr]=json[attr];
  157. }
  158. }
  159. </script>
  160. </body>
  161. </html>

猜你在找的Angularjs相关文章