Vue实现购物小球抛物线的方法实例
程序员文章站
2022-03-15 08:45:15
本文介绍的是利用vue实现购物小球抛物线的相关内容,下面话不多说了,来一起看看详细的介绍吧css片段 .shop{ position: fixed; top: 300px; left: 40px...
本文介绍的是利用vue实现购物小球抛物线的相关内容,下面话不多说了,来一起看看详细的介绍吧
css片段
.shop{ position: fixed; top: 300px; left: 40px; } .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; } .cart{ position: fixed; bottom: 22px; left: 32px; width: 30px; height: 30px; background-color: rgb(0,160,220); color: rgb(255,255,255); }
html片段
<div id="app"> <ul class="shop"> <li v-for="item in items"> <span>{{item.text}}</span> <span>{{item.price}}</span> <button @click="additem">添加</button> </li> </ul> <div class="cart" style="">{{count}}</div> <div class="ball-container"><!--小球--> <div v-for="ball in balls"> <transition name="drop" @before-enter="beforedrop" @enter="dropping" @after-enter="afterdrop"> <div class="ball" v-show="ball.show"> <div class="inner inner-hook"></div> </div> </transition> </div> </div> </div>
js片段
<script> new vue({ el:"#app", data:{ count: 0, items:[ { text: "苹果", price: 15 }, { text: "香蕉", price: 15 } ], balls: [ //小球 设为3个 { show: false }, { show: false }, { show: false }, ], dropballs:[], }, methods:{ additem(event){ this.drop(event.target); this.count ++; }, 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); //获取y el.style.display = ''; el.style.webkittransform = 'translatey('+y+'px)'; //translatey el.style.transform = 'translatey('+y+'px)'; let inner = el.getelementsbyclassname('inner-hook')[0]; inner.style.webkittransform = 'translatex('+x+'px)'; inner.style.transform = 'translatex('+x+'px)'; } } }, dropping(el, done) { /*重置小球数量 样式重置*/ let rf = el.offsetheight; 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)'; el.addeventlistener('transitionend', done); }, afterdrop(el) { /*初始化小球*/ let ball = this.dropballs.shift(); if (ball) { ball.show=false; el.style.display = 'none'; } } } }) </script>
transtion属性可以查看官方文档,具体位置可以通过调试获得。
到此这篇关于vue实现购物小球抛物线的文章就介绍到这了,更多相关vue购物小球抛物线内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!