最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實現(xiàn)商城購物車功能

 更新時間:2017年11月27日 11:47:41   作者:雪月青  
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)商城購物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)商城購物車功能的具體代碼,供大家參考,具體內(nèi)容如下

首先,先上最終的效果圖

效果并不是很好看,但是這不是重點。

首先,我們先看下布局:

<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>

非常常見的布局,微商城購物車隨處可見,先說明下,在這里,實現(xiàn)選中的功能并不是用傳統(tǒng)的label+checkbox實現(xiàn),而是一個單獨的label,目的是為了簡化dom結(jié)構(gòu),在傳統(tǒng)的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

最近在學(xué)習(xí)stylus的使用,所以,就當(dāng)做練習(xí)了。
接下就是javascript了。

export default {
 data () {
  return {
  good_list: [
   {
   title: 'Apple iPhone 6s 16GB 玫瑰金色 移動聯(lián)通電信4G手機',
   img: "http://sc.tywebs.cn/public/upload/goods/2017/04-27/a767693b9a84747f25335f0e33d3d380.jpg",
   num: 2,
   price: 6070.00,
   spec_item:[
    '內(nèi)存:16G',
    '網(wǎng)絡(luò):4G',
    '顏色:玫瑰金'
   ],
   is_selected: false
   },{
   title: 'Apple iPhone 6s 32GB 玫瑰金色 移動聯(lián)通電信4G手機',
   img: 'http://sc.tywebs.cn/public/upload/goods/2017/04-27/a767693b9a84747f25335f0e33d3d380.jpg',
   num: 2,
   price: 4570.00,
   spec_item:[
    '內(nèi)存:32G',
    '網(wǎng)絡(luò):4G',
    '顏色:玫瑰金'
   ],
   is_selected: true
   },{
   title: 'Apple iPhone 6s 8GB 玫瑰金色 移動聯(lián)通電信4G手機',
   img: 'http://sc.tywebs.cn/public/upload/goods/2017/04-27/a767693b9a84747f25335f0e33d3d380.jpg',
   num: 2,
   price: 4870.00,
   spec_item:[
    '內(nèi)存:8G',
    '網(wǎng)絡(luò):4G',
    '顏色:玫瑰金'
   ],
   is_selected: false
   },{
   title: 'Apple iPhone 6s 128GB 玫瑰金色 移動聯(lián)通電信4G手機',
   img: 'http://sc.tywebs.cn/public/upload/goods/2017/04-27/a767693b9a84747f25335f0e33d3d380.jpg',
   num: 2,
   price: 10568.00,
   spec_item:[
    '內(nèi)存:128G',
    '網(wǎng)絡(luò):4G',
    '顏色:玫瑰金'
   ],
   is_selected: true
   },
  ],
  totalPrice: 0,
  totalNum: 0,
  selected_all: false
  }
 },
 mounted: function () {
  this.getTotal();
 },
 watch: {
  'good_list': {
  handler: function (val, oldVal) {
   // console.log(val)
   return val;
  },
  deep: true
  }
 },
 methods: {
  getTotal () {
  this.totalPrice = 0
  this.totalNum = 0
  for (var i = 0; i < this.good_list.length; i++) {
   var _d = this.good_list[i]
   if(_d.is_selected){
   this.totalPrice += _d['price'] * _d['num']
   this.totalNum +=_d['num']
   }
  }
  },
  select_one (index) {
  if(this.good_list[index].is_selected == true){
   this.good_list[index].is_selected = false
  }else{
   this.good_list[index].is_selected = true
  }
  this.getTotal()
  },
  slect_all () {
  if(this.selected_all){
   for (var i = 0; i < this.good_list.length; i++) {
   this.good_list[i].is_selected = false;
   }
   this.selected_all = false   
  }else{
   for (var i = 0; i < this.good_list.length; i++) {
   this.good_list[i].is_selected = true;
   }
   this.selected_all = true   
  }
  this.getTotal()
  },
  reduce (index) {
  if(this.good_list[index].num <= 1) return;
  this.good_list[index].num --
  this.getTotal()
  },
  add (index) {
  this.good_list[index].num ++
  this.getTotal()
  }
 }
 }

這里用mock數(shù)據(jù)來代替后臺請求數(shù)據(jù)的動作,為了方便測試,邏輯比較簡單,首先getTotal這個方法用來計算選中的item的數(shù)量以及總價,select_one用來處理單個選中的邏輯,slect_all 用來處理全部選中以及全部取消選中的操作,reduce用來處理處理減操作,顧名思義add用來處理加的操作。當(dāng)然在真實的開發(fā)中,還會有校驗庫存的動作,每次加減都要校驗庫存。數(shù)據(jù)處理也會更復(fù)雜,但是聞琴弦而知雅意,器原理都是相通的。

github地址傳送門

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3公共組件的main.js的作用、常用代碼解析

    Vue3公共組件的main.js的作用、常用代碼解析

    在Vue3中,公共組件的main.js文件扮演著非常重要的角色,它的作用主要體現(xiàn)在:注冊全局組件、引入公共樣式、引入Vue、引入組件、創(chuàng)建Vue實例、掛載實例,本文將圍繞Vue3公共組件的main.js文件進(jìn)行詳細(xì)的闡述,包括其作用、常用代碼等方面
    2023-08-08
  • vue-infinite-loading2.0 中文文檔詳解

    vue-infinite-loading2.0 中文文檔詳解

    本篇文章主要介紹了vue-infinite-loading2.0 中文文檔詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • vue實現(xiàn)配置全局訪問路徑頭(axios)

    vue實現(xiàn)配置全局訪問路徑頭(axios)

    今天小編就為大家分享一篇vue實現(xiàn)配置全局訪問路徑頭(axios),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue中監(jiān)聽input框獲取焦點及失去焦點的問題

    vue中監(jiān)聽input框獲取焦點及失去焦點的問題

    這篇文章主要介紹了vue中監(jiān)聽input框獲取焦點,失去焦點的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • php+vue3實現(xiàn)點選驗證碼功能

    php+vue3實現(xiàn)點選驗證碼功能

    這篇文章主要介紹了php+vue3實現(xiàn)點選驗證碼,本文通過實例代碼給大家介紹的詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • Vue路由模塊化配置的完整步驟

    Vue路由模塊化配置的完整步驟

    這篇文章主要給大家介紹了關(guān)于Vue路由模塊化配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • vue2之jessibuca視頻插件使用教程詳細(xì)講解

    vue2之jessibuca視頻插件使用教程詳細(xì)講解

    Jessibuca進(jìn)行直播流播放,為用戶帶來便捷、高效的視頻觀看體驗,下面這篇文章主要給大家介紹了關(guān)于vue2之jessibuca視頻插件使用的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • Vue 實現(xiàn)定時刷新與自動更新(示例詳解)

    Vue 實現(xiàn)定時刷新與自動更新(示例詳解)

    在現(xiàn)代 Web 開發(fā)中,定時刷新頁面或定時更新數(shù)據(jù)是一種常見的需求,尤其是在需要與服務(wù)器進(jìn)行定時通信或者展示實時數(shù)據(jù)的場景下,Vue.js 提供了簡單而有效的方法來實現(xiàn)定時刷新和自動更新,本文將介紹幾種常見的定時刷新方式、適用場景、優(yōu)缺點以及性能考慮
    2024-11-11
  • Vue3+Canvas實現(xiàn)簡易的貪吃蛇游戲

    Vue3+Canvas實現(xiàn)簡易的貪吃蛇游戲

    貪吃蛇作為一個經(jīng)典的小游戲,是很多人兒時的記憶,當(dāng)時的掌機、諾基亞手機里面都有它的身影。本文將用Vue3?Canvas來復(fù)刻一下這款游戲,感興趣的可以了解一下
    2022-07-07
  • Vue升級帶來的elementui沖突警告:Invalid prop: custom validator check failed for prop “type“.的解決方案

    Vue升級帶來的elementui沖突警告:Invalid prop: custom va

    在頁面渲染的時候,控制臺彈出大量警告,嚴(yán)重影響控制臺的信息獲取功能,但是頁面基本能正常顯示,這是因為Vue升級帶來的elementui沖突警告: Invalid prop: custom validator check failed for prop “type“.的解決方案,本文給大家介紹了詳細(xì)的解決方案
    2025-04-04

最新評論

略阳县| 唐河县| 河西区| 赤水市| 峡江县| 澜沧| 永福县| 汝南县| 乌拉特前旗| 虞城县| 哈巴河县| 郸城县| 杭锦旗| 茶陵县| 乌什县| 秦皇岛市| 三台县| 华宁县| 临夏县| 阿巴嘎旗| 泉州市| 濮阳县| 镇巴县| 方城县| 岱山县| 海门市| 湘潭市| 隆尧县| 行唐县| 高碑店市| 林西县| 平乡县| 沾益县| 新建县| 汤原县| 伊金霍洛旗| 醴陵市| 衡水市| 汕尾市| 芜湖县| 玛沁县|