vue从创建到完整的饿了么(18)购物车详细信息的展示与删除

说明

1.上一章--本地缓存购物车
2.苍渡大神源码--项目源码地址
3.数据接口--API接口
4.UI框架--Mint UI

开始

1.目前的购物车功能
图片描述

2.要实现的购物车功能
图片描述

布局

1.样式
html如下(要放在购物车footdiv前)

<div v-if="true" class="car">
                    <div class="carmain flex2">
                        <div class="cartop padlr10 ih50">
                              <span>购物车</span>
                              <span class="right"><icon class="w15 mgr5 y2" name="delete"></icon>清空</span>
                        </div>
                        <div class="carmainbox flex1">
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                            <div class="carbox after padlr10 bgfff ih50">
                                  <span class="bold col666">辣条</span>
                                  <span class="right">
                                      <span class="colred mgr5">¥15</span>
                                      <span @click="removecar(items)"><icon class="addicon y4" name="offline"></icon></span>
                                      <span class="ih20 inblock">3</span>
                                      <span @click="addcar(items)" class=""><icon name="add" class="addicon y4"></icon></span>
                                  </span>
                            </div>
                        </div>
                    </div>
    </div>

新定义的css如下

.car{
   position: fixed;
   top:0px;
   left:0px;
   width:100vw;
   height:100vh;
   background-color:rgba(0,0,0,0.5);
}
.carmain{
  position:absolute;
  width:100%;
  bottom:0px;
  left:0px;
  padding-bottom:50px;
  max-height:70%;
}
.cartop{
  background-color:#ECEFF1;
}
.carmainbox{
  overflow-y: scroll;
}
.y2{
  transform: translateY(2px);
}
.y4{
  transform: translateY(4px); 
}
.bold{
  font-weight: bold;
}
.col666{
  color: #666;
}

页面如下
图片描述

渲染数据

1.显示与隐藏
在data中设置变量carinfo来控制购物车的详细信息的显示隐藏

data () {
    return {
      carinfo:false,                            //购物车详情是否显示
      mycar:"",                                 //我的购物车
      footdiv:true,                             //脚部购物车是否显示
      shopon:0,                                 //商品种类选中的哪个
      imgpath:'http://cangdu.org:8001/img/',    //商家头像的路径地址path
      shopinfo:"",                              //商家信息
      shopmean:"",                              //食品信息
      shoporscore:true,                         //商家还是评价
      score:"",                                 //评价信息
      scorerating:"",                           //评价分数  
      scoretags:"",                             //评价分类
      scoreindex:0,                             //选中第几个评价分类
      num:1,
      modal:false,                              //模态框显示隐藏
      myrouter:true,                            //是否显示big div
    }
  },

为购物车footdiv下的footleftdiv加上点击事件

 <transition name="left">
          <div v-if="footdiv" class="foot" :class="{on:mycar.length>=1}">
                <div class="footleft" @click="mycar.length>0?carinfo=!carinfo:''">
                    <div class="footlogo">
                        <icon name="footcar" class="footicon"></icon>
                        <div v-if="mycar.length" class="rednum">{{mycarshopnum}}</div>
                    </div>
                    <div class="footmain">
                        <div v-if="!mycar.length" class="">未选购商品</div>
                        <div v-if="mycar.length" class="ih30 fs1-2">¥ {{mycarshoppic}}</div>
                        <div v-if="mycar.length" class="ih20">{{this.shopinfo.piecewise_agent_fee.tips}}</div>
                    </div>
                </div>
                <div class="footright" :class="{on:shoppicbig==1}">
                    <span v-if="shoppicbig==3">¥{{this.shopinfo.float_minimum_order_amount}}起送</span>
                    <span v-if="shoppicbig==2">还差¥{{this.shopinfo.float_minimum_order_amount-mycarshoppic}}起送</span>
                    <span v-if="shoppicbig==1">去结算</span>
                </div>
          </div>
    </transition>

注意
只有在购物车有数据且点击购物车左半部分才会显示购物车的详细信息

2.购物车的详细信息cardiv修改如下

<transition name="opacity">
          <div v-if="carinfo&&mycar.length>=1" class="car">
                          <div class="carmain flex2">
                              <div class="cartop padlr10 ih50">
                                    <span>购物车</span>
                                    <span @click="mycar=[]" class="right"><icon class="w15 mgr5 y2" name="delete"></icon>清空</span>
                              </div>
                              <div class="carmainbox flex1">
                                  <div v-for="item in mycar" class="carbox after padlr10 bgfff ih50">
                                        <span class="bold col666">{{item.shop.specfoods[0].name}}</span>
                                        <span class="right">
                                            <span class="colred mgr5">¥{{item.shop.specfoods[0].price*item.num}}</span>
                                            <span @click="item.num=item.num-1"><icon class="addicon y4" name="offline"></icon></span>
                                            <span class="ih20 inblock">{{item.num}}</span>
                                            <span @click="item.num=item.num+1" class=""><icon name="add" class="addicon y4"></icon></span>
                                        </span>
                                  </div>
                              </div>
                          </div>
          </div>
    </transition>

注意
我把数据操作直接写在了@click里,这样只是改变了mycar里的数据,并没有改变本地缓存里的数据,我们需要把data里的mycar与本地缓存的mycar绑定起来
watch中添加

mycar:{
          handler (val1,val2){
                for(var i=0;i<val1.length;i++){
                    if(val1[i].num==0){
                        val1.splice(i,1);
                    }
                }
                if(!(val1.length>=1)){
                    this.carinfo=false;
                }
                localStorage.setItem("mycar",JSON.stringify(val1));
          },
          deep:true
}

如果你要监听的数据是一个对象,那么val1,val2相等,且必须写deep:true并把操作放在handler 函数里

运行试试
图片描述

这么一看,貌似没什么问题了
最终shop.vue代码如下

<template>
  <div id="shop" class="bgfff">
        <transition name="left">
        <div v-if="myrouter" class="big">
              <div class="topbg">
                      <img class="topbgimg" :src="imgpath+shopinfo.image_path">
              </div>
              <div class="shoptop">
                  <div class="toptop ih30">
                    <router-link to="/miste">
                        <icon class="backicon" name="back"></icon>
                    </router-link>
                    <span class="right">
                        <icon class="backicon2" name="cart"></icon>
                        <icon class="backicon2" name="more"></icon> 
                    </span>
                  </div>
                  <div class="topfoot">
                      <div class="topleft">
                          <img :src="imgpath+shopinfo.image_path">
                      </div>
                      <div class="topright nowarp">
                          <router-link to="shop/shopDetail">
                              <div class="foota">
                                  <div class="footamain fs1-2 nowarp">{{shopinfo.name}}</div>
                                  <icon name="right" class="icon3"></icon>
                              </div>
                          </router-link>
                          <div class="footb nowarp">
                              <div class="nowarp">公告:{{shopinfo.promotion_info}}</div>
                          </div>
                          <div class="footc">
                              <span class="footcmain"><span v-if="shopinfo.delivery_mode">{{shopinfo.delivery_mode.text}}•</span><span>约{{shopinfo.order_lead_time}}</span></span>
                          </div>
                      </div>
                  </div>
              </div>
              <div class="shopmid mgtop10">
                  <div @click="modal=true" v-if="shopinfo.activities" class="midtop"><span class="te mgr5">{{shopinfo.activities[0].icon_name}}</span><span>{{shopinfo.activities[0].description}}</span><span class="right">{{shopinfo.activities.length}}个活动 <icon name="down" class="icon4"></icon></span></div>
                  <div class="mytab">
                      <div @click="footdiv=shoporscore=true" :class="{ on:shoporscore }">商品</div>
                      <div @click="footdiv=shoporscore=false" :class="{ on:!shoporscore }">评价{{shopinfo.rating}}分</div>
                  </div>
              </div>

          <transition name="left">
              <div v-if="shoporscore" class="shopmain">
                  <div class="mianleft">
                      <div v-for="(item,index) in getshopnum" @click="itemgo(index)" :class="{on:index==shopon}" class="relative leftdiv">
                          <div>
                              <icon v-if="index==0" class="icon5" name="hot"></icon>
                              <icon v-if="index==1" class="icon5" name="discount"></icon>
                              <span class="fs0-8">{{item.name}}</span>
                              <span v-if="item.mynum" class="rednum2">{{item.mynum}}</span>
                          </div>
                      </div>
                  </div>
                  <div class="mainright">
                      <div class="item" v-for="item in getshopnum">
                          <div class="itemtop padtop10 ih30 after">
                              <span class="fs15">{{item.name}}</span>
                              <span class="fs0-8 col9f">{{item.description}}</span>
                          </div>
                          <div class="itemmain">
                              <div v-for="items in item.foods" class="after">
                                <router-link to="/shop/foodDetail">
                                    <div class="iteminfo">
                                        <div class="infoimgbox">
                                            <img :src="imgpath+items.image_path">
                                        </div>
                                        <div class="inforight nowarp">
                                            <div class="colblack fs15 ih20 nowarp">{{items.name}}</div>
                                            <div class="ih15 col9f"><span class="fs10 mgl">{{items.tips}}</span></div>
                                            <div class="ih15"><span v-if="false" class="fs10 mgl"><span class="zk">包装费</span><span class="yh">{{}}</span></span></div>
                                            <div class="ih20">
                                              <span class="colred fs12">¥</span>
                                              <span class="colred mgr5">{{items.specfoods[0].price}}</span>
                                              <span v-if="items.specfoods[0].original_price" class="fs12 col9f midline">¥56</span>
                                            </div>
                                        </div>
                                    </div>
                                  </router-link>
                                  <div class="iteminfofoot ih20">
                                          <transition name="top">
                                              <span v-if="items.mynum" class="ih20">
                                                  <span @click="removecar(items)"><icon class="addicon" name="offline"></icon></span>
                                                  <span class="ih20 inblock y-4">{{items.mynum}}</span>
                                              </span>
                                          </transition>
                                          <span v-if="items.specfoods.length==1" @click="addcar(items)" class=""><icon name="add" class="addicon"></icon></span>
                                          <span class="fs12 right gz" v-if="items.specfoods.length>1">选规则</span>
                                  </div>
                              </div>
                          </div>
                      </div>
                  </div>
              </div>
          </transition>

          <transition name="right">
              <div class="score" v-if="!shoporscore">
                  <div class="scoretop">
                    <div class="scoretopleft">
                        <div class="fs1-2 colf60">{{shopinfo.rating}}</div>
                        <div class="fs15 col9f">综合评价</div>
                        <div class="fs0-8 col9f">高于周边商家{{parseInt(scorerating.compare_rating*100)}}%</div>
                    </div>
                    <div class="scoretopright">
                        <div><span class="fs15 col9f mgr5">评价服务</span><stars :num="scorerating.service_score.toFixed(1)"></stars><span class="colf60 right">{{scorerating.service_score.toFixed(1)}}</span></div>
                        <div><span class="fs15 col9f mgr5">菜品评价</span><stars :num="scorerating.food_score.toFixed(1)"></stars><span class="colf60 right">{{scorerating.food_score.toFixed(1)}}</span></div>
                        <div><span class="fs15 col9f mgr5">送达时间</span><span class="fs15 colf60">{{scorerating.deliver_time}}分钟</span></div>
                    </div>
                  </div>
                  <div class="scoremain">
                        <div class="scoremaintop after">
                          <div v-for="(item,index) in scoretags" class="ih30 fs0-8" :class="{sty2:item.unsatisfied,sty1:!(item.unsatisfied),on:index==scoreindex}">{{item.name}}({{item.count}})</div>
                        </div>
                        <div class="scoremaininfo">
                            <div v-for="item in score" class="scoreitem after">
                                <div class="scoreitemleft">
                                    <img :src="imgaddpath(item.avatar)" >
                                </div>
                                <div class="scoreitemright fs12 col9f">
                                    <div>
                                        <span>{{item.username}}</span>
                                        <span class="right">{{item.rated_at}}</span>
                                    </div>
                                    <div>
                                        <stars :num="item.rating_star"></stars>
                                    </div>
                                    <div>
                                        {{item.time_spent_desc}}
                                    </div>
                                    <div class="scoreimgbox">
                                      <img v-for="itema in item.item_ratings" :src="imgaddpath(itema.image_hash)">
                                    </div>
                                    <div class="namebox">
                                        <div v-for="itemb in item.item_ratings">{{itemb.food_name}}</div>
                                    </div>

                                </div>
                            </div>
                          
                        </div>
                  </div>
              </div>
          </transition>

          <transition name="top">
              <div v-if="modal" class="modal flex2 colfff pad10">
                  <div class="modaltop flex1">
                    <div>
                      <div class="modaltitle">
                          {{shopinfo.name}}
                      </div>
                      <div class="modalmid">
                            <div class="modal_title ih30"><span>优惠信息</span></div>
                            <div>
                                <div v-if="shopinfo.activities" v-for="item in shopinfo.activities" class="midtop"><span class="te mgr5">{{item.icon_name}}</span><span>{{item.description}}</span></div>
                            </div>
                      </div>
                      <div class="modalinfo mgtop40">
                            <div class="modal_title ih30"><span>商家公告</span></div>
                            <div> 
                                {{shopinfo.promotion_info}}  
                            </div>
                      </div>
                    </div>
                  </div>
                  <div @click="modal=false" class="modalfoot">
                        <span>X</span>
                  </div>
              </div>
          </transition>

          <load v-if="num!=1"></load>
    </div>
    </transition>

    <transition name="opacity">
          <div v-if="carinfo&&mycar.length>=1" class="car">
                          <div class="carmain flex2">
                              <div class="cartop padlr10 ih50">
                                    <span>购物车</span>
                                    <span @click="mycar=[]" class="right"><icon class="w15 mgr5 y2" name="delete"></icon>清空</span>
                              </div>
                              <div class="carmainbox flex1">
                                  <div v-for="item in mycar" class="carbox after padlr10 bgfff ih50">
                                        <span class="bold col666">{{item.shop.specfoods[0].name}}</span>
                                        <span class="right">
                                            <span class="colred mgr5">¥{{item.shop.specfoods[0].price*item.num}}</span>
                                            <span @click="item.num=item.num-1"><icon class="addicon y4" name="offline"></icon></span>
                                            <span class="ih20 inblock">{{item.num}}</span>
                                            <span @click="item.num=item.num+1" class=""><icon name="add" class="addicon y4"></icon></span>
                                        </span>
                                  </div>
                              </div>
                          </div>
          </div>
    </transition>
    <transition name="left">
          <div v-if="footdiv" class="foot" :class="{on:mycar.length>=1}">
                <div class="footleft" @click="mycar.length>0?carinfo=!carinfo:''">
                    <div class="footlogo">
                        <icon name="footcar" class="footicon"></icon>
                        <div v-if="mycar.length" class="rednum">{{mycarshopnum}}</div>
                    </div>
                    <div class="footmain">
                        <div v-if="!mycar.length" class="">未选购商品</div>
                        <div v-if="mycar.length" class="ih30 fs1-2">¥ {{mycarshoppic}}</div>
                        <div v-if="mycar.length" class="ih20">{{this.shopinfo.piecewise_agent_fee.tips}}</div>
                    </div>
                </div>
                <div class="footright" :class="{on:shoppicbig==1}">
                    <span v-if="shoppicbig==3">¥{{this.shopinfo.float_minimum_order_amount}}起送</span>
                    <span v-if="shoppicbig==2">还差¥{{this.shopinfo.float_minimum_order_amount-mycarshoppic}}起送</span>
                    <span v-if="shoppicbig==1">去结算</span>
                </div>
          </div>
    </transition>
    

    <router-view></router-view>
  </div>
</template>

<script>
import stars from '../../components/stars/stars'
import load from '../../components/load/load'


export default {
  data () {
    return {
      carinfo:false,                            //购物车详情是否显示
      mycar:"",                                 //我的购物车
      footdiv:true,                             //脚部购物车是否显示
      shopon:0,                                 //商品种类选中的哪个
      imgpath:'http://cangdu.org:8001/img/',    //商家头像的路径地址path
      shopinfo:"",                              //商家信息
      shopmean:"",                              //食品信息
      shoporscore:true,                         //商家还是评价
      score:"",                                 //评价信息
      scorerating:"",                           //评价分数  
      scoretags:"",                             //评价分类
      scoreindex:0,                             //选中第几个评价分类
      num:1,
      modal:false,                              //模态框显示隐藏
      myrouter:true,                            //是否显示big div
    }
  },
  components:{
  //注册组件
      stars,
      load
  },
  mounted:function(){
  //生命周期
      var that=this;
      //获取购物车信息
      if(localStorage.getItem("mycar")){
          that.mycar=JSON.parse(localStorage.getItem("mycar"));
      }
      
      that.num=that.num-5;
      //餐馆详情
      this.$http.get('http://cangdu.org:8001/shopping/restaurant/'+this.$store.state.shopid+'').then(response => {
        console.log(response);
        this.shopinfo=response.body;
        that.num=that.num+1;
      }, response => {
        that.num=that.num+1;
        console.log(response);
      });
      //食品详情
      this.$http.get('http://cangdu.org:8001/shopping/v2/menu?restaurant_id='+this.$store.state.shopid+'').then(response => {
        console.log(response);
        that.num=that.num+1;
        this.shopmean=response.body;
      }, response => {
        that.num=that.num+1;
        console.log(response);
      });
      //评论详情
      this.$http.get('http://cangdu.org:8001/ugc/v2/restaurants/'+this.$store.state.shopid+'/ratings?offset=0&limit=10').then(response => {
        console.log(response);
        that.num=that.num+1;
        this.score=response.body;
      }, response => {
        that.num=that.num+1;
        console.log(response);
      });
      //评论分数
      this.$http.get('http://cangdu.org:8001/ugc/v2/restaurants/'+this.$store.state.shopid+'/ratings/scores').then(response => {
        console.log(response);
        this.scorerating=response.body;
        that.num=that.num+1;
      }, response => {
        console.log(response);
        that.num=that.num+1;

      });
       //评论分类
      this.$http.get('http://cangdu.org:8001/ugc/v2/restaurants/'+this.$store.state.shopid+'/ratings/tags').then(response => {
        console.log(response);
        this.scoretags=response.body;
        that.num=that.num+1;

      }, response => {
        console.log(response);
        that.num=that.num+1;

      });
       let mydiv=document.querySelector('.mainright');
       mydiv.addEventListener('scroll', this.handleScroll);
  },
  computed:{
  //计算属性
      //计算商品数量
      mycarshopnum:function(){
          var num=0;
          for(var i=0;i<this.mycar.length;i++){
                num+=this.mycar[i].num;
          }
          return num
      },
      //计算商品价格(商品只有一个种类)
      mycarshoppic:function(){
          var num=0;
           if(this.mycar.length){
              for(var i=0;i<this.mycar.length;i++){
                num+=(this.mycar[i].shop.specfoods[0].price*this.mycar[i].num);
              }
           };          
          return num;
      },
      //判断商家起送价与目前购物车价格
      shoppicbig:function(){
                    if(this.mycarshoppic>=this.shopinfo.float_minimum_order_amount){
                        return 1
                    }else if(this.mycarshoppic==0){
                        return 3
                    }else{
                        return 2
                    }
      },
      //商品与购物车数量结合
      getshopnum:function(){
          for(var i=0;i<this.shopmean.length;i++){
              var thisnum=0;
              for(var k=0;k<this.shopmean[i].foods.length;k++){
                    var isadd=true;                          //判断该商品是否在购物车
                    for( var h=0;h<this.mycar.length;h++){
                        if(this.shopmean[i].foods[k].specfoods[0]._id==this.mycar[h].shop.specfoods[0]._id){
                            this.shopmean[i].foods[k].mynum=this.mycar[h].num;
                            thisnum+=this.mycar[h].num;
                            isadd=false;
                            break;
                        }
                    }
                    if(isadd){
                        this.shopmean[i].foods[k].mynum=0;
                    }
                    
              }
              this.shopmean[i].mynum=thisnum;
          }
          return this.shopmean
      }
      
  },
  methods:{
  //函数
      imgaddpath:function(e){
        return "https://fuss10.elemecdn.com/"+e+".jpeg"
      },
      itemgo:function(index){
        this.shopon=index;
        document.querySelector('.mainright').scrollTop=document.querySelectorAll('.itemtop')[index].offsetTop;
      },
      handleScroll:function(){
        var mydiv=document.querySelectorAll('.item');
        for(var i =0;i<mydiv.length;i++){
           if(mydiv[i].offsetTop>document.querySelector('.mainright').scrollTop-10&&mydiv[i].offsetTop<document.querySelector('.mainright').scrollTop+10){
                this.shopon=i;   
           }
        }
      },
      newpage:function(){
        this.myrouter=(this.$route.path=="/shop"?true:false);
        this.footdiv=(((this.$route.path=="/shop"&&this.shoporscore)||this.$route.path=="/shop/foodDetail")?true:false);
      },
      gofoodDetail:function(){
          this.$router.push("/shop/foodDetail");
      },
      addcar:function(e){
          var that=this;
          if(localStorage.getItem("mycar")){
              var mycar=JSON.parse(localStorage.getItem("mycar"));
              var addok=true;                                              //数据是否添加
              for(var i =0;i<mycar.length;i++){
                  if(mycar[i].shop.item_id==e.item_id&&mycar[i].shop.category_id==e.category_id&&mycar[i].shop.restaurant_id==e.restaurant_id){
                      mycar[i].num=mycar[i].num+1;
                      addok=false;
                      break;
                  }
              }
              if(addok){
                    mycar.push({"shop":e,"num":1});
              }
              that.mycar=mycar;
              localStorage.setItem("mycar",JSON.stringify(mycar));
          }else{
              var mycar=[{"shop":e,"num":1}];
              that.mycar=mycar;
              localStorage.setItem("mycar",JSON.stringify(mycar));
          };
      },
      removecar:function(e){
          for(var i=0;i<this.mycar.length;i++){
              if(this.mycar[i].shop.specfoods[0]._id==e.specfoods[0]._id){
                    this.mycar[i].num==1?this.mycar.splice(i,1):(this.mycar[i].num=this.mycar[i].num-1);
                    break;
              }
          }
          localStorage.setItem("mycar",JSON.stringify(this.mycar));
      }
  },
  watch:{
        "$route":"newpage",
        mycar:{
          handler (val1,val2){
            for(var i=0;i<val1.length;i++){
                if(val1[i].num==0){
                    val1.splice(i,1);
                }
            }
            if(!(val1.length>=1)){
                this.carinfo=false;
            }
            localStorage.setItem("mycar",JSON.stringify(val1));
          },
          deep:true
        }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.big{
  display:-webkit-box;
  -webkit-box-orient:vertical;
  height:100vh;
}

.shoptop{
  height:120px;
  background-color: rgba(119,103,137,.43);
  box-sizing:border-box;
  padding:10px 10px 0px 10px;
  position: relative;
}
.topbg{
  position: absolute;
  width:100%;
  height:120px;
  left:0px;
  right:0px;
  overflow:hidden;
}
.topbgimg{
  width:100%;
  filter: blur(10px);
}
.backicon{
  height:30px;
  width:30px;
}
.backicon2{
  height:25px;
  width:25px;
}
.toptop{
  margin-bottom:10px;
}
.topfoot{
  height:70px;
  display:flex;
}
.topleft{
  height:80px;
  width:80px;
  background-color:#3c3c3c;
  margin-right:10px;
  border-radius:3px;
  box-shadow:0 0 5px #3c3c3c;  
}
.topleft>img{
  max-width:100%;
  max-height:100%;
  border-radius:3px;
}
.topright,.topleft{
  float:left;
}
.topright{
  height:100%;
  flex:1;
}
.foota{
  height:30px;
  color:white;
  line-height:30px;
  display:flex;
}
.icon3{
  width:20px;
  height:20px;
  margin-top:5px;
}
.footc{
  height:20px;
  line-height:20px;
  font-size:12px;
}
.footb{
  font-size:12px;
  color:white;
}
.footcmain{
  background-color:#0097FF;
  color:white;
  padding:0px 5px; 
}
.shopmid{
  padding:10px 10px 0px 10px;
  border-bottom:2px solid #F8F8F8;
}
.midtop{
  font-size:12px;
}
.te{
  background-color:#F08449;
  padding:0px 1px;
  color:white;
}
.mytab{
  overflow:hidden;
  margin-top:10px;
}
.mytab>div{
  float:left;
  padding-bottom:10px;
  margin-right:10px;
  border-bottom:2px solid white;
}
.mytab>div.on{
  color:#0B89FF;
  border-color:#0B89FF;
}
.leftdiv{
  width:60px;
  padding:0px 10px;
  color:#727272;
  background-color:#F8F8F8;
}
.leftdiv>div{
  padding:15px 0px;
  border-bottom:1px solid #F1F1F1;
}
.leftdiv.on{
  background-color:white;
  color:#080808;
}
.leftdiv.on>div{
  border:0px;
}
.icon5{
  width:15px;
  height:15px;
}
.mianleft{
  width:80px;
  box-sizing:border-box;
  overflow:scroll;
}
.shopmain{
  -webkit-box-flex:1;
  display:-webkit-box;
  overflow:hidden;
  margin-bottom:50px;
}
.mainright{
  -webkit-box-flex:1;
  overflow:scroll;
  padding-left:10px;
  position: relative;
}
.foot{
  height:50px;
  line-height:50px;
  background-color:#594C46;
  display:flex;
  position:fixed;
  bottom:0px;
  left:0px;
  width:100%;
}
.foot.on .footicon{
  color:#fff;
}
.foot.on .footmain{
  color:#fff;
}
.foot.on .footright{
  color:#fff;
}
.foot.on .footlogo{
  background-color:#3190E8;
}
.footleft{
  flex:2;
  display:flex;
}
.footright{
  flex:1;
  text-align:center;
  color:#B7B7B7;
  background-color:#61686A;
}
.footright.on{
  background-color:#4CD964;
  color:white;
}
.footlogo{
  text-align:center;
  width:50px;
  height:50px;
  border-radius:50%;
  background-color:#515151;
  margin:0px 10px;
  border:3px solid #444446;
  transform:translatey(-15px)
}
.footicon{
  color:#8a8a8a;
  width:40px;
  height:40px;
  margin-top:7px;
}
.footmain{
  height:50px;
  color:#ADADAD;
  font-size:0.8rem;
}
.itemmain{
  padding-right:10px;
 
}
.iteminfo{
   height:70px;
   display:flex;
   padding:10px 0px;
}
.infoimgbox{
  width:70px;
  height:70px;
  margin-right:5px;
  background-color:red;
}
.infoimgbox>img{
  width:100%;
  height:100%;
}
.inforight{
  flex:1;
}
.ih20{
  height:20px;
  line-height:20px;
}
.ih15{
  height:15px;
  line-height:15px;
}
.addicon{
  width:20px;
  height:20px;
}
.zk{
  background-color:#FF5F15;
  padding:0px 3px;
  color:white;
  border:1px solid #FF5F15;
}
.yh{
  padding:0px 3px;
  color:#FF5F15;
  border:1px solid #FF5F15;
}
.gz{
  display:inline-block;
  background-color:#3190E8;
  color:white;
  padding:0px 2px;
  border-radius:2px;
}

.scoretop{
  display:flex;
  padding:0px 10px 10px 10px;
  border-bottom:10px solid #F5F5F5;
}
.scoretopleft{
  flex:2;
  text-align:center;
}
.scoretopright{
  flex:3;
}
.scoretopright>div{
  display:flex;
}
.scoremain{
  padding:0px 10px;
}
.scoremaintop{
  padding:10px 0px 5px 0px;
  display:flex;
  flex-wrap:wrap;
}
.scoremaintop>div{
  padding:0px 8px;
  border-radius:5px;
  margin-right:5px;
  margin-bottom:5px;
}
.sty1{
  background-color:#EBF5FF;
  color:#9f9f9f;
}
.sty1.on{
  background-color:#3190E8;
  color:#fff;
}
.sty2{
  background-color:#F5F5F5;
  color:#AFAFAF;
}
.scoreitem{
  display:flex;
  margin-top:10px;
}
.scoreitemleft{
  width:50px;
  height:50px;
  margin-right:10px;
}
.scoreitemleft>img{
  width:100%;
  border-radius:50%; 
}
.scoreitemright{
  flex:1;
}
.namebox{
  display:flex;
  flex-wrap: wrap;
}
.namebox>div{
  border:1px solid;
  padding:3px 3px;
  margin-right:5px;
  border-radius:3px;
  margin-bottom:5px;
}
.score{
  padding-top:10px;
  flex:1;
  overflow-y: scroll;
}
.scoreimgbox>img{
  width:4rem;
  height:4rem;
  margin-right:10px;
}
.modal{
  box-sizing:border-box;
  width:100vw;
  height:100vh;
  background-color:#262626;
  position:fixed;
  top:0px;
  left:0px;
  z-index:10;
  
}
.modaltop{
  overflow: scroll;
}
.modaltitle{
  margin:20px 0px;
  text-align:center;
  font-size:1.5rem;
}
.modal_title{
  text-align:center;
  margin:10px;
}
.modal_title>span{
  padding:2px 5px;
  border:1px solid #fff;
  border-radius:5px;
}
.modalfoot{
  width:100%;
  margin-bottom:10px;
  justify-content:flex-end;
  text-align:center;
}
.modalfoot>span{
  display: inline-block;
  border:1px solid #fff;
  border-radius:50%;
  width:30px;
  padding:5px;
  height:30px;
  ling-height:30px;
  font-size:25px;
}
.iteminfofoot{
  position: absolute;
  bottom: 10px;
  right: 0px;
}
.hidden{
  overflow: hidden;
}
.rednum{
   position: absolute;
   top:-3px;
   right:-5px;
   border-radius:50%;
   background-color:red;
   color:white;
   height:18px;
   width:18px;
   text-align:center;
   line-height:18px;
   font-size:12px;
}
.y-4{
  transform: translateY(-4px);
}
.y2{
  transform: translateY(2px);
}
.y4{
  transform: translateY(4px);
  
}
.rednum2{
  position: absolute;
   top:0px;
   right:0px;
   border-radius:50%;
   background-color:red;
   color:white;
   height:15px;
   width:15px;
   text-align:center;
   line-height:15px;
   font-size:12px;
}
.car{
   position: fixed;
   top:0px;
   left:0px;
   width:100vw;
   height:100vh;
   background-color:rgba(0,0,0,0.5);
}
.carmain{
  position:absolute;
  width:100%;
  bottom:0px;
  left:0px;
  padding-bottom:50px;
  max-height:70%;
}
.cartop{
  background-color:#ECEFF1;
}
.carmainbox{
  overflow-y: scroll;
}
</style>

下面的话,就来实现购物车的点击飞小球动画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值