本文实例为大家分享了IONIC购物车的具体代码,供大家参考,具体内容如下

HTML
<div ng-app="app">
<div class="l-header">
<div class="l-cart">
<div class="l-cart-count" ng-click="showCart = !showCart">{{ calculateTotalProducts() }}</div>
<div class="l-cart-items" ng-show="showCart">
<div ng-show="cart.length">
<ul>
<li ng-repeat="item in cart">
<div class="l-cart-item-name">{{ item.product.name }}</div>
<div class="l-cart-item-quantity">
<input type="number" ng-model="item.quantity" ng-change="removeIfZero(item)" />
</div>
<div class="l-cart-item-subtotal">{{ item.quantity * item.product.price | currency }}</div>
<div class="remove-item">
<img src="https://cdn3.iconfinder.com/data/icons/cleaning-icons/512/Trash_Can-512.png" ng-click="removeFromCart(item)">
</div>
</li>
</ul>
<div class="l-cart-total">total <b>{{ calculateTotalPrice() | currency }}</b></div>
</div>
<div class="l-cart-empty" ng-hide="cart.length">tu carrito está vacío</div>
</div>
</div>
</div>
<ul class="l-stock">
<li class="l-product" ng-repeat="product in stock" ng-click="addToCart(product)" ng-class="{'is-on-cart': isProductOnCart(product)}">
<div class="l-product-name">{{ product.name }}</div>
<div class="l-product-price">{{ product.price | currency }}</div>
</li>
</ul>
</div>
CSS:
body color #333 padding 60px 10px 10px 10px font-family Arial, Helvetica, sans-serif user-select none .is-red color red !important .l-header display flex justify-content flex-end align-items center position fixed top 0 right 0 left 0 height 30px padding 10px background-color #2c3e50 .l-cart position relative .l-cart-count font-size 12px font-weight 700 width 30px line-height 30px text-align center color #ecf0f1 background-color #27ae60 border-radius 50% cursor pointer .l-cart-items position absolute top 100% right 0 width 270px margin 10px -10px 0 0 padding 10px font-size 12px background-color #ecf0f1 &:before content "" position absolute bottom 100% right 15px margin 0 0 -2px 0 border 10px solid transparent border-bottom-color #ecf0f1 li display flex align-items center padding-bottom 10px margin-bottom 10px .l-cart-item-name flex 1 overflow hidden white-space nowrap text-overflow ellipsis .l-cart-item-quantity width 30px margin 0 10px input display block border none padding 5px margin 0 width 100% text-align right appearance none &:focus outline none background-color #ffc &::-webkit-outer-spin-button, &::-webkit-inner-spin-button -webkit-appearance none margin 0 .l-cart-item-subtotal color #000 width 70px text-align right .remove-item img width:30px height:30px margin 0 10px text-align center .l-cart-total margin-top 10 padding-top 10px text-align right border-top 1px solid #bdc3c7 b font-weight 700 font-size 1.4em .l-cart-empty text-align center font-size 1.4em color #95a5a6 .l-stock & > li float left margin 0 10px 10px 0 &:after content "" clear both .l-product display flex color #fff cursor pointer & > div padding 10px .l-product-name background-color #2980b9 .l-product-price background-color #3498db .is-on-cart .l-product-name background-color #27ae60 .l-product-price background-color #2ecc71
JS
/**
* Esta función genera productos aleatoriamente
* (http://marak.com/faker.js/)
**/
function fetchStock () {
var i = 0,
stock = [],
total = faker.random.number({min: 10, max: 30});
for (i = 0; i < total; i++) {
stock.push({
name : faker.commerce.productName(),
price: faker.random.number({min: 1, max: 500})
});
}
return stock;
};
/**
* Aquí empieza nuestro código Angular...
**/
var app = angular.module('app', []);
app.run(function ($rootScope) {
var cart = [],
stock = fetchStock();
var addToCart = function (product) {
var item = cart.find(function (item) {
return item.product === product;
});
if (item) {
item.quantity++;
} else {
cart.push({
product : product,
quantity: 1
});
}
};
var removeFromCart = function (item) {
var index = cart.indexOf(item);
cart.splice(index, 1);
};
var removeIfZero = function (item) {
if (item.quantity < 1) {
removeFromCart(item);
}
};
var calculateTotalPrice = function () {
return cart.reduce(function (sum, item) {
return sum + item.quantity * item.product.price;
}, 0);
};
var calculateTotalProducts = function () {
return cart.reduce(function (sum, item) {
return sum + item.quantity;
}, 0);
};
var isProductOnCart = function (product) {
return cart.some(function (item) {
return item.product === product;
});
};
angular.extend($rootScope, {
stock: stock,
cart: cart,
addToCart: addToCart,
removeFromCart: removeFromCart,
removeIfZero: removeIfZero,
calculateTotalPrice: calculateTotalPrice,
calculateTotalProducts: calculateTotalProducts,
isProductOnCart: isProductOnCart
});
});
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# IONIC
# 购物车
# js购物车实现思路及代码(个人感觉不错)
# Javascript操纵Cookie实现购物车程序
# 简单的前端js+ajax 购物车框架(入门篇)
# js实现简单的购物车有图有代码
# 原生js模拟淘宝购物车项目实战
# 如何解决IONIC页面底部被遮住无法向上滚动问题
# ionic隐藏tabs的方法
# Ionic默认的Tabs模板使用实例
# ionic组件ion-tabs选项卡切换效果实例
# 郁闷!ionic中获取ng-model绑定的值为undefined如何解决
# 大家分享
# 具体内容
# 大家多多
# addToCart
# isProductOnCart
# stock
# est
# vac
# CSS
# padding
# color
相关文章:
建站主机数据库如何配置才能提升网站性能?
如何通过山东自助建站平台快速注册域名?
高端智能建站公司优选:品牌定制与SEO优化一站式服务
西安制作网站公司有哪些,西安货运司机用的最多的app或者网站是什么?
如何选择适配移动端的WAP自助建站平台?
制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?
用v-html解决Vue.js渲染中html标签不被解析的问题
如何通过cPanel快速搭建网站?
建站之星如何一键生成手机站?
如何通过万网虚拟主机快速搭建网站?
建站OpenVZ教程与优化策略:配置指南与性能提升
深圳网站制作平台,深圳市做网站好的公司有哪些?
建站之星代理费用多少?最新价格详情介绍
建站主机SSH密钥生成步骤及常见问题解答?
清单制作人网站有哪些,近日“兴风作浪的姑奶奶”引起很多人的关注这是什么事情?
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
Thinkphp 中 distinct 的用法解析
建站之星安装后如何配置SEO及设计样式?
建站主机如何选?性能与价格怎样平衡?
建站之星好吗?新手能否轻松上手建站?
如何在阿里云虚拟服务器快速搭建网站?
专业公司网站制作公司,用什么语言做企业网站比较好?
再谈Python中的字符串与字符编码(推荐)
简易网站制作视频教程,使用记事本编写一个简单的网页html文件?
建站之星伪静态规则如何设置?
如何在企业微信快速生成手机电脑官网?
微网站制作教程,不会写代码,不会编程,怎么样建自己的网站?
大型企业网站制作流程,做网站需要注册公司吗?
*服务器网站为何频现安全漏洞?
网站制作公司排行榜,四大门户网站排名?
如何通过西部数码建站助手快速创建专业网站?
成都网站制作报价公司,成都工业用气开户费用?
香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧
济南网站建设制作公司,室内设计网站一般都有哪些功能?
如何高效配置IIS服务器搭建网站?
香港服务器建站指南:免备案优势与SEO优化技巧全解析
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
IOS倒计时设置UIButton标题title的抖动问题
如何用5美元大硬盘VPS安全高效搭建个人网站?
如何选择美橙互联多站合一建站方案?
如何访问已购建站主机并解决登录问题?
XML的“混合内容”是什么 怎么用DTD或XSD定义
网站规划与制作是什么,电子商务网站系统规划的内容及步骤是什么?
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
微课制作网站有哪些,微课网怎么进?
大连网站设计制作招聘信息,大连投诉网站有哪些?
免费制作小说封面的网站有哪些,怎么接网站批量的封面单?
Python路径拼接规范_跨平台处理说明【指导】
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
*请认真填写需求信息,我们会在24小时内与您取得联系。