hx-jump-ball.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view class="hx-jump-ball">
  3. <view class="ballBox" :style="{transition:'all '+adminTime +'ms' + ' linear','z-index':index,left: ctnBallX+'px',top: ctnBallY+'px',opacity:ctnOpacity}">
  4. <view class="ballOuter"
  5. :style="{transition:'all '+adminTime +'ms' + ' linear',left: ballX+'px',top: ballY+'px',opacity:opacity,width:ballWidth + 'px',height:ballHeight + 'px','background-color':backgroundColor,'background-image': backgroundImage ?`url(${backgroundImage})`: ''}">
  6. </view>
  7. </view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. name: "hx-jump-ball",
  13. data() {
  14. return {
  15. flag: false,
  16. ballBoxAnimation: null,
  17. ballOuterAnimation: null,
  18. ballBoxAnimationData: {},
  19. ballOuterAnimationData: {},
  20. //小球透明
  21. opacity: 1,
  22. ballX: 0,
  23. ballY: 0,
  24. ctnOpacity: 1,
  25. ctnBallX: 0,
  26. ctnBallY: 0,
  27. adminTime: 500,
  28. };
  29. },
  30. props: {
  31. //小球宽度
  32. ballWidth: {
  33. type: Number,
  34. default: 15
  35. },
  36. //小球高度
  37. ballHeight: {
  38. type: Number,
  39. default: 15
  40. },
  41. //小球颜色
  42. backgroundColor: {
  43. type: String,
  44. default: "reg"
  45. },
  46. //图片
  47. backgroundImage: {
  48. type: String,
  49. default: ""
  50. },
  51. //小球的堆叠顺序
  52. index: {
  53. type: Number,
  54. default: 0
  55. },
  56. //开始动画
  57. start: {
  58. type: Number,
  59. default: 1
  60. },
  61. //html元素class名称,[起点元素,终点元素]
  62. element:{
  63. type: Array,
  64. default(){
  65. return []
  66. }
  67. },
  68. //下落速度 ms毫秒
  69. speed:{
  70. type: Number,
  71. default: 800
  72. },
  73. //贝塞尔曲线
  74. bezier:{
  75. type: String,
  76. default: "cubic-bezier(.6,-0.63,.94,.71)"
  77. }
  78. },
  79. watch:{
  80. start(val,oldVal) {
  81. var that = this;
  82. if(!that.element){
  83. return;
  84. }
  85. if(this.flag){
  86. return;
  87. }
  88. that.flag = !that.flag;
  89. that.getElementCoordinate(that.element[0],that.element[1]);
  90. }
  91. },
  92. created() {
  93. this.ballBoxAnimation = uni.createAnimation({
  94. duration: 0,
  95. timingFunction: this.bezier,
  96. delay: 0
  97. });
  98. this.ballOuterAnimation = uni.createAnimation({
  99. duration: 0,
  100. timingFunction: "linear",
  101. delay: 0
  102. });
  103. //this.setEnd();
  104. //初始化位置
  105. },
  106. methods:{
  107. //获取元素坐标
  108. getElementCoordinate(startElement,endElement){
  109. let that = this;
  110. const query = uni.createSelectorQuery();
  111. let nodesRef = query.select(startElement);
  112. nodesRef.fields({
  113. id: true,
  114. rect: true,
  115. size: true
  116. }, res => {
  117. if(!res){
  118. that.flag = !that.flag;
  119. that.$emit("msg",{code: 100, error: '未获取到起始元素位置'})
  120. return ;
  121. }
  122. const SLeft = res.left + ((res.width + that.ballWidth ) / 2 - that.ballWidth);
  123. const STop = res.bottom - ((res.height - that.ballHeight ) / 2 + that.ballHeight);
  124. let nodesRef2 = uni.createSelectorQuery().select(endElement);
  125. nodesRef2.fields({
  126. id: true,
  127. rect: true,
  128. size: true
  129. }, res2 => {
  130. if(!res2){
  131. that.$emit("msg",{code: 101, error: '未获取到结束元素位置'})
  132. return ;
  133. }
  134. //计算出元素的中心坐标
  135. let ELeft = res2.left + ((res2.width + that.ballWidth ) / 2 - that.ballWidth);
  136. let ETop = res2.bottom - ((res2.height - that.ballHeight ) / 2 + that.ballHeight);
  137. that.startAnimation(SLeft,STop,ELeft,ETop);
  138. }).exec()
  139. }).exec()
  140. },
  141. //开始动画
  142. startAnimation(SLeft,STop,ELeft,ETop){
  143. let that = this;
  144. this.adminTime = 500
  145. let jumpDistance = SLeft - ELeft;
  146. this.opacity= 1;
  147. this.ballX = SLeft - ELeft;
  148. this.ballY= 0;
  149. this.ctnOpacity= 1;
  150. this.ctnBallX= ELeft;
  151. this.ctnBallY= STop;
  152. setTimeout(function() {
  153. this.adminTime = 0;
  154. this.opacity= 1;
  155. this.ballX = 0;
  156. this.ballY= 0;
  157. this.ctnOpacity= 1;
  158. this.ctnBallX= ELeft;
  159. this.ctnBallY= ETop;
  160. }, 100);
  161. // 暂时注释掉,待uniapp修复bug后再调整
  162. //根坐标
  163. // this.ballBoxAnimation.translate3d(ELeft,STop,0).opacity(1).step({duration: 800});
  164. // this.ballBoxAnimation.translate3d(ELeft,ETop,0).step({duration: 800});
  165. // this.ballBoxAnimationData = this.ballBoxAnimation.export();
  166. // console.log('根坐标执行玩');
  167. // //相对根的坐标
  168. // this.ballOuterAnimation.translate3d(jumpDistance,0,0).opacity(1).step({duration: 800});
  169. // this.ballOuterAnimation.translate3d(0,0,0).step({duration: 800});
  170. // this.ballOuterAnimationData = this.ballOuterAnimation.export();
  171. // console.log('相对根的坐标');
  172. // setTimeout(function() {
  173. // console.log("动画完成");
  174. // that.flag = !that.flag;
  175. // }, 800);
  176. //初始化位置
  177. //that.setStart(SLeft,STop,ELeft,ETop);
  178. //因为uniapp step()有bug,所以必须要延时执行
  179. // that.ballBoxAnimation = uni.createAnimation({
  180. // duration: 0,
  181. // timingFunction: that.bezier,
  182. // delay: 0
  183. // });
  184. // that.ballOuterAnimation = uni.createAnimation({
  185. // duration: 0,
  186. // timingFunction: "linear",
  187. // delay: 0
  188. // });
  189. //根坐标
  190. //that.ballBoxAnimation.translate3d(ELeft,STop,0).opacity(1).step({duration: 0});
  191. //that.ballBoxAnimation.opacity(1).translate3d(ELeft,ETop,0).step({duration: 1500});
  192. //that.ballBoxAnimation.opacity(0).step({duration: 0});
  193. //that.ballBoxAnimationData = that.ballBoxAnimation.export();
  194. //相对根的坐标
  195. //that.ballOuterAnimation.translate3d(SLeft - ELeft,0,0).opacity(1).step({duration: 0});
  196. //that.ballOuterAnimation.opacity(1).translate3d(0,0,0).step({duration: 1500});
  197. //that.ballOuterAnimation.opacity(0).step({duration: 0});
  198. //that.ballOuterAnimationData = that.ballOuterAnimation.export();
  199. setTimeout(function() {
  200. that.flag = !that.flag;
  201. that.$emit("msg",{code:0,status:true});
  202. }, that.speed);
  203. },
  204. ballIni(){
  205. },
  206. //动画开始前初始化小球位置并显示小球
  207. setStart(SLeft,STop,ELeft,ETop){
  208. this.ballBoxAnimation.translate3d(ELeft,STop,0).opacity(1).step({duration: 0});
  209. this.ballBoxAnimationData = this.ballBoxAnimation.export();
  210. this.ballOuterAnimation.translate3d(SLeft - ELeft,0,0).opacity(1).step({duration: 0});
  211. this.ballOuterAnimationData = this.ballOuterAnimation.export();
  212. },
  213. //隐藏小球
  214. setEnd(){
  215. this.ballBoxAnimation.opacity(0).step({duration: 0});
  216. this.ballBoxAnimationData = this.ballBoxAnimation.export();
  217. this.ballOuterAnimation.opacity(0).step({duration: 0});
  218. this.ballOuterAnimationData = this.ballOuterAnimation.export();
  219. }
  220. }
  221. }
  222. </script>
  223. <style>
  224. .ballBox{
  225. position: fixed;
  226. left: 0;
  227. top: 0;
  228. z-index: 9;
  229. /* 用颜色来演示用原理 */
  230. background-color: #4CD964;
  231. height:30rpx;
  232. width:30rpx;
  233. }
  234. .ballOuter {
  235. background:red;
  236. height:100%;
  237. width:100%;
  238. border-radius: 50%;
  239. background-size: 100% 100%;
  240. background-position: center;
  241. }
  242. </style>