js添加到购物车动画效果实现

方法一: 用js控制购车的的位置实现

原理解析: 通过js动态生成一个过度元素(就是一个可以移动的小球),并控制该元素的位置移动,从点击按钮dom元素移动到购物车的dom元素,实现添加到购物车的动态效果。

  • 效果展示

绿色小球会按照抛物线的轨迹移动到购物车里
在这里插入图片描述

/*
* 添加到购物车动画 脚本
* */

/***
 * 动画轨迹控制
 * @param addBtnDom 增加按钮的dom元素或者选择器
 * @param shopCarDom 购物车的dom元素或选择器
 */
function controllPath(addBtn, shopCar) {
    var addBtnDom = null,
        shopCarDom = null;
    if(typeof addBtn === "string"){
       addBtnDom = document.querySelector(addBtn);
    } else if(addBtn instanceof HTMLElement){
        addBtnDom = addBtn
    } else {
        alert("传入的参数错误: 第一个参数应为增加按钮的dom元素或该元素的选择器。");
        return;
    }

    if(typeof shopCar === "string"){
        shopCarDom = document.querySelector(shopCar);
    } else if(shopCar instanceof HTMLElement){
        shopCarDom = shopCar;
    }else {
        alert("传入的参数错误: 第二个参数应为购物车的dom元素或该元素的选择器。");
        return;
    }

    // 获取两个dom的位置
    var addBtnposition = addBtnDom.getBoundingClientRect();
    var shopCarPosition = shopCarDom.getBoundingClientRect();
    var addBtnCenterX = (addBtnposition.left + addBtnposition.right) / 2;
    var addBtnCenterY = (addBtnposition.top + addBtnposition.bottom) / 2;
    var shopCarCenterX = (shopCarPosition.left + shopCarPosition.right) / 2;
    var shopCarCenterY = (shopCarPosition.top + shopCarPosition.bottom) / 2;
    //计算增加按钮 是在 相对于购物车的 左边还是右边(用于控制后面的移动方向)
    var relativePosition = addBtnCenterX > shopCarCenterX ? -1 : 1;

    // 获取连个dom之间的距离
    var xDistance = Math.abs(addBtnCenterX - shopCarCenterX);
    var yDistance = Math.abs(addBtnCenterY - shopCarCenterY);

    // 绘制小球并设置其位置
    var ballDom = drawBall();
    ballDom.style.top = addBtnCenterY + "px";
    ballDom.style.left = addBtnCenterX + "px";
    document.body.appendChild(ballDom);
    /*
     * 根据一元二次方程的轨迹求出对象的系数 y = ax^2 + bx + c
     *  var coefficientC = 0;
     *  var coefficientB = 0;
     *  var coefficientA = yDistance / Math.pow(xDistance, 2);
     */
    //小球的横竖坐标
    var xAbscissa = 0, yAbscissa = 0;

    //设置移动路径
    var ballTimer = setInterval(function () {
        //每次重新坐标
        xAbscissa += 5 * relativePosition;
        yAbscissa = (yDistance / Math.pow(xDistance, 2)) * Math.pow(xAbscissa, 2);
        ballDom.style.top = addBtnCenterY + yAbscissa + "px";
        ballDom.style.left = addBtnCenterX + xAbscissa + "px";
        //检查是否到达终点
        var surplusDistance = parseInt(ballDom.style.left) - shopCarCenterX;
        if (Math.abs(surplusDistance) <= 10) {
            clearInterval(ballTimer);
        }
    }, 25);
}

/*
* 绘制小球
* */
function drawBall() {
    var ballDom = document.createElement("div");
    ballDom.style.width = "20px";
    ballDom.style.height = "20px";
    ballDom.style.border = "1px solid #ccc";
    ballDom.style.background = "#73dd51";
    ballDom.style.borderRadius = "50%";
    ballDom.style.position = "absolute";
    return ballDom;
}

  • 使用说明
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>添加到购物车</title>
    <style>
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
        }

        .box {
            position: relative;
            width: 100%;
            height: 100%;
            border: 2px solid #cc726f;
            box-sizing: border-box;
        }

        .add-btn {
            width: 40px;
            height: 40px;
            border: 2px solid orange;
            border-radius: 50%;
            position: absolute;
            top: 10px;
            left: 10px;
            background: antiquewhite;
            box-sizing: border-box;
        }

        .add-btn:before, .add-btn:after {
            content: "";
            position: absolute;
            top: 50%;
            left: 10%;
            margin-top: -2px;
            width: 80%;
            height: 4px;
            background: white;
        }

        .add-btn:after {
            transform: rotate(90deg);
        }

        .shop-car {
            width: 100px;
            height: 40px;
            border: 1px solid #ff574d;
            position: absolute;
            top: 300px;
            left: 100px;
            text-align: center;
            line-height: 40px;
        }

    </style>
</head>
<body>
<div class="box">
    <div class="add-btn"></div>
    <div class="shop-car">购物车</div>
</div>
<script src="./addToCar.js"></script>
<script>
    var addBtnDom = document.querySelector(".add-btn");
    var shopCarDom = document.querySelector(".shop-car");
    addBtnDom.addEventListener("click",function () {
        //第一种方式
        controllPath(addBtnDom, shopCarDom);
        //第二种方式
        //controllPath(".add-btn", ".shop-car");
    })
</script>
</body>
</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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITzhongzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值