[关闭]
@Wangww0925 2019-08-07T07:58:41.000000Z 字数 4343 阅读 271

jquery 淘宝加入购物车 贝塞尔曲线 动画

jQuery


实际需求:

当点击“购买”按钮后,当前货物“飞向”侧边的购物车。设计要求,飞行轨迹必须是一条“曲线”。


解决

TweenMax官方文档

BezierPlugin插件可以很好的解决这个问题。这是greensock.com提供的GreenSock Animation Platform (GSAP)库的一个插件


js 使用

TweenMax-tween核心库
EasePack-缓动库,如果没有特别缓动,可不加载
BezierPlugin-贝塞尔曲线库,必须

  1. <script src="http://cdn.bootcss.com/gsap/1.9.7/TweenMax.min.js"></script>
  2. <script src="http://cdn.bootcss.com/gsap/1.9.7/easing/EasePack.min.js"></script>
  3. <script src="http://cdn.bootcss.com/gsap/1.9.7/plugins/BezierPlugin.min.js"></script>

例子

TweenMax 具体使用请参考官方文档

TweenMax.to()

曲线运动----结合TweenMax可直接产生曲线运动

  1. /*
  2. * target object 是 需要动画的对象
  3. * duration number 是 动画持续时间,单位(秒)
  4. * vars object 是 动画参数(CSS属性、延迟、重复次数等)
  5. */
  6. TweenMax.to( target, duration, vars )

demo

demo下载

重要部分

  1. TweenMax.to($(".box")[0], 1.5, {
  2. bezier: [ // 起点坐标不用放到代码中,以下三个点分别是第2点,第3点及终点
  3. {left: that.left2, top: that.top2},
  4. {left: that.left1, top: that.top1}
  5. ],
  6. width: 20, // 由初始宽度慢慢变成宽20px,【注意此处不需要加单位】
  7. height: 20, // 由初始高度慢慢变成高20px,【注意此处不需要加单位】
  8. ease: Power1.easeOut,
  9. onComplete: function (){} // 动画完成回调
  10. });

html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. /*动画元素*/
  8. .move{
  9. width: 35px;
  10. height: 35px;
  11. background-color: #ff8200;
  12. -webkit-border-radius: 50%;
  13. -moz-border-radius: 50%;
  14. -ms-border-radius: 50%;
  15. -o-border-radius: 50%;
  16. border-radius: 50%;
  17. position: fixed;
  18. }
  19. .btn {
  20. margin: 150px 0px 0px 500px;
  21. }
  22. /*购物车*/
  23. .box {
  24. width: 50px;
  25. height: 100px;
  26. background-color: red;
  27. position: fixed;
  28. top: 50%;
  29. right: 0px;
  30. -webkit-transform: translateY(-50%);
  31. -moz-transform: translateY(-50%);
  32. -ms-transform: translateY(-50%);
  33. -o-transform: translateY(-50%);
  34. transform: translateY(-50%);
  35. }
  36. .box p{
  37. padding: 0px 15px;
  38. text-align: center;
  39. color: #fff;
  40. font-size: 14px;
  41. }
  42. /*加入购物车提示*/
  43. .cart{
  44. display: none;
  45. position: absolute;
  46. top: 10px;
  47. right: 55px;
  48. -webkit-border-radius: 4px;
  49. -moz-border-radius: 4px;
  50. -ms-border-radius: 4px;
  51. -o-border-radius: 4px;
  52. border-radius: 4px;
  53. border: 1px solid #D6D6D6;
  54. background-color: #fff;
  55. cursor: pointer;
  56. }
  57. .cart:after{
  58. position: absolute;
  59. top: 15px;
  60. right: -8px;
  61. display: block;
  62. content: "";
  63. width: 0px;
  64. height: 0px;
  65. border-top: 8px solid transparent;
  66. border-bottom: 8px solid transparent;
  67. border-left: 8px solid #fff;
  68. }
  69. .cart:before{
  70. position: absolute;
  71. top: 14px;
  72. right: -9px;
  73. display: block;
  74. content: "";
  75. width: 0px;
  76. height: 0px;
  77. border-top: 9px solid transparent;
  78. border-bottom: 9px solid transparent;
  79. border-left: 9px solid #D6D6D6;
  80. }
  81. .cart p{
  82. padding: 0px 10px;
  83. white-space: nowrap;
  84. color: #333;
  85. font-size: 12px;
  86. }
  87. </style>
  88. </head>
  89. <body>
  90. <button class="btn">加入购物车</button>
  91. <div class="box">
  92. <p>购物车</p>
  93. <div class="cart">
  94. <p>加入购物车成功</p>
  95. </div>
  96. </div>
  97. </body>
  98. <script src="js/jquery.min.js"></script>
  99. <script src="js/TweenMax.min.js"></script>
  100. <script src="js/BezierPlugin.min.js"></script>
  101. <script src="js/demo.js"></script>
  102. </html>

js

  1. (function (win){
  2. var that = "";
  3. function Tween(startDom, endDom, options){
  4. this.startDom = startDom;
  5. this.endDom = endDom;
  6. this.opts = {
  7. prefix: "move", // 动画类名前缀, 避免类名重复
  8. speed: 1.5, // 动画时间,单位(秒)
  9. width: 20,
  10. height: 20,
  11. ease: Power1.easeOut,
  12. };
  13. this.tweenArr = []; // dom元素名称存放
  14. that = this;
  15. }
  16. // 初始化定位
  17. Tween.prototype.init = function (callback){
  18. that.scrollTop = $("html").scrollTop(); // 获取页面滚动的高度
  19. // 获取动画初始定位
  20. that.top = $(that.startDom).offset().top - that.scrollTop - 40;
  21. that.left = $(that.startDom).offset().left + 80;
  22. // 获取动画终点的定位
  23. that.top1 = $(that.endDom).offset().top - that.scrollTop + 10;
  24. that.left1 = $(that.endDom).offset().left + 8;
  25. // 判断曲线第二个点的定位
  26. that.top2 = that.top1 - that.top1/3*0.6;
  27. that.left2 = (that.left1 - that.left)/3*1.2 + that.left;
  28. // 创建动画元素,并将动画添加到body中
  29. var name = new Date().getTime();
  30. that.name = name;
  31. $("body").append('<div class="' + that.opts.prefix + " " + name + '" style="top:' + that.top + 'px;left:' + that.left + 'px;"></div>');
  32. // 将name添加到数组中,方便动画完毕删除
  33. that.tweenArr.push(name);
  34. that.animate(callback); // 动画
  35. }
  36. // 动画
  37. Tween.prototype.animate = function (callback){
  38. TweenMax.to($("." + that.opts.prefix + "." + that.name)[0], that.opts.speed, {
  39. bezier: [ // 起点坐标不用放到代码中,以下三个点分别是第2点,第3点及终点
  40. {left: that.left2, top: that.top2},
  41. {left: that.left1, top: that.top1}
  42. ],
  43. width: that.opts.width,
  44. height: that.opts.height,
  45. ease: that.opts.ease,
  46. onComplete: function (){ // 动画完成回调
  47. $("." + that.opts.prefix + "." + that.tweenArr[0]).stop().fadeOut(200).remove(); // 动画消失、移除
  48. that.tweenArr.shift(); // 删除第一个元素
  49. callback ? callback() : "";
  50. }
  51. });
  52. }
  53. win.Tween = Tween
  54. })(window)
  55. var timer = ""; // 定时器
  56. $(".btn").unbind();
  57. $(".btn").click(function (){
  58. var tween = new Tween('.btn', '.box');
  59. tween.init(function (){
  60. $(".cart").show(); // 显示加入购票车样式
  61. clearTimeout(timer); // 清除定时
  62. timer = setTimeout(function (){
  63. $(".cart").hide();
  64. }, 10000);
  65. })
  66. })

效果图

具体动画效果可下载demo查看

未点击之前
image_1dcj7ecfr1p9r1sscst5m2kkin9.png-20.2kB

点击
image_1dcj7eofp16co483isc1gmlk5sm.png-13.8kB

动画消失 提示加入购物车成功
image_1dcj7fp1p1q1qbhdp9ppsvokp13.png-21kB


参考文献

BezierPlugin插件(js/jquery)--模拟曲线贝塞尔运动、绘制曲线的实用插件
TweenMax官方文档
TweenMax使用方法

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注