CSS3 抛物线 加入购物车

 

CSS3 抛物线 加入购物车

分类: 前端   151人阅读  评论(0)  收藏  举报

目录(?)[+]

前言

项目中用到购物车抛物线动画 一开始使用的是Jquery插件 jquery.fly.js;在手机上使用时一些较手机效果不是很好,因为这个插件会根据抛物线动画时间和距离计算抛物线点轨迹(FPS,说是一秒要有60帧的样子)会有卡顿现象,而且还有window.requestAnimationFrame的兼容性问题 ,如果在只是PC段使用还是挺好的。下面介绍我们使用CSS3的方式。

开始介绍之前,建议看下大神的文章 贝塞尔曲线与CSS3动画、SVG和canvas的基情

先贴代码 
//使用的是Css3 两个元素,外面的元素纵坐标移动,里面的元素横坐标移动 不使用keyframe

JavaScript

预设5个抛物点,这部分视情况是否要这样做

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">var $pointDiv = $('<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-property" style="box-sizing: border-box;">id</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pointDivs"</span>>').appendTo('body');
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(var i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;i<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>;i++){
    $('<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="box-sizing: border-box;">class</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"point-outer point-pre"</span>><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="box-sizing: border-box;">class</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"point-inner"</span>/》</<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>').appendTo($pointDiv);
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

点击事件代码

<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取开始点坐标</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> startOffset = $(ev.target).offset();
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取结束点坐标</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> endTop = window.innerHeight - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, endLeft = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>,left = startOffset.left+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>,top = startOffset.top+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> outer = $(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'#pointDivs .point-pre'</span>).first().removeClass(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"point-pre"</span>).css({left:left+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'px'</span>,top:top+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'px'</span>});
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> inner = outer.find(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">".point-inner"</span>);
setTimeout(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
    outer[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>].style.webkitTransform = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'translate3d(0,'</span>+(endTop - top)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'px,0)'</span>;
    inner[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>].style.webkitTransform = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'translate3d('</span>+(endLeft - left)+<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'px,0,0)'</span>;
    setTimeout(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
        outer.removeAttr(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"style"</span>).addClass(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"point-pre"</span>);
        inner.removeAttr(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"style"</span>);
    },<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">800</span>);
},<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li></ul>

CSS

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.point-outer</span><span class="hljs-rules" style="box-sizing: border-box;">{
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">position</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);">absolute</span></span>; 
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">z-index</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"><span class="hljs-number" style="box-sizing: border-box;">20</span></span></span>;
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">-webkit-transition</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);">all <span class="hljs-number" style="box-sizing: border-box;">0.8</span>s <span class="hljs-function" style="box-sizing: border-box;">cubic-bezier(<span class="hljs-number" style="box-sizing: border-box;">0.39</span>,-<span class="hljs-number" style="box-sizing: border-box;">0.4</span>,<span class="hljs-number" style="box-sizing: border-box;">0.83</span>,<span class="hljs-number" style="box-sizing: border-box;">0.23</span>)</span> <span class="hljs-number" style="box-sizing: border-box;">0</span>s</span></span>;
<span class="hljs-rule" style="box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.point-inner</span><span class="hljs-rules" style="box-sizing: border-box;">{
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">width</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"><span class="hljs-number" style="box-sizing: border-box;">10</span>px</span></span>;
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">height</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"><span class="hljs-number" style="box-sizing: border-box;">10</span>px</span></span>; 
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">border-radius</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"><span class="hljs-number" style="box-sizing: border-box;">50</span>%</span></span>;
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">background-color</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"><span class="hljs-hexcolor" style="box-sizing: border-box;">#ff6326</span></span></span>; 
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">-webkit-transition</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);">all <span class="hljs-number" style="box-sizing: border-box;">0.8</span>s linear <span class="hljs-number" style="box-sizing: border-box;">0</span>s</span></span>;
<span class="hljs-rule" style="box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.point-outer</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.point-pre</span><span class="hljs-rules" style="box-sizing: border-box;">{
    <span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">display</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);">none</span></span>;
<span class="hljs-rule" style="box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

说明

使用div中嵌入div:外面的div是outer,里面的是inner 。outer 的css设置其垂直方向运动,inner的css设置水平方式运动。为达到抛物线效果,inner的运动速度设置的匀速(linear),而outer运动速度由贝塞尔曲线(cubic-bezier(n,n,n,n))设定,贝塞尔曲线值可以参考示例中的值。

这里在说明下JS中的两个setTimeout。第一个setTimeout是等元素定位到起始点,第二个是抛物线动画自行完成后隐藏抛物点。

版权声明:本文为博主原创文章,未经博主允许不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值