温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Vue商品控件与购物车联动效果怎么实现

发布时间:2021-06-26 13:52:43 阅读:142 作者:小新 栏目:web开发
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家分享的是有关Vue商品控件与购物车联动效果怎么实现的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

示例:构建商品控件与购物车联动

商品控件

商品控件的结构编写

Vue商品控件与购物车联动效果怎么实现 

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

<template>
 <div class="goods">
  <div class="menu-wrapper" ref="menuScroll">
   <ul>
    <!--专场-->
    <li class="menu-item" :class="{'current':currentIndex===0}" @click="selectMenu(0)">
     <p class="text">
      <img :src="container.tag_icon" v-if="container.tag_icon" class="icon">
      {{container.tag_name}}
     </p>
    </li>
    <li
     class="menu-item"
     v-for="(item,index) in goods"
     :class="{'current':currentIndex===index+1}"
     @click="selectMenu(index+1)"
    >
     <p class="text">
      <img :src="item.icon" v-if="item.icon" class="icon">
      {{item.name}}
     </p>
    </li>
   </ul>
  </div>
  <!-- 右侧商品列表 -->
  <div class="foods-wrapper" ref="foodScroll">
   <!--专场-->
   <ul>
    <li class="container-list food-list-hook">
     <div v-for="item in container.operation_source_list">
      <img :src="item.pic_url">
     </div>
    </li>
    <!-- 具体分类-->
    <li v-for="item in goods" class="food-list food-list-hook">
     <h4 class="title">{{item.name}}</h4>
     <!--具体商品列表-->
     <ul>
      <li v-for="food in item.spus" class="food-item">
       <div class="icon" :></div>

       <div class="content">
        <h4 class="name">{{food.name}}</h4>
        <p class="desc" v-if="food.description">{{food.description}}</p>
        <div class="extra">
         <span class="saled">{{food.month_saled_content}}</span>
         <span class="praise">{{food.praise_content}}</span>
        </div>
        <img
         class="product"
         :src="food.product_label_picture"
         v-show="food.product_label_picture"
        >
        <p class="price">
         <span class="text">¥{{food.min_price}}</span>
         <span class="unit">/{{food.unit}}</span>
        </p>
       </div>
       <div class="cartcontrol-wrapper">
        <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;
 top190px;
 bottom51px;
 overflow: hidden;
 width100%;
}
.goods .menu-wrapper {
 -webkit-box-flex0;
 -ms-flex0 0 85px;
 flex0 0 85px;
 background#f4f4f4;
}
.goods .menu-wrapper .menu-item {
 padding16px 23px 15px 10px;
 border-bottom1px 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-top1px;
}
.goods .menu-wrapper .menu-item .text {
 font-size13px;
 color#333333;
 line-height17px;
 vertical-align: middle;
 -webkit-line-clamp: 2;
 display: -webkit-box;
 -webkit-box-orient: vertical;

 overflow: hidden;
}
.goods .menu-wrapper .menu-item .text .icon {
 width15px;
 height15px;
 vertical-align: middle;
}
.goods .menu-wrapper .menu-item .num {
 position: absolute;
 right5px;
 top5px;
 width13px;
 height13px;
 border-radius50%;
 color: white;
 background: red;
 text-align: center;
 font-size7px;
 line-height13px;
}

.goods .foods-wrapper {
 -webkit-box-flex1;
 -ms-flex1;
 flex1;
 /* background: blue; */
}
.goods .foods-wrapper .container-list {
 padding11px 11px 0 11px;
 border-bottom1px solid #e4e4e4;
}
.goods .foods-wrapper .container-list img {
 width100%;
 margin-bottom11px;
 border-radius5px;
}
.goods .foods-wrapper .food-list {
 padding11px;
}
.goods .foods-wrapper .food-list .title {
 height13px;
 font-size13px;
 backgroundurl(btn_yellow_highlighted@2x.png) no-repeat left center;
 background-size2px 10px;
 padding-left7px;
 margin-bottom12px;
}

.goods .foods-wrapper .food-list .food-item {
 display: flex;
 margin-bottom25px;
 position: relative;
}
.goods .foods-wrapper .food-list .food-item .icon {
 flex0 0 63px;
 background-position: center;
 background-size120% 100%;
 background-repeat: no-repeat;
 margin-right11px;
 height75px;
}
.goods .foods-wrapper .food-list .food-item .content {
 flex1;
}
.goods .foods-wrapper .food-list .food-item .content .name {
 font-size16px;
 line-height21px;
 color#333333;
 margin-bottom10px;
 padding-right27px;
}
.goods .foods-wrapper .food-list .food-item .content .desc {
 font-size10px;
 line-height19px;
 color#bfbfbf;
 margin-bottom8px;

 /* 超出部分显示省略号*/
 -webkit-line-clamp: 1;
 display: -webkit-box;
 -webkit-box-orient: vertical;
 overflow: hidden;
}
.goods .foods-wrapper .food-list .food-item .content .extra {
 font-size10px;
 color#bfbfbf;
 margin-bottom7px;
}
.goods .foods-wrapper .food-list .food-item .content .extra .saled {
 margin-right14px;
}
.goods .foods-wrapper .food-list .food-item .content .product {
 height15px;
 margin-bottom6px;
}
.goods .foods-wrapper .food-list .food-item .content .price {
 font-size0;
}
.goods .foods-wrapper .food-list .food-item .content .price .text {
 font-size14px;
 color#fb4e44;
}
.goods .foods-wrapper .food-list .food-item .content .price .unit {
 font-size12px;
 color#bfbfbf;
}
</style>

商品数量控制组件

这里用了vue动画

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

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

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

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

https://cn.vuejs.org/v2/api/#...

<template>
 <div class="cartcontrol">
  <transition name="move">
   <div class="cart-decrease" @click="decreaseCart" v-show="food.count">
    <span class="inner icon-remove_circle_outline"></span>
   </div>
  </transition>
  <div class="cart-count" v-show="food.count">{{food.count}}</div>
  <div class="cart-add icon-add_circle" @click="increaseCart">
   <i class="bg"></i>
  </div>
 </div>
</template>

<script>
import Vue from 'vue'
export default {
 props:{
  food:{
   type:Object
  }
 },
 methods:{
  decreaseCart(){
   this.food.count--;
  },
  increaseCart(){
   if(!this.food.count){
    Vue.set(this.food,'count',1);
   }else{
    this.food.count++;
   }
  }
  
 }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.cartcontrol {
 font-size0;
}

.cartcontrol .cart-decrease {
 display: inline-block;
 width26px;
 height26px;
 font-size26px;
 color#b4b4b4;
}

.cartcontrol .cart-count {
 display: inline-block;
 width25px;
 text-align: center;
 font-size12px;
 line-height26px;
 vertical-align: top;
}

.cartcontrol .cart-add {
 display: inline-block;
 width26px;
 height26px;
 font-size26px;
 color#ffd161;
 position: relative;
}
.cartcontrol .cart-add .bg {
 width20px;
 height20px;
 border-radius50%;
 background: black;
 position: absolute;
 left3px;
 top3px;
 z-index: -1;
}

.move-enter-active,
.move-leave-active {
 transition: all 0.5s linear;
}
.move-enter,
.move-leave-to {
 transformtranslateX(20pxrotate(180deg);
}
</style>

购物车组件

我们现在创建shopcart购物车组件。

<template>
 <div class="shopcart" :class="{'highligh':totalCount>0}">
  <div class="shopcart-wrapper">
   <div class="content-left">
    <div class="logo-wrapper" :class="{'highligh':totalCount>0}">
     <span class="icon-shopping_cart logo" :class="{'highligh':totalCount>0}"></span>
     <i class="num" v-show="totalCount">{{totalCount}}</i>
    </div>
    <div class="desc-wrapper">
     <p class="total-price" v-show="totalPrice">¥{{totalPrice}}</p>
     <p class="tip" :class="{'highligh':totalCount>0}">另需{{shipping_fee_tip}}</p>
    </div>
   </div>

   <div class="content-right" :class="{'highligh':totalCount>0}">{{payStr}}</div>
  </div>
 </div>
</template>

<script>
// 导入BScroll
import BScroll from "better-scroll";

export default {
 props: {
  min_price_tip: {
   typeString,
   default""
  },
  shipping_fee_tip: {
   typeString,
   default""
  },
  selectFoods: {
   typeArray,
   default() {
    return [
     /* {
      min_price: 10,
      count: 3
     },
     {
      min_price: 7,
      count: 1
     } */
    ];
   }
  }
 },
 computed: {
  // 总个数 将所选商品的个数累加得到总个数。
  totalCount() {
   let num = 0;
   this.selectFoods.forEach(food => {
    num += food.count;
   });

   return num;
  },
  // 总金额
  totalPrice() {
   let total = 0;
   this.selectFoods.forEach(food => {
    total += food.min_price * food.count;
   });

   return total;
  },
  // 结算按钮显示
  payStr() {
   if (this.totalCount > 0) {
    return "去结算";
   } else {
    return this.min_price_tip;
   }
  }
 },
 components: {
  BScroll
 }
};
</script>

<style>
.shopcart-wrapper {
 width100%;
 height51px;
 background#514f4f;
 position: fixed;
 left0;
 bottom0;
 display: flex;
 z-index99;
}
.shopcart-wrapper.highligh {
 background#2d2b2a;
}

.shopcart-wrapper .content-left {
 flex1;
}
.shopcart-wrapper .content-left .logo-wrapper {
 width50px;
 height50px;
 background#666666;
 border-radius50%;
 position: relative;
 top: -14px;
 left10px;
 text-align: center;
 float: left;
}
.shopcart-wrapper .content-left .logo-wrapper.highligh {
 background#ffd161;
}
.shopcart-wrapper .content-left .logo-wrapper .logo {
 font-size28px;
 color#c4c4c4;
 line-height50px;
}
.shopcart-wrapper .content-left .logo-wrapper .logo.highligh {
 color#2d2b2a;
}
.shopcart-wrapper .content-left .logo-wrapper .num {
 width15px;
 height15px;
 line-height15px;
 border-radius50%;
 font-size9px;
 color: white;
 background: red;
 position: absolute;
 right0;
 top0;
}
.shopcart-wrapper .content-left .desc-wrapper {
 float: left;
 margin-left13px;
}
.shopcart-wrapper .content-left .desc-wrapper .total-price {
 font-size18px;
 line-height33px;
 color: white;
}
.shopcart-wrapper .content-left .desc-wrapper .tip {
 font-size12px;
 color#bab9b9;
 line-height51px;
}
.shopcart-wrapper .content-left .desc-wrapper .tip.highligh {
 line-height12px;
}

.shopcart-wrapper .content-right {
 flex0 0 110px;
 font-size15px;
 color#bab9b9;
 line-height51px;
 text-align: center;
 font-weight: bold;
}
.shopcart-wrapper .content-right.highligh {
 background#ffd161;
 color#2d2b2a;
}

.shopcart-wrapper .shopcart-list {
 position: absolute;
 left0;
 top0;
 z-index: -1;
 width100%;
}
.shopcart-wrapper .shopcart-list.show {
 transformtranslateY(-100%);
}

.shopcart-wrapper .shopcart-list .list-top {
 height30px;
 text-align: center;
 font-size11px;
 background#f3e6c6;
 line-height30px;
 color#646158;
}

.shopcart-wrapper .shopcart-list .list-header {
 height30px;
 background#f4f4f4;
}
.shopcart-wrapper .shopcart-list .list-header .title {
 float: left;
 border-left4px solid #53c123;
 padding-left6px;
 line-height30px;
 font-size12px;
}
.shopcart-wrapper .shopcart-list .list-header .empty {
 float: right;
 line-height30px;
 margin-right10px;
 font-size0;
}
.shopcart-wrapper .shopcart-list .list-header .empty img {
 height14px;
 margin-right9px;
 vertical-align: middle;
}
.shopcart-wrapper .shopcart-list .list-header .empty span {
 font-size12px;
 vertical-align: middle;
}

.shopcart-wrapper .shopcart-list .list-content {
 max-height360px;
 overflow: hidden;
 background: white;
}
.shopcart-wrapper .shopcart-list .list-content .food-item {
 height38px;
 padding12px 12px 10px 12px;
 border-bottom1px solid #f4f4f4;
}
.shopcart-wrapper .shopcart-list .list-content .food-item .desc-wrapper {
 float: left;
 width240px;
}
.shopcart-wrapper
 .shopcart-list
 .list-content
 .food-item
 .desc-wrapper
 .desc-left {
 float: left;
 width170px;
}
.shopcart-wrapper
 .shopcart-list
 .list-content
 .food-item
 .desc-wrapper
 .desc-left
 .name {
 font-size16px;
 margin-bottom8px;

 /* 超出部分隐藏*/
 -webkit-line-clamp: 1;
 display: -webkit-box;
 -webkit-box-orient: vertical;
 overflow: hidden;
 height16px;
}
.shopcart-wrapper
 .shopcart-list
 .list-content
 .food-item
 .desc-wrapper
 .desc-left
 .unit {
 font-size12px;
 color#b4b4b4;
}
.shopcart-wrapper
 .shopcart-list
 .list-content
 .food-item
 .desc-wrapper
 .desc-left
 .description {
 font-size12px;
 color#b4b4b4;

 /* 超出部分隐藏*/
 overflow: hidden;
 height12px;
}
.shopcart-wrapper
 .shopcart-list
 .list-content
 .food-item
 .desc-wrapper
 .desc-right {
 float: right;
 width70px;
 text-align: right;
}
.shopcart-wrapper
 .shopcart-list
 .list-content
 .food-item
 .desc-wrapper
 .desc-right
 .price {
 font-size12px;
 line-height38px;
}

.shopcart-wrapper .shopcart-list .list-content .food-item .cartcontrol-wrapper {
 float: right;
 margin-top6px;
}

.shopcart .shopcart-mask {
 position: fixed;
 top0;
 right0;
 width100%;
 height100%;
 z-index98px;
 backgroundrgba(717270.6);
}
</style>

Vue商品控件与购物车联动效果怎么实现 

感谢各位的阅读!关于“Vue商品控件与购物车联动效果怎么实现”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI

开发者交流群×