当前位置:   article > 正文

微信小程序飞入购物车动画_添加购物车飞入动画

添加购物车飞入动画

wxml

  1. <view class="container">
  2. <view wx:for='12345678901234567' class="item" bindtap="addshopcar" hover-class="hover">商品{{index}}</view>
  3. </view>
  4. <view class="dot" animation='{{ani}}' hidden="{{!showdot}}">+1</view>
  5. <view class="count">{{count}}</view>
  6. <view class="shopcar" id="shopcar">
  7. <image src="/image/car.png" class="shopcarimg"></image>
  8. </view>

wxss

  1. .container {
  2. height: 100%;
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. justify-content: space-between;
  7. box-sizing: border-box;
  8. }
  9. .item {
  10. width: 720rpx;
  11. height: 100rpx;
  12. display: flex;
  13. flex-direction: row;
  14. align-items: center;
  15. font-weight: bold;
  16. padding-left: 30rpx;
  17. border-bottom: 1rpx solid #efeff4;
  18. }
  19. .hover {
  20. background: #d9d9db;
  21. }
  22. .dot {
  23. top: 0;
  24. left: 0;
  25. position: fixed;
  26. width: 40rpx;
  27. height: 40rpx;
  28. border-radius: 50%;
  29. background: #1dae01;
  30. display: flex;
  31. flex-direction: row;
  32. justify-content: center;
  33. align-items: center;
  34. font-size: 24rpx;
  35. color: white;
  36. }
  37. .shopcar {
  38. position: fixed;
  39. right: 40rpx;
  40. bottom: 60rpx;
  41. }
  42. .shopcarimg {
  43. width: 48rpx;
  44. height: 48rpx;
  45. }
  46. .count {
  47. color: red;
  48. position: fixed;
  49. font-weight: bold;
  50. right: 30rpx;
  51. bottom: 100rpx;
  52. }

js

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. //购物车x坐标
  7. animationx: 0,
  8. //购物车y坐标
  9. animationy: 0,
  10. //是否显示飞行物,默认不显示
  11. showdot: false,
  12. //动画对象
  13. ani: {},
  14. //商品记数
  15. count: 0
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. },
  22. /**
  23. * 生命周期函数--监听页面初次渲染完成
  24. */
  25. onReady: function () {
  26. var that = this
  27. //页面渲染完后获取购物车在页面中的坐标
  28. const query = wx.createSelectorQuery()
  29. query.select('#shopcar').boundingClientRect()
  30. query.selectViewport().scrollOffset()
  31. query.exec(function(res) {
  32. let point = res[0]
  33. //坐标修正,让飞行物可以正好落在购物车正中心,20是飞行物宽度一半然后转化成px
  34. var xtemp = (point.left + point.right) / 2 - 20 / 750 * wx.getSystemInfoSync().windowWidth
  35. var ytemp = (point.top + point.bottom) / 2 - 20 / 750 * wx.getSystemInfoSync().windowWidth
  36. console.log('xtemp : ' + xtemp + ' ytemp : ' + ytemp)
  37. that.setData({
  38. //获取修正后坐标
  39. animationx: xtemp,
  40. animationy: ytemp
  41. })
  42. })
  43. },
  44. addshopcar(e){
  45. var that = this
  46. if(that.data.showdot == true){
  47. return
  48. }
  49. //获取点击点坐标
  50. var touches = e.touches[0]
  51. //坐标修正,同上,这么做是为了让飞行点落到点击的中心
  52. let toptemp = touches.clientY - 20 / 750 * wx.getSystemInfoSync().windowWidth
  53. let lefttemp = touches.clientX - 20 / 750 * wx.getSystemInfoSync().windowWidth
  54. console.log('toptemp : ' + toptemp + ' lefttemp : ' + lefttemp)
  55. var animation1 = wx.createAnimation({
  56. duration: 1,
  57. timingFunction: 'ease'
  58. })
  59. //通过极短的时间让飞行点移动到手指点击位置,同时让飞行点显示出来
  60. animation1.left(lefttemp).top(toptemp).step()
  61. that.setData({
  62. ani: animation1.export(),
  63. showdot: true
  64. })
  65. //然后让飞行点飞行到购物车坐标处,形成添加效果
  66. setTimeout(function(){
  67. const animation = wx.createAnimation({
  68. duration: 1500,
  69. timingFunction: 'ease'
  70. })
  71. //通过Animation的left和top这两个API,将飞行点移动到购物车坐标处
  72. animation.left(that.data.animationx).top(that.data.animationy).step()
  73. that.setData({
  74. ani: animation.export()
  75. })
  76. setTimeout(function () {
  77. var counttemp = that.data.count + 1
  78. that.setData({
  79. showdot: false,
  80. ani: null,
  81. count: counttemp
  82. })
  83. }.bind(this), 1520)//这里也是要稍微延后,后隐藏飞行点,然后清除动画,增加购物计数器
  84. },15)//注意这里要稍微延后,保证前面移动到手指点击处的动画完成
  85. },
  86. /**
  87. * 生命周期函数--监听页面显示
  88. */
  89. onShow: function () {
  90. },
  91. /**
  92. * 生命周期函数--监听页面隐藏
  93. */
  94. onHide: function () {
  95. },
  96. /**
  97. * 生命周期函数--监听页面卸载
  98. */
  99. onUnload: function () {
  100. },
  101. /**
  102. * 页面相关事件处理函数--监听用户下拉动作
  103. */
  104. onPullDownRefresh: function () {
  105. },
  106. /**
  107. * 页面上拉触底事件的处理函数
  108. */
  109. onReachBottom: function () {
  110. },
  111. /**
  112. * 用户点击右上角分享
  113. */
  114. onShareAppMessage: function () {
  115. }
  116. })

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/654203
推荐阅读
相关标签
  

闽ICP备14008679号