用vue做出来小案列的购物车

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

<script src="js/vue.min.js"></script>

<style type="text/css">

#app{width: 500px; margin: 100px auto; background: #09f; font-size: 18px;}

ul{list-style: none; padding: 0; width: 400px; margin: 0 auto; padding-top: 50px; }

li{height: 60px; display: flex; justify-content: space-between;padding: 0 10px; color: #fff;background: #f90; margin: 14px 0; align-items: center;}

p{margin: 0;}

.num{display: flex; height: 80px; align-items: center; justify-content: space-between; color: #FFFFFF; padding: 0 80px;}

span{font-weight: bold;}

.active{background: #0f9;}

</style>

</head>

<body>

<div id="app">

<ul>

<li v-for="(item,index) in list" :class="{active:item.isActive}" @click="click(index)"><p>{{item.name}}</p><span>${{item.price}}</span></li>

</ul>

<hr />

<div class="num"><p>Total:</p><span>${{sum}}</span></div>

</div>

</body>

<script type="text/javascript">

var vm = new Vue({

el:"#app",

data:{

list:[

{

name:"商品1",

price:220,

isActive:false

},

{

name:"商品2",

price:400,

isActive:false

},

{

name:"商品3",

price:300,

isActive:false

},

{

name:"商品4",

price:250,

isActive:false

},

],

sum:0

},

methods:{

click(index){

this.list[index].isActive = !this.list[index].isActive

if(this.list[index].isActive){

this.sum+=this.list[index].price;

}else{

this.sum-=this.list[index].price;

}

}

}

})

</script>

</html>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 详情页面 packagecom.example.shoppingcar; importandroid.conten...
    ForCrazyLove阅读 3,682评论 0 2
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,174评论 0 2
  • 文/怀左同学 01 《旅行者的早餐》,书到武汉的那天,微雨,我被书的封面吸引,手里攥着笔,一边看,一边做点随心的笔...
    怀左同学阅读 9,775评论 35 117
  • 忆江南-桂花赋 夜中展 伴露悄然开 黄也红也平平色 素手胭脂淡淡分 独醉天上云 却羞赧 心事蝶儿猜 性...
    featherchao阅读 1,162评论 0 0
  • 文/月明红红 细雨烟蒙过大江,来时无为倍添伤 无边落叶萧萧下,满目苍夷硕硕黄 旧雁离家已飞远,蛰虫归穴更疏狂 微凉...
    月明红红阅读 2,444评论 2 7

友情链接更多精彩内容