1. 程式人生 > >餓了麼專案---11、新增商品到購物車的聯動功能(1)

餓了麼專案---11、新增商品到購物車的聯動功能(1)

export default { name:'cartshop', props: { //選中商品 selectFoods:{ type:Array }, //起送價 minPrice:{ type:Number }, //配送費 deliveryPrice:{ type:Number
} }, data(){ return{ balls:[{ show:false },{ show:false },{ show:false },{ show:false },{ show:false }], dropBalls:[] } }, computed:{ AllPrice:function
(){
let ap = 0; this.selectFoods.forEach((item)=>{ ap +=item.price*item.count; }) return ap }, foodCount:function(){ let count = 0; this.selectFoods.forEach((item)=>{ count +=item.count; }) return
count; }, DescPric:function(){ if(this.AllPrice==0){ return '¥'+this.minPrice+'起送'; }else if(this.AllPrice<this.minPrice){ let price =this.minPrice -this.AllPrice; return '還差¥'+price+'起送'; }else if(this.AllPrice>this.minPrice){ return '結算'; } } }, 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; } } }, beforeEnter(el){ let count = this.balls.length; while (count--) { let ball = this.balls[count]; if (ball.show===true) { //cartcontrol元件的加號DOM物件相對視窗的距離 let rect = ball.el.getBoundingClientRect(); let x =rect.left - 32; let y = -(window.innerHeight - rect.top - 23); el.style.display = ''; //小球縱軸做動畫 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)`; } } }, enter(el){ //觸發瀏覽器重繪 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)'; }); }, afterEnter(el){ let ball = this.dropBalls.shift(); if (ball) { el.style.display = 'none'; ball.show = false; } } } }