Vue商品控件与购物车联动效果的实例代码

本篇我们将构建商品控件与购物车联动。

商品控件

商品控件的结构编写

Vue商品控件与购物车联动效果的实例代码

 

在商品组件的<template>标签内完成项目结构,以及数据,事件的绑定,与判断逻辑的书写。

<template> <div> <div ref="menuScroll"> <ul> <!--专场--> <li :class="{'current':currentIndex===0}" @click="selectMenu(0)"> <p> <img :src="container.tag_icon" v-if="container.tag_icon"> {{container.tag_name}} </p> </li> <li v-for="(item,index) in goods" :class="{'current':currentIndex===index+1}" @click="selectMenu(index+1)" > <p> <img :src="item.icon" v-if="item.icon"> {{item.name}} </p> </li> </ul> </div> <!-- 右侧商品列表 --> <div ref="foodScroll"> <!--专场--> <ul> <li> <div v-for="item in container.operation_source_list"> <img :src="item.pic_url"> </div> </li> <!-- 具体分类--> <li v-for="item in goods"> <h3>{{item.name}}</h3> <!--具体商品列表--> <ul> <li v-for="food in item.spus"> <div :style="head_bg(food.picture)"></div> <div> <h3>{{food.name}}</h3> <p v-if="food.description">{{food.description}}</p> <div> <span>{{food.month_saled_content}}</span> <span>{{food.praise_content}}</span> </div> <img :src="food.product_label_picture" v-show="food.product_label_picture" > <p> <span>¥{{food.min_price}}</span> <span>/{{food.unit}}</span> </p> </div> <div> <Cartcontrol :food="food"></Cartcontrol> </div> </li> </ul> </li> </ul> </div> <Shopcart :poiInfo="poiInfo" :selectFoods="selectFoods"></Shopcart> </div> </template>

Shopcart组件是Goods组件的子组件,在Shopcart组件初始化的时候我们可以传入给其参数poiInfo selectFoods.

请求数据,声明方法与计算属性

<script> import BScroll from "better-scroll";//滚动组件 import Shopcart from "components/Shopcart/Shopcart";//购物车 import Cartcontrol from "components/Cartcontrol/Cartcontrol";//控制商品数量按钮 export default { data() { return { container: {}, goods: [], poiInfo: {}, listHeight: [], menuScroll: {}, foodScroll: {}, scrollY: 0 }; }, components: { BScroll,//引入组件 Shopcart, Cartcontrol }, created() { this.$axios .get("api/goods") .then(response => { let json = response.data; if (json.code === 0) { // 重点 this.container = json.data.container_operation_source; this.goods = json.data.food_spu_tags; this.poiInfo = json.data.poi_info; this.$nextTick(function() { this.initScroll(); // 左右联动 // 右->左 // 计算区间 this.caculateHeight(); }); } }) .catch(function(error) { // handle error console.log(error); }); }, computed: { // 根据右侧判断左侧index currentIndex() { for (let i = 0; i < this.listHeight.length; i++) { let start = this.listHeight[i]; let end = this.listHeight[i + 1]; if (this.scrollY >= start && this.scrollY < end) { return i; } } return 0; }, selectFoods() { let foods = []; this.goods.forEach(good => { good.spus.forEach(food => { if (food.count > 0) { foods.push(food); } }); }); return foods; } }, methods: { head_bg(imgName) { return "background-image: url(" + imgName + ");"; }, initScroll() { this.menuScroll = new BScroll(this.$refs.menuScroll, { click: true }); this.foodScroll = new BScroll(this.$refs.foodScroll, { probeType: 3, click: true }); this.foodScroll.on("scroll", pos => { this.scrollY = Math.abs(Math.round(pos.y)); }); }, caculateHeight() { let foodList = this.$refs.foodScroll.getElementsByClassName( "food-list-hook" );//获取到dom元素 let height = 0; this.listHeight.push(height); for (let i = 0; i < foodList.length; i++) { height += foodList[i].clientHeight; this.listHeight.push(height); } // [0, 215, 1343, 2425, 3483, 4330, 5823, 7237, 8022, 8788, 9443] }, selectMenu(index) { let foodlist = this.$refs.foodScroll.getElementsByClassName( "food-list-hook" ); // 根据下标,滚动到相对应的元素 let el = foodlist[index]; // 滚动到对应元素的位置 this.foodScroll.scrollToElement(el, 100); } } }; </script>

定义商品组件的样式

<style scoped> .goods { display: -webkit-box; display: -ms-flexbox; display: flex; position: absolute; top: 190px; bottom: 51px; overflow: hidden; width: 100%; } .goods .menu-wrapper { -webkit-box-flex: 0; -ms-flex: 0 0 85px; flex: 0 0 85px; background: #f4f4f4; } .goods .menu-wrapper .menu-item { padding: 16px 23px 15px 10px; border-bottom: 1px solid #e4e4e4; position: relative; } .goods .menu-wrapper .menu-item.current { background: white; font-weight: bold; margin-top: -1px; } .goods .menu-wrapper .menu-item:first-child.current { margin-top: 1px; } .goods .menu-wrapper .menu-item .text { font-size: 13px; color: #333333; line-height: 17px; vertical-align: middle; -webkit-line-clamp: 2; display: -webkit-box; -webkit-box-orient: vertical; overflow: hidden; } .goods .menu-wrapper .menu-item .text .icon { width: 15px; height: 15px; vertical-align: middle; } .goods .menu-wrapper .menu-item .num { position: absolute; right: 5px; top: 5px; width: 13px; height: 13px; border-radius: 50%; color: white; background: red; text-align: center; font-size: 7px; line-height: 13px; } .goods .foods-wrapper { -webkit-box-flex: 1; -ms-flex: 1; flex: 1; /* background: blue; */ } .goods .foods-wrapper .container-list { padding: 11px 11px 0 11px; border-bottom: 1px solid #e4e4e4; } .goods .foods-wrapper .container-list img { width: 100%; margin-bottom: 11px; border-radius: 5px; } .goods .foods-wrapper .food-list { padding: 11px; } .goods .foods-wrapper .food-list .title { height: 13px; font-size: 13px; background: url(btn_yellow_highlighted@2x.png) no-repeat left center; background-size: 2px 10px; padding-left: 7px; margin-bottom: 12px; } .goods .foods-wrapper .food-list .food-item { display: flex; margin-bottom: 25px; position: relative; } .goods .foods-wrapper .food-list .food-item .icon { flex: 0 0 63px; background-position: center; background-size: 120% 100%; background-repeat: no-repeat; margin-right: 11px; height: 75px; } .goods .foods-wrapper .food-list .food-item .content { flex: 1; } .goods .foods-wrapper .food-list .food-item .content .name { font-size: 16px; line-height: 21px; color: #333333; margin-bottom: 10px; padding-right: 27px; } .goods .foods-wrapper .food-list .food-item .content .desc { font-size: 10px; line-height: 19px; color: #bfbfbf; margin-bottom: 8px; /* 超出部分显示省略号*/ -webkit-line-clamp: 1; display: -webkit-box; -webkit-box-orient: vertical; overflow: hidden; } .goods .foods-wrapper .food-list .food-item .content .extra { font-size: 10px; color: #bfbfbf; margin-bottom: 7px; } .goods .foods-wrapper .food-list .food-item .content .extra .saled { margin-right: 14px; } .goods .foods-wrapper .food-list .food-item .content .product { height: 15px; margin-bottom: 6px; } .goods .foods-wrapper .food-list .food-item .content .price { font-size: 0; } .goods .foods-wrapper .food-list .food-item .content .price .text { font-size: 14px; color: #fb4e44; } .goods .foods-wrapper .food-list .food-item .content .price .unit { font-size: 12px; color: #bfbfbf; } </style>

商品数量控制组件

这里用了vue动画

cart-decrease类为商品数量减少结构。 使用指令v-show控制其显隐。有商品数量的时候会按照规定动画进行显示,反之则隐藏。

cart-count类为选中的商品数量。

cart-add类为商品数量增加结构。

通过vue全局api set进行第一次点击增加商品按钮时候的设置。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/7566a75389276e9d42702a015d9e9653.html