购物车抛物线原理

本文介绍了两种购物车飞入特效的实现方法:一种使用二次函数进行路径计算,通过JavaScript控制元素沿抛物线轨迹移动;另一种利用CSS动画实现商品图标从商品详情页面飞入购物车的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天看了 一些 购物车的飞入特效,和 参考了网上的一些前端写的,自己概括了2种。

第一种是利用2次函数 y=ax2+bx+c

<script type="text/javascript">
var parabola = function(config){
var b = 0,
INTERVAL = 15,
timer = null,
x1,y1,x2,y2,originx,originy,diffx,diffy;

this.config = config || {};
// 起点
this.origin = $(this.config.origin)||null;
// 终点
this.target = $(this.config.target)||null;
// 运动的元素
this.element = $(this.config.element)||null;
// 曲线弧度
this.a = this.config.a || 0.002;
// 运动时间(ms)
this.time = this.config.time || 1000;

this.init = function(){
x1 = this.origin.offset().left;
y1 = this.origin.offset().top;
x2 = this.target.offset().left;
y2 = this.target.offset().top;
originx = x1;
originy = y1;
diffx = x2-x1;
diffy = y2-y1,
speedx = diffx/this.time;

// 已知a = 3, 根据抛物线函数 y = a*x*x + b*x + c 将抛物线起点平移到坐标原点[0, 0],终点随之平移,那么抛物线经过原点[0, 0] 得出c = 0;
// 终点平移后得出:y2-y1 = a*(x2 - x1)*(x2 - x1) + b*(x2 - x1)
// 即 diffy = a*diffx*diffx + b*diffx;
// 可求出常数b的值
b = (diffy - this.a*diffx*diffx)/diffx;
this.element.css({
left: x1,
top: y1
})
return this;
};

// 确定动画方式
this.moveStyle = function(){
var moveStyle = 'position',
testDiv = document.createElement('div');
if('placeholder' in testDiv){
['','ms','moz','webkit'].forEach(function(pre){
var transform = pre + (pre ? 'T' : 't') + 'ransform';
if(transform in testDiv.style){
moveStyle = transform;
}
})
}
return moveStyle;
}

this.move = function(){
var start = new Date().getTime(),
moveStyle = this.moveStyle(),
_this = this;

timer = setInterval(function(){
if(new Date().getTime() - start > _this.time){
clearInterval(timer);
_this.element.css({
left: x2,
top: y2
})
typeof _this.config.callback === 'function' && _this.config.callback(_this.element);
return;
}
x = speedx * (new Date().getTime() - start);
y = _this.a*x*x + b*x;
if(moveStyle === 'position'){
_this.element.css({
left: x+originx,
top: y+originy
})
}else{
if(window.requestAnimationFrame){
window.requestAnimationFrame(_this.element[0].style[moveStyle] = 'translate('+x+'px,'+y+'px)');
}else{
_this.element[0].style[moveStyle] = 'translate('+x+'px,'+y+'px)';
}
}
},INTERVAL)
return this;
}

this.init();
};


</script>

第二种是利用css

$(function () {
var $orgImg = $('.origin', this);
var $targetDiv = $('.target');
var $fxImg = $orgImg.clone().css({
'position': 'absolute',
'z-index': 10000,
'width': $orgImg.width(),
'height': $orgImg.height(),
'border-radius': '50%'
}).css($orgImg.offset()).appendTo('body');


$fxImg
.animate({
left: [$targetDiv.offset().left + $targetDiv.width() - $fxImg.width(), 'linear'],
top: [$targetDiv.offset().top + $targetDiv.height() - $fxImg.height(), 'easeOutQuad']
}, 600)
.fadeOut(200, function () {
$fxImg.detach();
});

})
$('.parabola-el').on('click', function(){


});
只需要引入
<script src="./js/jquery-ui.min.js"></script>
及 jquery.js 即可

转载于:https://www.cnblogs.com/xiaominsweet/p/6483905.html

js加入购物车抛物线画与购物车果特,亲测可用, 当您在电商购物网站浏览中意的商品时,您可以点击页面中的“加入购物车”按钮即可将商品加入购物车中。本文介绍借助一款基于jQuery的画插件,点击加入购物车按钮时,实现商品将飞入到右侧的购物车中的果。 HTML 首先载入jQuery库文件和jquery.fly.min.js插件。 复制代码 代码如下: 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片、价格、名称以及加入购物车按钮等信息。 复制代码 代码如下: ¥3499.00 LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计 加入购物车 ¥3799.00 Hisense/海信 LED50T1A 海信电视官方旗舰店 加入购物车 ¥¥3999.00 Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视 加入购物车 ¥6969.00 乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视 加入购物车 然后,我们还需要在页面的右侧加上购物车以及提示信息。 复制代码 代码如下: 购物车 已成功加入购物车CSS 我们使用CSS先将商品排列美化,然后设置右侧购物车样式,具体请看代码: 复制代码 代码如下: .box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center} .box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left} .box:hover{border:1px solid #f90} .box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500} .box h4 span{font-size:20px} .u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;} .m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;height: 100%;font-size: 12px;color: #fff;} .cart{color: #fff;t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值