angularjs實現的購物車效果程式碼例項
分享一段程式碼例項,它實現了購物車效果,能夠實時計算商品的總價格。
並且選中產品的時候,有相關樣式的變化,程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <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>
相關文章
- Vue實現購物車效果Vue
- AngularJS 實現簡單購物車AngularJS
- angular實現購物車自動計算價格程式碼例項Angular
- 通過拖動將商品新增到購物車效果程式碼例項
- 加入購物車動畫效果實現動畫
- vue例項-購物車功能Vue
- css3實現的動物頭像效果程式碼例項CSSS3
- 點選回車實現表單提交效果程式碼例項
- jquery實現的選項卡效果例項程式碼jQuery
- canvas實現的驗證碼效果程式碼例項Canvas
- jquery實現的分頁效果例項程式碼jQuery
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- 用Provider實現商品加入購物車的動畫效果IDE動畫
- asp.net 實現購物車詳細程式碼ASP.NET
- js實現的垂直選項卡效果程式碼例項JS
- 購物車的實現原理
- javascript實現animate()動畫效果程式碼例項JavaScript動畫
- css實現矩形切角效果程式碼例項CSS
- angularJS結合canvas實現的畫圖程式碼例項AngularJSCanvas
- js小球拋物線效果程式碼例項JS
- 利用canvas實現的驗證碼效果程式碼例項Canvas
- html實現簡單ListViews效果的例項程式碼HTMLView
- css3實現的折角效果程式碼例項CSSS3
- js實現的留言本效果程式碼例項JS
- js實現的div拖動效果例項程式碼JS
- css實現的div垂直居中效果程式碼例項CSS
- javascript實現的淡入淡出效果程式碼例項JavaScript
- javascript實現的燃放煙火效果程式碼例項JavaScript
- js實現的元素抖動效果程式碼例項JS
- jquery實現的滑動軸效果程式碼例項jQuery
- canvas實現的雪花飄落效果程式碼例項Canvas
- canvas實現的藍色雨滴效果程式碼例項Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- CSS實現的柱狀圖效果例項程式碼CSS
- javascript實現的動畫效果簡單例項程式碼JavaScript動畫單例
- css實現圖片灰度效果程式碼例項CSS
- javascript模擬實現ArrayList效果程式碼例項JavaScript