小程序实现 点击加入购物车 红点抛物线飘入

http://ledc.cn/article/9

 

 

小程序实现 点击加入购物车 红点抛物线飘入

实现效果

1、index.wxss

1

2

3

4

5

6

7

8

9

10

11

.good_box {

width30rpx;

height30rpx;

positionfixed;

border-radius: 50%;

overflowhidden;

left50%;

top50%;

z-index99;

background#b02c41;

}

2、index.wxml

1

2

3

<view class="good_box" hidden="{{hide_good_box}}"  style="left: {{bus_x}}px; top: {{bus_y}}px;"></view>

<!-- 点击加  点击事件 -->

<view class="add" bindtap="touchOnGoods">+</view>

3、app.js 里面加入

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

//获取屏幕[宽、高]

screenSize: function () {

var that = this;

wx.getSystemInfo({

success: function (res) {

that.globalData.ww = res.windowWidth;

that.globalData.hh = res.windowHeight;

}

})

},

/**

    * @param sx 起始点x坐标

    * @param sy 起始点y坐标

    * @param cx 控制点x坐标

    * @param cy 控制点y坐标

    * @param ex 结束点x坐标

    * @param ey 结束点y坐标

    * @param part 将起始点到控制点的线段分成的份数,数值越高,计算出的曲线越精确

    * @return 贝塞尔曲线坐标

   */

bezier: function (points, part) {

let sx = points[0]['x'];

let sy = points[0]['y'];

let cx = points[1]['x'];

let cy = points[1]['y'];

let ex = points[2]['x'];

let ey = points[2]['y'];

var bezier_points = [];

// 起始点到控制点的x和y每次的增量

var changeX1 = (cx - sx) / part;

var changeY1 = (cy - sy) / part;

// 控制点到结束点的x和y每次的增量

var changeX2 = (ex - cx) / part;

var changeY2 = (ey - cy) / part;

//循环计算

for (var i = 0; i <= part; i++) {

// 计算两个动点的坐标

var qx1 = sx + changeX1 * i;

var qy1 = sy + changeY1 * i;

var qx2 = cx + changeX2 * i;

var qy2 = cy + changeY2 * i;

// 计算得到此时的一个贝塞尔曲线上的点

var lastX = qx1 + (qx2 - qx1) * i / part;

var lastY = qy1 + (qy2 - qy1) * i / part;

// 保存点坐标

var point = {};

point['x'] = lastX;

point['y'] = lastY;

bezier_points.push(point);

}

//console.log(bezier_points)

return {

'bezier_points': bezier_points

};

},

4、app.js的onLaunch里面加入

1

2

//贝塞尔曲线

this.screenSize();

5、index.js加入

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

data: {

hide_good_box: true,

},

onLoad: function (options) {

this.busPos = {};

this.busPos['x'] = app.globalData.ww/2;

this.busPos['y'] = app.globalData.hh - 10;

console.log('购物车坐标',this.busPos)

},

touchOnGoods: function (e) {

// 如果good_box正在运动

if (!this.data.hide_good_box) return;

this.finger = {};

var topPoint = {};

this.finger['x'] = e.touches["0"].clientX;

this.finger['y'] = e.touches["0"].clientY;

if (this.finger['y'] < this.busPos['y']) {

topPoint['y'] = this.finger['y'] - 150;

else {

topPoint['y'] = this.busPos['y'] - 150;

}

topPoint['x'] = Math.abs(this.finger['x'] - this.busPos['x']) / 2;

if (this.finger['x'] > this.busPos['x']) {

topPoint['x'] = (this.finger['x'] - this.busPos['x']) / 2 + this.busPos['x'];

else {

topPoint['x'] = (this.busPos['x'] - this.finger['x']) / 2 + this.finger['x'];

}

this.linePos = app.bezier([this.finger, topPoint, this.busPos], 20);

this.startAnimation();

},

startAnimation: function () {

var index = 0,

that = this,

bezier_points = that.linePos['bezier_points'],

len = bezier_points.length - 1;

this.setData({

hide_good_box: false,

bus_x: that.finger['x'],

bus_y: that.finger['y']

})

this.timer = setInterval(function () {

index++;

that.setData({

bus_x: bezier_points[index]['x'],

bus_y: bezier_points[index]['y']

})

if (index >= len) {

clearInterval(that.timer);

that.setData({

hide_good_box: true,

})

}

}, 15);

},

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
本课程讲了Vue3+Vue2+Uni-app(uniapp)入门基础与实战,其中还重点讲解了ES6、TypeScript这些基础知识,实战由两大价值5000元的真实企业级项目组成,分别是仿京东电商网站和仿美团微信点餐小程序,同时两大项目代码全部赠送,还赠送前后端架构模板,工作中直接使用。VUE和uni-app是目前热门的前端框架,本课程教你如何快速学会VUE和uni-app并应用到实战,教你如何解决内存泄漏,常用UI库的使用,自己封装组件和插件,正式上线白屏问题,性能优化、解决iphoneX“刘海”兼容性问题、微信支付、微信授权登录,获取位置在地图上显示,获取用户所在的城市和街道信息,微信小程序发布审核等。对正在工作当中或打算学习VUE和uni-app高薪就业的你来说,那么这门课程便是你手中的葵花宝典。学习技巧:学习当中不要只看,一定要多敲代码,如果碰到某一个知识点不是很明白,不要钻牛角尖,千万不要因为一个点,放弃整个森林,接着往下学,硬着头皮开发项目。只要能亲自开发一个完整的项目,你会发现不明白的地方自然而然就明白了,项目做出来就真正的学会了。此vue和uni-app课程以面试和实战为基础进行讲解,每个知识点都会让你知道在实际项目开发中如何使用,学习后,可以开发大型项目,增强逻辑思维,至少让你拥有3年以上开发经验的实力!代码和ppt均可下载!免费提供《企业级完整实战项目接口文档》,绝对可用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值