vue实现购物车小案例


Posted in Javascript onSeptember 27, 2019

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

最终效果

vue实现购物车小案例

HTML部分:

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>shopcar.html</title>
 <script src="https://cdn.jsdelivr.net/npm/vue"></script>
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
 <link rel="stylesheet" href="css/shopcar.css" >
 <script src="js/shopcar.js"></script>
</head>
<body>
 <div class="content1" id="content1">
 <div class="con1">
 <input type="checkbox" id="all" class="all">
 <p>全选</p>
 <p>商品</p>
 <p>单价</p>
 <p>数量</p>
 <p>小计</p>
 <p>操作</p>
 </div>
 <div class="con2">
 <ul class="uls1">
 <input type="checkbox" class="jingdong">
 <p class="uls1p2">京东自营</p>
 </ul>
 <ul class="uls1a">
 <p>满赠</p>
 <a href="#" >活动商品满19,即可领取商品一件></a>
 <p>查看赠品</p>
 <a href="#" >去凑单</a>
 </ul>
 <ul class="uls2" id="box">
 <input type="checkbox" class="all all1">
 <a href="#" >
 <img src="img/aaa.jpg" alt="">
 <p>OPPO R11全网通双卡双待 64G 32G<br> 手机 玫瑰金色</p>
 </a>
 <p class="weight"><i id="weight">¥2000</i></p>
 <button class="min" @click="sub(1,2000)">-</button>
 <input ref="add1" type="text" class="txt" v-model="add1">
 <button class="add" @click="add(1,2000)">+</button>
 <p class="xiaoji"><i id="xiaoji1">¥{{add2}}</i></p> 
 <p class="a666 b666">删除</p>
 <p class="a666">移到我的关注</p>
 <p class="a666">加到我的关注</p>
 </ul>
 <ul class="uls2" id="box2">
 <input type="checkbox" class="all all2">
 <a href="#" >
 <img src="img/bbb.jpg" alt="">
 <p>OPPO R11全网通双卡双待 64G 32G<br> 手机 玫瑰金色</p>
 </a>
 <p class="weight"><i id="weight">¥3000</i></p>
 <button class="min" @click="numsub(1,3000)">-</button>
 <input ref="num1" type="text" class="txt" v-model="num1">
 <button class="add" @click="numadd(1,3000)">+</button>
 <p class="xiaoji"><i id="xiaoji1">¥{{num2}}</i></p> 
 <p class="a666 b666">删除</p>
 <p class="a666">移到我的关注</p>
 <p class="a666">加到我的关注</p> 
 </ul>
 <div class="uls4">
 <div class="uls4L">
 <input type="checkbox" class="all">
 <a href="#" class="delete">删除选中商品</a>
 <a href="#" >移到我的关注</a>
 <a href="#" >清除下柜商品</a>
 </div>
 <div class="uls4R">
 <div class="uls4a">
 <div class="uls4a1">
 <div class="aaa">
 <p>已选择<i class="chose">0</i>件商品^</p>
 <p>总价:<span>¥</span><i>{{num3}}</i></p>
 </div>
 <p class="bbb">已节省:¥- 00.0</p>
 </div>
 </div>
 <div class="uls4b">
 <a href="gouwu.html" rel="external nofollow" >去结算</a>
 </div>
 </div>
 </div>
 </div>
 </div>
<div class="fixed">
 <p>京东商城</p>
 <span>您确认删除吗?</span>
 <button class="sure">确认</button>
 <button class="quxiao">取消</button>
</div>
</body>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/shopcar.js"></script>
<script type="text/javascript"> 
</script>

css部分

/* common */
body,div,dl,dt,dd,ul,li,h1,h2,h3,h4,h5,h6,code,form,input,textarea,p,th,td,fieldset,legend,figure{
 margin:0;
 padding:0;
 }
 body{
 font-family:"微软雅黑",Arial;
 }
 ul,ol{
 list-style:none;
 }
 a{
 text-decoration:none;
 }
 img{
 border:0;
 }
 
/* content1 */
.content1{
 width: 100%;
}
.con1{
 width:952px; 
 height: 36px;
 margin:0 auto;
 background: #f3f3f3;
}
.con1 input{
 float: left;
 margin-top: 10px;
}
.con1 p{
 float: left;
 font-size: 12px;
 line-height: 36px;
}
.con1 p:nth-child(3){
 margin-left: 60px;
}
.con1 p:nth-child(4){
 margin-left: 300px;
}
.con1 p:nth-child(5){
 margin-left: 93px;
}
.con1 p:nth-child(6){
 margin-left: 93px;
}
.con1 p:nth-child(7){
 margin-left: 93px;
}
.con1 p:nth-child(8){
 margin-left: 93px;
}
.con2{
 width: 952px;
 height: 364px;
 margin:0 auto;
 margin-top: 15px;
}
.con2 ul:first-child{
 width: 100%;
 height: 36px;
 border-bottom: 1px solid #ececec;
}
.uls1 input, .uls1 p, .uls1 img{
 float: left;
 margin-top: 10px;
}
.uls1{
 background: white;
}
.uls1 p{
 font-size: 12px;
}
.all{
 margin-right: 10px;
 margin-left: 20px;
}
.uls1p1{
 margin-right: 20px;
 font-weight: 600;
}
.jingdong{
 margin-left: 20px;
}
.uls1a{
 width: 100%;
 height:37px;
 border-top: 2px solid #aaaaaa;
}
.uls1a p,.uls1a a{
 float: left;
}
.uls1a p:nth-child(1){
 font-size: 14px;
 width: 35px;
 height: 20px;
 border: 1px solid #f5993c;
 text-align: center;
 line-height: 20px;
 color: #ff9933;
 margin-left: 20px;
 margin-right: 20px;
 margin-top: 5px;
}
.uls1a a:nth-child(2){
 font-size: 12px;
 margin-top: 10px;
 margin-right: 20px;
 color: #999999;
}
.uls1a p:nth-child(3){
 width: 60px;
 height: 20px;
 background:red;
 text-align: center;
 line-height: 20px;
 color: white;
 font-size: 14px;
 margin-top: 5px;
}
.uls1a a:nth-child(4){
 font-size: 12px;
 margin-top: 10px;
 margin-left: 10px;
 color: #666666;
}
.uls1p2{
 margin-left: 10px;
 color: white;
 width: 55px;
 height: 17px;
 background: #e4393b;
 text-align: center;
}
.uls2{
 width: 100%;
 height: 98px;
 border-bottom:1px dashed #ececec;
 background: white;
}
.uls2 input,.uls2 a,.uls2 img,.uls2 li,.weight,button{
 float: left;
}
.uls2 a img{
 width: 60px;
 border: 1px solid #ececec;
 margin-top: 17px;
}
.uls2 p,.uls2 span{
 font-size: 12px;
}
.uls2 a p{
 font-size: 12px;
 color: #666666;
 margin-top: 16px;
 float: left;
 margin-left: 10px;
}
.uls2 input{
 margin-top: 40px;
}
.uls2 li span{
 display: block;
}
.uls2 .txt{
 width: 32px;
 height: 19px;
 border: 1px solid #cccccc;
 border-left: none;
 border-right: none;
 outline:none; 
 text-indent: 10px;
 margin-top: 20px;
 font-weight: 600;
 font-size: 12px;
}
.weight{
 margin-left: 150px;
 margin-top: 19px;
}
.uls2 li{
 margin-top: 19px;
 margin-left: 66px;
 margin-right: 37px;
}
.uls2 button{
 width: 20px;
 height: 21px;
 border: none;
 border: 1px solid #cccccc;
 background: white;
 margin-top: 20px;
 outline:none; 
 cursor: pointer;
}
.uls2 li p{
 color: #999999;
 text-decoration: line-through;
}
.uls2 a .laji{
 width: 18px;
 float: left;
 margin-left: 60px;
 margin-top: 20px;
}
.xiaoji{
 float: left;
 margin-left: 47px;
 margin-top: 20px;
 margin-right: 80px;
}
.content1 i{
 font-style: normal;
}
.uls3{
 width: 100%;
 height: 36px;
 border-bottom: 1px dashed #cccccc;
 background: white;
}
.heji{
 float: right;
 margin-right: 20px;
}
.heji p,.heji span{
 float: left;
 font-size: 12px;
 line-height: 36px;
}
.heji span{
 color: #999999;
}
.heji p:nth-child(2),.heji p:nth-child(4){
 margin-right: 44px;
}
.uls4{
 width: 100%;
 height: 60px;
 border-top: 1px solid #cccccc;
 border-bottom: 1px solid #cccccc;
 margin-top: 15px;
 background: white;
}
.uls4L{
 float: left;
 margin-left: 10px;
}
.uls4R{
 float: right;
}
.uls4L p{
 float: left;
 line-height: 60px;
 font-size: 12px;
 color: #999999;
 margin-right: 10px;
}
.uls4L input{
 float: left;
 margin-top: 25px;
 margin-right: 10px;
}
.uls4L a{
 font-size: 12px;
 color: #999999;
 line-height: 60px;
}
.uls4L a:first-child{
 margin-right: 5px;
}
.uls4a,.uls4b{
 float: left;
}
.uls4a1 p{
 float: left;
 font-size: 12px;
}
.uls4a2 p ,.uls4a2 span{
 float: left;
 font-size: 12px;
 margin-top: 10px;
}
.uls4a{
 margin-right: 20px;
}
.aaa p{
 font-size: 12px;
 color: #999999;
}
.aaa p:first-child{
 margin-right: 20px;
 margin-top: 20px;
}
.aaa p:last-child{
 margin-top: 20px;
}
.aaa span,.aaa i{
 color: red;
 font-weight: 600;
}
.bbb{
 float: left;
 color: #999999;
}
.shopnum{
 color: #ff9933;
 font-weight: 600;
}
.uls4b a{
 width: 70px;
 height: 61px;
 display: block;
 background: red;
 font-size: 14px;
 color: white;
 font-weight: 600;
 text-align: center;
 line-height: 60px;
}
.weight{
 margin-right: 60px;
}
.a666{
 cursor: pointer;
}
.fixed{
 position: fixed;
 width: 400px;
 height: 200px;
 background: white;
 z-index: 200;
 top:40%;
 left: 30%;
 border: 1px solid red;
 display: none;
}
.fixed p{
 width: 380px;
 height: 30px;
 background: red;
 font-size: 12px;
 color: white;
 line-height: 30px;
 padding-left: 20px;
}
.fixed span{
 display: block;
 color: #666666;
 margin-top: 20px;
 text-align: center;
}
.fixed button:nth-child(3){
 margin-left: 140px;
}
.fixed button{
 margin-right: 20px;
 border: none;
 width: 50px;
 height: 30px;
 margin-top: 50px;
 border: 1px solid red;
 color: red;
 background: white;
}

js部分

//基础加减
new Vue({
 el:"#content1",
 data:{
 add1:1,
 add2:2000,
 num1:1,
 num2:3000,
 num3:5000, //设置总价的最小值
 },
 methods:{
 add:function(inc,inc2,inc3){
 this.add1 += inc;
 this.add2 += inc2;
 this.num3 = this.add2 +this.num2;
 },
 sub:function(dec,dec2,dec3){
 this.add1 -= dec;
 this.add2 -= dec2;
 this.num3 = this.add2 +this.num2;
 if(this.add1 <= 1){
 this.add1 =1;
 }
 if(this.add2 <= 2000){
 this.add2 =2000;
 }
 if(this.num3 <= 5000){
 this.num3 =5000;
 }
 },
 numadd:function(mun1,mun2,mun3){
 this.num1 += mun1;
 this.num2 += mun2;
 this.num3 = this.add2 +this.num2;
 this.num3 = this.add2 +this.num2;
 },
 numsub:function(mun1,mun2,mun3){
 this.num1 -= mun1;
 this.num2 -= mun2;
 this.num3 = this.add2 +this.num2;
 if(this.num1 <= 1){
 this.num1 =1;
 }
 if(this.num2 <= 3000){
 this.num2 =3000;
 }
 if(this.num3 <= 5000){
 this.num3 =5000;
 }
 }, 
 }
});
 
 
$(function(){
 $(".b666").click(function(){
 var $this = $(this);
 $(".fixed").css("display","block");
 $(".sure").click(function(){
 $this.parent().css("display","none");
 $(this).parent().css("display","none");
 });
 $(".quxiao").click(function(){
 $(this).parent().css("display","none");
 });
 });
});
 
//全选
$("#all").click(function(){
 if($("#all[type='checkbox']").prop("checked") == true){ 
 $(".all").attr('checked', true);
 $(".chose").html(2);
 }else{
 $(".all").attr('checked', false); 
 $(".chose").html(0); 
 }
 });
$(".all1").click(function(){
 if($(".all1[type='checkbox']").prop("checked") == true){ 
 $(".chose").html(1);
 }else{
 $(".chose").html(0); 
 } 
});
$(".all2").click(function(){
 if($(".all2[type='checkbox']").prop("checked") == true){ 
 $(".chose").html(1);
 }else{
 $(".chose").html(0); 
 } 
}); 
 
$(".delete").click(function(){
 if($(".all[type='checkbox']").prop("checked") == false){
 return;
 }
 if($("#all[type='checkbox']").prop("checked") == true){
 $(".uls2").css("display","none");
 $(".chose").html(0);
 } 
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JS控件的生命周期介绍
Oct 22 Javascript
JS 页面计时器示例代码
Oct 28 Javascript
js控制元素显示在屏幕固定位置及监听屏幕高度变化的方法
Aug 11 Javascript
快速掌握Node.js事件驱动模型
Mar 21 Javascript
jQuery实现ajax的叠加和停止(终止ajax请求)
Aug 08 Javascript
javascript数组去重方法分析
Dec 15 Javascript
jQuery使用JSONP实现跨域获取数据的三种方法详解
May 04 jQuery
微信小程序富文本渲染引擎的详解
Sep 30 Javascript
Vue.js实现的表格增加删除demo示例
May 22 Javascript
详解多页应用 Webpack4 配置优化与踩坑记录
Oct 16 Javascript
详解使用uni-app开发微信小程序之登录模块
May 09 Javascript
Json实现传值到后台代码实例
Jun 30 Javascript
解决layui中onchange失效以及form动态渲染失效的问题
Sep 27 #Javascript
layui 地区三级联动 form select 渲染的实例
Sep 27 #Javascript
vue实现移动端省市区选择
Sep 27 #Javascript
vue实现表单录入小案例
Sep 27 #Javascript
解决LayUI加上form.render()下拉框和单选以及复选框不出来的问题
Sep 27 #Javascript
layui form.render('select', 'test2') 更新渲染的方法
Sep 27 #Javascript
jquery 回调操作实例分析【回调成功与回调失败的情况】
Sep 27 #jQuery
You might like
PHP引用(&amp;)各种使用方法实例详解
2014/03/20 PHP
phpnow php探针环境检测代码
2014/11/04 PHP
PHP框架实现WebSocket在线聊天通讯系统
2019/11/21 PHP
PHP设计模式入门之状态模式原理与实现方法分析
2020/04/26 PHP
php提高脚本性能的4个技巧
2020/08/18 PHP
SyntaxHighlighter代码加色使用方法
2008/09/07 Javascript
在javascript中执行任意html代码的方法示例解读
2013/12/25 Javascript
JS下载文件|无刷新下载文件示例代码
2014/04/17 Javascript
浅谈JavaScript数据类型
2015/03/03 Javascript
使用js复制链接中的部分文字的方法
2015/07/30 Javascript
jQuery插件之Tocify动态节点目录菜单生成器附源码下载
2016/01/08 Javascript
javaScript 事件绑定、事件冒泡、事件捕获和事件执行顺序整理总结
2016/10/10 Javascript
vue.js学习笔记:如何加载本地json文件
2017/01/17 Javascript
js实现产品缩略图效果
2017/03/10 Javascript
详解Angular2表单-模板驱动的表单(Template-Driven Forms)
2017/08/04 Javascript
webpack分离css单独打包的方法
2018/06/12 Javascript
微信小程序实现两个页面传值的方法分析
2018/12/11 Javascript
如何将百度地图包装成Vue的组件的方法步骤
2019/02/12 Javascript
详解从0开始搭建微信小程序(前后端)的全过程
2019/04/15 Javascript
python通过exifread模块获得图片exif信息的方法
2015/03/16 Python
用Eclipse写python程序
2018/02/10 Python
python读取图片并修改格式与大小的方法
2018/07/24 Python
Python列表list排列组合操作示例
2018/12/18 Python
利用python实现在微信群刷屏的方法
2019/02/21 Python
Python合并同一个文件夹下所有PDF文件的方法
2019/03/11 Python
基于HTML5 Canvas:字符串,路径,背景,图片的详解
2013/05/09 HTML / CSS
餐饮主管岗位职责
2013/12/10 职场文书
技校毕业生自荐信
2014/06/03 职场文书
材料专业大学毕业生自荐书
2014/07/02 职场文书
2014党员民主评议个人思想剖析发言
2014/09/19 职场文书
2014幼儿园保育员工作总结
2014/11/10 职场文书
幼儿园开学家长寄语(2015秋季)
2015/05/27 职场文书
堂吉诃德读书笔记
2015/06/30 职场文书
关于HTML编码导致的乱码问题
2021/09/04 HTML / CSS
win10清理dns缓存
2022/04/19 数码科技
JavaScript原型链中函数和对象的理解
2022/06/16 Javascript