JavaScript学习——模拟购物车抛物线

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>模拟购物车抛物线</title>
  <style>
    *{
      box-sizing: border-box;
    }
    .shopping-cart{
      border: 1px solid #000;
      width: 300px;
      height: 500px;
      margin: 50px auto;
    }
    img{
      width: 30px;
      height: 30px;
    }
    .list ul li{
      list-style: none;
      /* background-color: #ddd; */
      margin-top: 10px;
      width: 200px;
      display: flex;
      justify-content: space-between;
    }
    .bottom{
      background-color: pink;
      height: 50px;
      vertical-align: middle;
      padding: 10px 40px;
    }
  </style>
</head>
<body>
  <div class="shopping-cart">
    <div class="list" id="list">
      <ul>
        <li class="item">
          <span>披萨</span>
          <img class="image" src="plus.png" alt="">
        </li>
        <li class="item">
          <span>汉堡</span>
          <img class="image" src="plus.png" alt="">
        </li>
        <li class="item">
          <span>奶茶</span>
          <img class="image" src="plus.png" alt="">
        </li>
        <li class="item">
          <span>炸鸡</span>
          <img class="image" src="plus.png" alt="">
        </li>
        <li class="item">
          <span>火锅</span>
          <img class="image" src="plus.png" alt="">
        </li>
      </ul>
    </div>
    <div class="bottom">
      <img id="shopping" src="shopping.png" alt="">
      <span id="content"></span>
    </div>
  </div>
  <script>
    let list = document.getElementById("list");
    let liItem = list.getElementsByClassName("item");
    let ball = document.getElementsByClassName("ball")[0];
    let content = document.getElementById("content");
    let arr = new Array();
    for(let i = 0; i< liItem.length; i++){
      let liImage = liItem[i].getElementsByClassName("image")[0];
      let liText = liItem[i].getElementsByTagName("span")[0];
      liImage.addEventListener('click',function(event){
        let ball = document.createElement('div');
        let x = event.pageX - this.offsetWidth / 2;
        let y = event.pageY - this.offsetHeight / 2;
        ball.style.position = 'absolute';
        ball.style.width = '20px';
        ball.style.height = '20px';
        ball.style.borderRadius = '50%';
        ball.style.backgroundColor = '#2fb7f8';
        ball.style.top = y + "px";
        ball.style.left = x + "px";
        ball.style.transition = 'left .6s linear, top .6s cubic-bezier(0.5, -0.5, 1, 1)';
        document.body.appendChild(ball);
        setTimeout(() => {
            let target = document.getElementById("shopping");
            ball.style.left = (target.offsetLeft + target.offsetWidth / 2) + 'px';
            ball.style.top = (target.offsetTop) + 'px';
            arr.push(liText.innerHTML);
            content.innerHTML = arr;
        }, 0);
        ball.ontransitionend = function(){
            this.remove()
        }
      })
    }
    
  </script>
  
</body>
</html>

图片
plus.png
在这里插入图片描述
shopping.png
在这里插入图片描述

运行结果
在这里插入图片描述
参考文章
https://www.jianshu.com/p/5869e1c2ddf7
https://mickey0524.github.io/2017/08/06/cubic-bezier/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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
要实现购物抛物线效果,可以使用 react-motion 这个动画库。 首先,需要引入 react-motion 库。 ```JavaScript import { Motion, spring } from 'react-motion'; ``` 然后,可以在购物组件中添加一个“添加到购物”按钮。当用户点击该按钮时,可以触发一个动画,将商品从添加按钮处抛向购物。 ```JavaScript class ShoppingCart extends React.Component { state = { items: [], // 购物列表 showItem: false, // 是否显示添加的商品 itemX: 0, // 商品的 X 坐标 itemY: 0, // 商品的 Y 坐标 cartX: 0, // 购物的 X 坐标 cartY: 0, // 购物的 Y 坐标 }; // 添加商品到购物 handleAddItem = (item) => { const { clientWidth, clientHeight } = document.documentElement; // 获取添加按钮的位置 const { x: itemX, y: itemY } = this.btnAdd.getBoundingClientRect(); // 获取购物的位置 const { x: cartX, y: cartY } = this.cart.getBoundingClientRect(); // 更新状态,显示添加的商品,同时保存商品和购物的位置 this.setState({ showItem: true, items: [...this.state.items, item], itemX, itemY, cartX, cartY, }); // 2 秒后隐藏添加的商品 setTimeout(() => { this.setState({ showItem: false }); }, 2000); }; render() { const { items, showItem, itemX, itemY, cartX, cartY } = this.state; return ( <div> <ul> {/* 渲染购物列表 */} {items.map((item) => ( <li key={item.id}>{item.name}</li> ))} </ul> {/* 添加按钮 */} <button ref={(btn) => (this.btnAdd = btn)} onClick={() => this.handleAddItem({ id: 1, name: '商品1' })}> 添加到购物 </button> {/* 购物 */} <div ref={(cart) => (this.cart = cart)} style={{ width: '50px', height: '50px', border: '1px solid #ccc', position: 'fixed', right: '50px', bottom: '50px', }} > {/* 购物图标 */} <i className="fa fa-shopping-cart fa-2x"></i> {/* 商品动画 */} <Motion defaultStyle={{ x: itemX, y: itemY, }} style={{ x: showItem ? spring(cartX - itemX, { stiffness: 100, damping: 10 }) : itemX, y: showItem ? spring(cartY - itemY, { stiffness: 100, damping: 10 }) : itemY, }} > {(style) => showItem ? ( <div style={{ position: 'absolute', left: style.x, top: style.y, width: '20px', height: '20px', background: 'red', borderRadius: '50%', }} ></div> ) : null } </Motion> </div> </div> ); } } ``` 在上面的代码中,我们首先定义了一些状态,包括购物列表、是否显示添加的商品、商品和购物的坐标等。在 `handleAddItem` 方法中,我们获取了添加按钮和购物的位置,并保存到状态中。然后,我们使用 `react-motion` 的 `Motion` 组件来实现动画。`Motion` 组件的 `defaultStyle` 属性定义了动画的起始状态,即商品的位置。`style` 属性定义了动画的结束状态,即商品飞到购物的位置。在这里,我们使用了 `react-motion` 的 `spring` 函数来实现动画效果。`spring` 函数接受两个参数,第一个参数是动画的结束位置,第二个参数是动画的配置项,包括 `stiffness` 和 `damping` 两个属性,可以用来调整动画的弹性和阻力。最后,我们在 `Motion` 组件中渲染了一个红色的圆形,表示商品。 这样,就实现了购物抛物线效果。当用户点击添加按钮时,商品会从该位置飞向购物,形成一个抛物线的动画效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值