vue 2.0 購物車小球拋物線的範例程式碼,vue範例程式碼

來源:互聯網
上載者:User

vue 2.0 購物車小球拋物線的範例程式碼,vue範例程式碼

本文介紹了vue 2.0 購物車小球拋物線的範例程式碼,分享給大家,具體如下:

備忘:此項目模仿 餓了嗎。我用的是最新的Vue, 視頻上的一些寫法已經被廢棄了。

布局代碼

<div class="ball-container"> <transition name="drop"       v-for="ball in balls"       @before-enter="beforeDrop"       @enter="dropping"       @after-enter="afterDrop">  <div v-show="ball.show" class="ball" v-bind:css="false">   <div class="inner inner-hook" ></div>  </div> </transition></div>

css代碼(使用stylus寫法)

.ball-container .ball  position fixed  left 32px  bottom 22px  z-index 200  transition all 0.4s cubic-bezier(0.49,-0.29,0.75,0.41)  .inner   width 16px   height 16px   border-radius 50%   background-color rgb(0,160,220)   transition all 0.4s linear

js代碼

data() {  return {   balls : [    {     show: false    },    {     show: false    },    {     show: false    },    {     show: false    },    {     show: false    }   ],   dropBalls: []  };},   methods: {  drop(el) {   for(let i = 0; i < this.balls.length; i++) {    let ball = this.balls[i];    if(!ball.show) {     ball.show = true;     ball.el = el;     this.dropBalls.push(ball);     return ;    }   }  }  beforeDrop(el) {   let count = this.balls.length;   while (count--) {    let ball = this.balls[count];    if(ball.show) {     let rect = ball.el.getBoundingClientRect();     let x = rect.left - 32;     let y = -(window.innerHeight - rect.top - 22);     el.style.webkitTransform = `translate3d(0,${y}px,0)`;     el.style.transform = `translate3d(0,${y}px,0)`;     let inner = el.getElementsByClassName('inner-hook')[0];     inner.style.webkitTransform = `translate3d(${x}px,0,0)`;     inner.style.transform = `translate3d(${x}px,0,0)`;    }   }  },  dropping(el) {   /* eslint-disable no-unused-vars */   let rf = el.offsetHeight;   this.$nextTick(() => {    el.style.webkitTransform = 'translate3d(0,0,0)';    el.style.transform = 'translate3d(0,0,0)';    let inner = el.getElementsByClassName('inner-hook')[0];    inner.style.webkitTransform = 'translate3d(0,0,0)';    inner.style.transform = 'translate3d(0,0,0)';   });  },  afterDrop(el){   let ball = this.dropBalls.shift();   if(ball) {    ball.show = false;    el.style.display = 'none';   }  }}

getBoundingClientRect()。方法請閱讀這篇文章http://www.bkjia.com/article/134208.htm

說明:

goods 是一個組件,裡麵包含menu(div) , foods(div), shopcart(購物車組件)。其中foods 包含cartcontrol(即小球組件)

組件之間的通訊:說明:菜單和商品

第1個問題:小球,需要擷取所點擊的商品的數量。

利用Vue的props,將foods值傳遞給cartcontrol。但是這樣有個問題。即子組件更新,無法同步回父組件。且,在子組件中,對food註冊了一個count屬性,此屬性也無法同步回父組件(goods)。

解決方案:

匯入全域的Vue。

利用Vue.set(target,key,value); 對 target註冊count;

第2個問題:小球點擊,將所點擊過的商品數目傳遞給 shopcart。

在goods的 computed:{} 定義一個方法,將該方法以props的方式,傳遞給shopcart。

因為,shopcart,對傳遞過去的資料僅資料運算(不會改變)。因此不用同步會父組件。

第3個問題:購物車小球做拋物線運動。

對於購物車小球做拋物線運動。首先,落點都在購物車,小球則是隨機的。要做拋物線運動,就要擷取,所點擊的 + 號的x,y位置。其次,拋物線運動,只有在enter--> enter-to這段期間有,在leave--> leave-to 期間是沒有的,因此,需要用Vue提供的鉤子函數。

擷取 + 號x,y 位置:

小球(cartcontrol)是子組件。需要把資料傳遞給 goods(父組件)。可以使用Vuex,或者直接使用事件匯流排。對於餓了嗎demo。直接使用事件匯流排。

建立一個 空的Vue。在 cartcontrol 中 ,通過 Bus.$emit(key, ... arg); 註冊一個監聽,然後再父組件 通過Bus.$on(key, function(... arg));監聽此方法。將所操作的 dom 對象傳遞過去即可

Vue提供的鉤子

這裡要說明一點,Vue在他的官網,對於只有過度的js,done是必須的,當我加上done的時候,after-enter方法無法被執行。
還有1個問題,Vue官網推薦,只有過度效果,在做過度動畫的元素上加上v-bind:class='false'。之前沒加,出現了,小球只能在第1次點擊的地方做過度效果。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.