点击添加到购物车按钮 实现小球飞到购物车的效果

文章介绍了如何在React应用中利用useRef创建动态小球,并在用户点击加入购物车按钮时,在点击位置实现动画效果。通过`requestAnimationFrame`优化动画性能。
摘要由CSDN通过智能技术生成

import React, { useRef } from 'react'

export default function App() {

  const box = useRef(null); //  创建一个引用,用于获取购物车位置的元素

  const createBall = (left, top) => {    //  创建小球并实现动画效果的函数

    const ball = document.createElement('div');   //  创建一个div作为小球元素

    //  设置小球的位置

    ball.style.position = 'absolute';

    ball.style.left = left - 10 + 'px';

    ball.style.top = top - 10 + 'px';

    //  设置小球的尺寸、形状和颜色等样式

    ball.style.width = '20px';

    ball.style.height = '20px';

    ball.style.borderRadius = '50%';

    ball.style.backgroundColor = 'red';

    ball.style.transition = 'left .6s linear, top .6s cubic-bezier(0.5,-0.5,1,1)';

    //  将小球元素添加到body中

    document.body.appendChild(ball);

    // 使用 requestAnimationFrame 替代 setTimeout

    requestAnimationFrame(() => {

      ball.style.left = box.current.offsetLeft + box.current.offsetWidth / 2 + 'px';

      ball.style.top = box.current.offsetTop + 'px';

    });

    //  当小球的过渡动画结束后,移除小球元素

    ball.ontransitionend = function () {  

      ball.remove();

    };

  };

  //  处理'加入购物车'按钮的点击事件

  const handleAddToCart = (e) => {

    //  获取鼠标点击的坐标信息

    const { clientX, clientY } = e;

    //  在按钮位置创建小球并实现动画效果

    createBall(clientX, clientY);

  };

  return (

    <div>

      <div>

        <button onClick={(e) => handleAddToCart(e)} style={{ borderRadius: '5px', backgroundColor: '#ff5000', color: 'white' }}>加入购物车</button>

      </div>

      <div ref={box} style={{ position: 'fixed', bottom: '2px', right: '0' }}>

        购物车

      </div>

    </div>

  )

}

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现将数据添加购物车效果,需要前端和后端的配合。 前端部分: 1. 在 HTML 页面中添加购物车按钮,并为其绑定一个事件监听器; 2. 在事件监听器中使用 AJAX 技术向后端发送请求,请求添加商品到购物车中; 3. 显示添加成功或失败的信息。 一个简单的HTML页面代码示例: ```html <!DOCTYPE html> <html> <head> <title>商品列表</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function() { $(".add-to-cart").click(function() { var id = $(this).data("id"); $.ajax({ url: "addToCart.php", method: "POST", data: {id: id}, success: function(response) { $(".message").html(response); } }); }); }); </script> </head> <body> <h1>商品列表</h1> <ul> <li>商品1 <button class="add-to-cart" data-id="1">加入购物车</button></li> <li>商品2 <button class="add-to-cart" data-id="2">加入购物车</button></li> <li>商品3 <button class="add-to-cart" data-id="3">加入购物车</button></li> </ul> <div class="message"></div> </body> </html> ``` 在这个示例中,我们为每个商品添加了一个加入购物车按钮,并为每个按钮设置了一个 `data-id` 属性,用来表示该商品的唯一标识符。当用户点击加入购物车按钮时,我们使用 AJAX 技术向后端发送请求,并将商品的唯一标识符作为参数传递给后端。 后端部分: 1. 接收前端发送的请求,获取商品的唯一标识符; 2. 根据商品的唯一标识符从数据库中获取商品的信息; 3. 将商品的信息添加购物车中,并将购物车保存到 session 中; 4. 返回添加成功或失败的信息给前端。 一个简单的PHP代码示例: ```php <?php session_start(); if ($_SERVER["REQUEST_METHOD"] == "POST") { $id = $_POST["id"]; // 根据商品的唯一标识符从数据库中获取商品信息 $item = getItemById($id); if ($item) { // 将商品信息添加购物车中 if (!isset($_SESSION["cart"])) { $_SESSION["cart"] = array(); } array_push($_SESSION["cart"], $item); echo "商品已添加购物车。"; } else { echo "商品不存在。"; } } ?> ``` 在这个示例中,我们首先获取前端发送的请求中的商品唯一标识符,并根据该标识符从数据库中获取商品信息。如果商品存在,我们将其添加购物车中,并将购物车保存到 session 中。最后,我们返回添加成功或失败的信息给前端

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值