@Wangww0925
2019-08-07T07:58:41.000000Z
字数 4343
阅读 370
jQuery
当点击“购买”按钮后,当前货物“飞向”侧边的购物车。设计要求,飞行轨迹必须是一条“曲线”。
BezierPlugin插件可以很好的解决这个问题。这是greensock.com提供的GreenSock Animation Platform (GSAP)库的一个插件
TweenMax-tween核心库
EasePack-缓动库,如果没有特别缓动,可不加载
BezierPlugin-贝塞尔曲线库,必须
<script src="http://cdn.bootcss.com/gsap/1.9.7/TweenMax.min.js"></script><script src="http://cdn.bootcss.com/gsap/1.9.7/easing/EasePack.min.js"></script><script src="http://cdn.bootcss.com/gsap/1.9.7/plugins/BezierPlugin.min.js"></script>
TweenMax 具体使用请参考官方文档
曲线运动----结合TweenMax可直接产生曲线运动
/** target object 是 需要动画的对象* duration number 是 动画持续时间,单位(秒)* vars object 是 动画参数(CSS属性、延迟、重复次数等)*/TweenMax.to( target, duration, vars )
demo下载
重要部分
TweenMax.to($(".box")[0], 1.5, {bezier: [ // 起点坐标不用放到代码中,以下三个点分别是第2点,第3点及终点{left: that.left2, top: that.top2},{left: that.left1, top: that.top1}],width: 20, // 由初始宽度慢慢变成宽20px,【注意此处不需要加单位】height: 20, // 由初始高度慢慢变成高20px,【注意此处不需要加单位】ease: Power1.easeOut,onComplete: function (){} // 动画完成回调});
html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style>/*动画元素*/.move{width: 35px;height: 35px;background-color: #ff8200;-webkit-border-radius: 50%;-moz-border-radius: 50%;-ms-border-radius: 50%;-o-border-radius: 50%;border-radius: 50%;position: fixed;}.btn {margin: 150px 0px 0px 500px;}/*购物车*/.box {width: 50px;height: 100px;background-color: red;position: fixed;top: 50%;right: 0px;-webkit-transform: translateY(-50%);-moz-transform: translateY(-50%);-ms-transform: translateY(-50%);-o-transform: translateY(-50%);transform: translateY(-50%);}.box p{padding: 0px 15px;text-align: center;color: #fff;font-size: 14px;}/*加入购物车提示*/.cart{display: none;position: absolute;top: 10px;right: 55px;-webkit-border-radius: 4px;-moz-border-radius: 4px;-ms-border-radius: 4px;-o-border-radius: 4px;border-radius: 4px;border: 1px solid #D6D6D6;background-color: #fff;cursor: pointer;}.cart:after{position: absolute;top: 15px;right: -8px;display: block;content: "";width: 0px;height: 0px;border-top: 8px solid transparent;border-bottom: 8px solid transparent;border-left: 8px solid #fff;}.cart:before{position: absolute;top: 14px;right: -9px;display: block;content: "";width: 0px;height: 0px;border-top: 9px solid transparent;border-bottom: 9px solid transparent;border-left: 9px solid #D6D6D6;}.cart p{padding: 0px 10px;white-space: nowrap;color: #333;font-size: 12px;}</style></head><body><button class="btn">加入购物车</button><div class="box"><p>购物车</p><div class="cart"><p>加入购物车成功</p></div></div></body><script src="js/jquery.min.js"></script><script src="js/TweenMax.min.js"></script><script src="js/BezierPlugin.min.js"></script><script src="js/demo.js"></script></html>
js
(function (win){var that = "";function Tween(startDom, endDom, options){this.startDom = startDom;this.endDom = endDom;this.opts = {prefix: "move", // 动画类名前缀, 避免类名重复speed: 1.5, // 动画时间,单位(秒)width: 20,height: 20,ease: Power1.easeOut,};this.tweenArr = []; // dom元素名称存放that = this;}// 初始化定位Tween.prototype.init = function (callback){that.scrollTop = $("html").scrollTop(); // 获取页面滚动的高度// 获取动画初始定位that.top = $(that.startDom).offset().top - that.scrollTop - 40;that.left = $(that.startDom).offset().left + 80;// 获取动画终点的定位that.top1 = $(that.endDom).offset().top - that.scrollTop + 10;that.left1 = $(that.endDom).offset().left + 8;// 判断曲线第二个点的定位that.top2 = that.top1 - that.top1/3*0.6;that.left2 = (that.left1 - that.left)/3*1.2 + that.left;// 创建动画元素,并将动画添加到body中var name = new Date().getTime();that.name = name;$("body").append('<div class="' + that.opts.prefix + " " + name + '" style="top:' + that.top + 'px;left:' + that.left + 'px;"></div>');// 将name添加到数组中,方便动画完毕删除that.tweenArr.push(name);that.animate(callback); // 动画}// 动画Tween.prototype.animate = function (callback){TweenMax.to($("." + that.opts.prefix + "." + that.name)[0], that.opts.speed, {bezier: [ // 起点坐标不用放到代码中,以下三个点分别是第2点,第3点及终点{left: that.left2, top: that.top2},{left: that.left1, top: that.top1}],width: that.opts.width,height: that.opts.height,ease: that.opts.ease,onComplete: function (){ // 动画完成回调$("." + that.opts.prefix + "." + that.tweenArr[0]).stop().fadeOut(200).remove(); // 动画消失、移除that.tweenArr.shift(); // 删除第一个元素callback ? callback() : "";}});}win.Tween = Tween})(window)var timer = ""; // 定时器$(".btn").unbind();$(".btn").click(function (){var tween = new Tween('.btn', '.box');tween.init(function (){$(".cart").show(); // 显示加入购票车样式clearTimeout(timer); // 清除定时timer = setTimeout(function (){$(".cart").hide();}, 10000);})})
具体动画效果可下载demo查看
未点击之前

点击

动画消失 提示加入购物车成功

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