vue实现商城购物车功能

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

首先,先上最终的效果图

效果并不是很好看,但是这不是重点。

首先,我们先看下布局:

<template>
 <div class="shopcar" id="demo04">
 <div class="header-title">
  <h3>购物车</h3>
 </div>
 <div class="car-list">
  <ul>
  <li class="car-item" v-for="(item,index) in good_list">
   <div class="input-block">
   <label class="input-label" :class="{active: item.is_selected}" :for="'car-checkbox-'+index" @click="select_one(index)"></label>
   </div>
   <div class="car-item-content">
   <div class="car-img">
    <img :src="item.img" />
   </div>
   <div class="car-cont">
    <h3>{{item.title}}</h3>
    <div class="cat-desc">
    <span v-for="spec in item.spec_item">{{spec}}</span>
    </div>
   </div>
   <div class="num">
    <span @click="reduce(index)">-</span>
    <span>{{item.num}}</span>
    <span @click="add(index)">+</span>
   </div>
   <div class="car-price">
    <span class="car-price">¥{{item.price}}</span>
    <span class="car-num">X{{item.num}}</span>
   </div>
   </div>
  </li>
  </ul>
 </div>
 <div class="car-footer">
  <div class="foot-car">
  <label for="foot-check" class="input-label" :class="{active: selected_all}" @click="slect_all"></label>
  </div>
  <div class="total-cont">
  <span>总价:{{totalPrice}}</span>
  <span>共{{totalNum}}件</span>
  </div>
  <div class="btn-box">
  <button>立即下单</button>
  </div>
 </div>
 </div>
</template>

非常常见的布局,微商城购物车随处可见,先说明下,在这里,实现选中的功能并不是用传统的label+checkbox实现,而是一个单独的label,目的是为了简化dom结构,在传统的jq项目或者zepto项目中,一般会会这样写,主要是为了方便操作demo,但是vue项目完全不用考虑dom的操作,怎么方便怎么来就ok。

在加些简单的样式,

 .header-title
 height 42px
 line-height 42px
 background #f5f5f5
 border-bottom 1px solid #ddd
 .header-title h3
 font-weight normal
 text-align center
 .car-list 
 background #f2f2f2
 margin-top 12px
 padding 8px
 .car-item
 border-bottom 1px solid #ddd
 position relative
 height 76px
 overflow hidden
 .car-checkbox
 display none
 .input-block
 width 30px
 float left
 height 55px
 line-height 55px
 .input-label
 cursor pointer 
 position absolute 
 width 18px 
 height 18px 
 top 18px 
 left 0 
 background #fff 
 border:2px solid #ccc
 border-radius 50%
 .input-label:after 
 opacity 0 
 content '' 
 position absolute 
 width 9px 
 height 5px 
 background transparent 
 top 6px 
 left 6px 
 border 2px solid #333 
 border-top none 
 border-right none 
 -webkit-transform rotate(-45deg) 
 -moz-transform rotate(-45deg) 
 -o-transform rotate(-45deg) 
 -ms-transform rotate(-45deg) 
 transform rotate(-45deg) 
 .car-img
 width 64px
 height 64px
 float left
 overflow hidden
 .car-img img
 width 100%
 .input-label.active
 background #F15A24
 .car-cont 
 margin-left 100px
 .car-cont h3
 font-weight normal
 line-height 24px
 font-size 14px
 .car-price 
 position absolute
 right 12px
 bottom 0px
 width 40px
 height 40px
 text-align right
 .car-price span
 display block
 height 24px
 line-height 24px
 width 100%
 .car-footer 
 height 60px
 background #f5f5f5
 position fixed
 bottom 0
 left 0
 right 0
 .foot-car
 width 42px
 height 60px
 float left
 margin-left 12px
 position relative
 .total-cont 
 height 60px
 line-height 60px
 font-size 16px
 float left
 .total-cont span
 display inline-block 
 margin-left 12px
 .btn-box
 float right
 height 60px
 line-height 60px
 .btn-box button
 height 100%
 width: 100px
 border none
 background #F15A24
 color #fff
 font-size 16px
 .num
 position absolute
 top 28px
 right 25px
 width 120px
 .num span
 display inline-block
 width 28px
 height 28px
 float left
 text-align center
 line-height 28px
 border 1px solid #ddd
 font-size 14px
      

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

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