小程序飞入购物车特效

小程序加入购物车动画效果:点击商品,出现一个小的商品图,呈现曲线(贝塞尔曲线/抛物线)飞向购物车的图标里。

app.js
App({onLaunch: function () {this.screenSize();},screenSize: function () {var that = this;wx.getSystemInfo({success: function (res) {//可视窗口宽度var ww = res.windowWidth;//可视窗口高度var hh = res.windowHeight;that.globalData.ww = ww;that.globalData.hh = hh;}})},bezier: function (points, times) {// 0、以3个控制点为例,点A,B,C,AB上设置点D,BC上设置点E,DE连线上设置点F,则最终的贝塞尔曲线是点F的坐标轨迹。// 1、计算相邻控制点间距。// 2、根据完成时间,计算每次执行时D在AB方向上移动的距离,E在BC方向上移动的距离。// 3、时间每递增100ms,则D,E在指定方向上发生位移, F在DE上的位移则可通过AD/AB = DF/DE得出。// 4、根据DE的正余弦值和DE的值计算出F的坐标。// 邻控制AB点间距var bezier_points = [];var points_D = [];var points_E = [];const DIST_AB = Math.sqrt(Math.pow(points[1]['x'] - points[0]['x'], 2) + Math.pow(points[1]['y'] - points[0]['y'], 2));// 邻控制BC点间距const DIST_BC = Math.sqrt(Math.pow(points[2]['x'] - points[1]['x'], 2) + Math.pow(points[2]['y'] - points[1]['y'], 2));// D每次在AB方向上移动的距离const EACH_MOVE_AD = DIST_AB / times;// E每次在BC方向上移动的距离 const EACH_MOVE_BE = DIST_BC / times;// 点AB的正切const TAN_AB = (points[1]['y'] - points[0]['y']) / (points[1]['x'] - points[0]['x']);// 点BC的正切const TAN_BC = (points[2]['y'] - points[1]['y']) / (points[2]['x'] - points[1]['x']);// 点AB的弧度值const RADIUS_AB = Math.atan(TAN_AB);// 点BC的弧度值const RADIUS_BC = Math.atan(TAN_BC);// 每次执行for (var i = 1; i <= times; i++) {// AD的距离var dist_AD = EACH_MOVE_AD * i;// BE的距离var dist_BE = EACH_MOVE_BE * i;// D点的坐标var point_D = {};point_D['x'] = dist_AD * Math.cos(RADIUS_AB) + points[0]['x'];point_D['y'] = dist_AD * Math.sin(RADIUS_AB) + points[0]['y'];points_D.push(point_D);// E点的坐标var point_E = {};point_E['x'] = dist_BE * Math.cos(RADIUS_BC) + points[1]['x'];point_E['y'] = dist_BE * Math.sin(RADIUS_BC) + points[1]['y'];points_E.push(point_E);// 此时线段DE的正切值var tan_DE = (point_E['y'] - point_D['y']) / (point_E['x'] - point_D['x']);// tan_DE的弧度值var radius_DE = Math.atan(tan_DE);// 地市DE的间距var dist_DE = Math.sqrt(Math.pow((point_E['x'] - point_D['x']), 2) + Math.pow((point_E['y'] - point_D['y']), 2));// 此时DF的距离var dist_DF = (dist_AD / DIST_AB) * dist_DE;// 此时DF点的坐标var point_F = {};point_F['x'] = dist_DF * Math.cos(radius_DE) + point_D['x'];point_F['y'] = dist_DF * Math.sin(radius_DE) + point_D['y'];bezier_points.push(point_F);}return {'bezier_points': bezier_points};},globalData: {}
})
index.js
//index.js
//获取应用实例
var app = getApp()
Page({data: {imgUrls: ['../../images/products/1.jpg','../../images/products/2.jpg','../../images/products/3.jpg','../../images/products/4.jpg','../../images/products/5.jpg','../../images/products/6.jpg','../../images/products/7.jpg','../../images/products/8.jpg','../../images/products/9.jpg','../../images/products/10.jpg',],indicatorDots: true,autoplay: true,interval: 1500,duration: 1000,hideCount: true, //角标初始是隐藏的count: 0, //角标数hide_good_box: true,feiBox: ""},onLoad: function() {var that = this;//可视窗口x,y坐标this.busPos = {};this.busPos['x'] = app.globalData.ww * .85;this.busPos['y'] = app.globalData.hh * .85;},//点击商品触发的事件touchOnGoods: function(e) {//把点击每一项的对应的商品图保存下来,就是飞向购物车的图片this.setData({feiBox: this.data.imgUrls[e.currentTarget.dataset.idx]})// 如果good_box正在运动if (!this.data.hide_good_box) return;//当前点击位置的x,y坐标this.finger = {};var topPoint = {};this.finger['x'] = e.touches["0"].clientX;this.finger['y'] = e.touches["0"].clientY - 20;if (this.finger['y'] < this.busPos['y']) {topPoint['y'] = this.finger['y'] - 150;} else {topPoint['y'] = this.busPos['y'] - 150;}if (this.finger['x'] < this.busPos['x']) {topPoint['x'] = Math.abs(this.finger['x'] - this.busPos['x']) / 2 + this.finger['x'];} else {topPoint['x'] = this.busPos['x'];this.finger['x'] = this.busPos['x']}this.linePos = app.bezier([this.finger, topPoint, this.busPos], 30);this.startAnimation();},//开始动画startAnimation: function() {var index = 0,that = this,bezier_points = that.linePos['bezier_points'];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 >= 28) {clearInterval(that.timer);that.setData({hide_good_box: true,hideCount: false,count: that.data.count += 1})}}, 33);}
})
index.wxml
<!--index.wxml-->
<view class="container"><!--商品图片列表区域 --><scroll-view><view catchtap="touchOnGoods" wx:key="keys" class="goods" hover-class="goods_hover" data-idx="{{index}}" wx:for="{{imgUrls}}" style="position:relative"><image src="{{item}}" ></image></view></scroll-view><!-- 购物车图标 和 角标数字--><!-- <view class="{{needAni ? 'bus scale': 'bus'}}"> --><view class="bus"><image src="../../images/shopCart.png"></image><view class="count" hidden="{{hideCount}}">{{count}}</view></view><!-- 加购物车时飞的图片 --><view class="good_box" hidden="{{hide_good_box}}"  style="left: {{bus_x}}px; top: {{bus_y}}px;"><image src="{{feiBox}}"></image></view>
</view>
index.wxss
/**index.wxss**/scroll-view {margin-top: 5px;
}.goods {width: 48%;margin: 1%;float: left;background: #fff;
}.goods_hover {transform: scale(0.98);
}.goods image {width: 100%;height: 150px;
}.bus {position: fixed;left: 85%;top: 85%;background: #fff;border-radius: 50%;
}.bus image {width: 62px;height: 62px;position: absolute;left: 50%;top: 50%;margin: -4px;
}.count {display: block;width: 20px;height: 20px;line-height: 20px;text-align: center;font-size: 12px;background: #ff4611;border-radius: 10px;color: #fff;position: absolute;left: 34px;top: 0;
}.scale {background: rgba(120, 188, 255, 0.3);border: 1px solid rgba(0, 116, 255, 0.4);
}.scale image {transform: scale(1.2);
}.good_box {width: 40px;height: 40px;position: fixed;border-radius: 50%;overflow: hidden;left: 50%;top: 50%;z-index: +99;border: 1px solid #fff;background: rgba(120, 188, 255, 0.2);
}.good_box image {display: block;width: 100%;height: 100%;
}

项目下载地址

小程序飞入购物车特效相关推荐

  1. 微信小程序(购物车)--在wxml中设置保留小数位数

    微信小程序(购物车)–在wxml中设置保留小数位数 一.在该页面文件夹下新建一个wxs后缀的文件 var filters = {toFix: function (value) {return valu ...

  2. 微信小程序 - 实现购物车结算

    示例源码下载:小程序-实现购物车结算

  3. 微信小程序实战 购物车功能

    代码地址如下: http://www.demodashi.com/demo/12400.html 一.准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.com ...

  4. 微信小程序实现购物车功能,包含完整小程序代码和运行效果截图

    微信小程序实现购物车功能,在商场比较常见,今天刚刚做好,效果不错. 下面从js文件,json文件,wxml文件和wxss文件,分享给大家. 直接上代码: 目录 1.index.js文件内容 2.ind ...

  5. 微信小程序实现购物车页面

    微信小程序实现购物车页面 先来弄清楚购物车的需求. 单选.全选和取消,而且会随着选中的商品计算出总价 单个商品购买数量的增加和减少 删除商品.当购物车为空时,页面会变为空购物车的布局 根据设计图,我们 ...

  6. 小程序实现购物车商品飞入效果-贝塞尔曲线动画

    看过很多文章,几乎都是JS计算,控制三个点实现,对我菜鸟的我来说太难了,我这边是用来position+CSS3 cubic-bezier() 函数,既然是分享,我会尽量贴完整代码,害,有的博客,没头没 ...

  7. 微信小程序之购物车功能

    前言 以往的购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消, ...

  8. 小程序判断数组的index是否为空_微信小程序之购物车功能(仅学习)

    购物车,基本都是通过大量的 DOM 操作来实现.微信小程序其实跟 vue.js 的用法非常像,接下来就看看小程序可以怎样实现购物车功能. 需求 先来弄清楚购物车的需求. 单选.全选和取消,而且会随着选 ...

  9. 微信小程序侧边栏滑动特效(左右滑动)

    侧边栏滑动是很常见的功能,但是小程序出来不久,很多特效还没有成熟案例,只能原生重写,所以今天为大家带来4个漂亮的侧边栏特效~~ 侧边栏特效一 先看效果: wxml: <!--page/one/i ...

最新文章

  1. 带你刷burpsuite官方网络安全学院靶场(练兵场)之客户端漏洞——跨站请求伪造(CSRF)专题
  2. Linux下自动检测USB热插拔
  3. 数据加密类型及创建和申请CA证书
  4. 网易2019实习生招聘题目 被3整除
  5. Redis架构及分片管理
  6. c++求n的几次方_数理统计|笔记整理(E)——Ch7-C习题课
  7. SQL server 2005 如何批量修改架构名(包括表名和存储过程名) .
  8. 一次关于使用status作为变量引发的bug及思考
  9. 基于dpdk的用户态协议栈f-stack实现分析
  10. 背包问题之完全背包算法详解
  11. 微信开放平台、微信公众平台和微信商户平台
  12. 【Unity 3D】简易小车游戏
  13. 火狐书签栏 谷歌_适用于Firefox的Google工具栏等
  14. linux 利用缓存文件.swp恢复源文件
  15. WP7 个人股票软件 GilStock v1.0 正式版
  16. vue项目添加ico(已修改)
  17. win10系统连接wifi后可以上网但是显示【无Internet,安全】解决 and Maple初始化失败问题
  18. 【计算机网络】B类IP地址
  19. IE-LAB网络实验室:思科ccie,sp ccie 思科ccnp CCIE与HCIE哪个更好找工作
  20. 全球及中国职业教育市场创新发展与投资决策建议报告2022版

热门文章

  1. 如何腾出计算机内存,电脑C盘又飘红?教你这样清理内存,可以轻松腾出大量空间...
  2. php 数据相加,PHP数组合并之array_merge和数组相加
  3. 怎么用ubuntu进入python_ubuntu 下python环境的切换使用
  4. shell题库选择题_shell 练习题
  5. socket编程简单Demo讲解及源码分享(C# Winform 内网)
  6. 构建官方CoreOS COSA 镜像并构建 CoreOS
  7. 节点大小可变的环形队列实现
  8. 服务器体系(SMP, NUMA, MPP)与共享存储器架构(UMA和NUMA)
  9. 结构体数组(SoA)与数组结构体(AoS)
  10. 照片转3d模型_三星使用AI将照片转换为3D模型