news 2025/12/19 19:48:44

购物车小球动画:点击商品生成飞向购物车的小球动画

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
购物车小球动画:点击商品生成飞向购物车的小球动画

最近做了一个小需求,写购物车小球动画效果,给大家分享一下这个功能的源码,以便以后的使用。

实现逻辑

每次点击时,拿到点击的位置作为小球的开始位置,再获取到购物车的结束位置。确定了两端位置之后,给小球设置css的path路径(使用贝塞尔曲线),最后通过animate方法执行动画效果,即可实现。

<!DOCTYPEhtml><html lang="zh-CN"><head><meta charset="UTF-8"/><meta name="viewport"content="width=device-width, initial-scale=1.0"/><title>购物车小球动画</title><script src="https://unpkg.com/vue@3/dist/vue.global.js"></script><style>*{margin:0;padding:0;box-sizing:border-box;}body{font-family:"PingFang SC","Helvetica Neue",Arial,sans-serif;background-color:#f5f5f5;padding:20px;color:#333;}.container{max-width:800px;margin:0auto;background:white;border-radius:12px;box-shadow:04px12pxrgba(0,0,0,0.1);padding:20px;}h1{text-align:center;color:#cf7e27;margin-bottom:20px;}.description{text-align:center;margin-bottom:30px;color:#666;}.items-container{display:flex;flex-wrap:wrap;gap:15px;justify-content:center;margin-bottom:30px;}.item{width:100px;padding:10px;background:#f8f8f8;border-radius:8px;text-align:center;cursor:pointer;transition:transform0.2s;}.item:hover{transform:translateY(-3px);box-shadow:04px8pxrgba(0,0,0,0.1);}.item-img{width:60px;height:60px;background:linear-gradient(135deg,#ffb800,#cf7e27);border-radius:50%;margin:0auto8px;}.cart-container{position:fixed;bottom:20px;right:20px;}.cart{width:60px;height:60px;background:#cf7e27;border-radius:50%;display:flex;align-items:center;justify-content:center;position:relative;box-shadow:04px12pxrgba(0,0,0,0.15);}.cart-count{position:absolute;top:-5px;right:-5px;background:#ff3000;color:white;font-size:12px;width:20px;height:20px;border-radius:50%;display:flex;align-items:center;justify-content:center;}.ball-container{position:absolute;pointer-events:none;}.ball{width:20px;height:20px;background:linear-gradient(135deg,#ff3000,#cf7e27);border-radius:50%;box-shadow:02px4pxrgba(0,0,0,0.2);position:absolute;top:0;right:0;}.control-panel{background:#f9f9f9;padding:15px;border-radius:8px;margin-top:20px;}.slider-container{margin:10px0;}label{display:block;margin-bottom:5px;font-weight:bold;color:#555;}input[type="range"]{width:100%;}.value-display{text-align:center;font-size:14px;color:#777;}.code-example{background:#2d2d2d;color:#f8f8f2;padding:15px;border-radius:8px;margin-top:20px;overflow-x:auto;font-family:"Fira Code",monospace;}</style></head><body><div id="app"></div><script type="module">const{createApp,ref,reactive}=Vue;constApp={setup(){constitems=ref([{id:1,name:"美食",price:25},{id:2,name:"饮料",price:15},{id:3,name:"水果",price:20},{id:4,name:"甜品",price:18},{id:5,name:"快餐",price:22},{id:6,name:"小吃",price:12},]);constcart=reactive({count:0,total:0,});constballs=ref([]);constballIndex=ref(0);constanimationSpeed=ref(600);constaddToCart=(item,event)=>{cart.count++;cart.total+=item.price;// 获取点击位置conststartX=event.clientX;conststartY=event.clientY;createBall(startX,startY);};// 创建小球constcreateBall=(startX,startY)=>{letendEle=document.querySelector(".cart").getBoundingClientRect();letendX=Math.floor(endEle.left+endEle.width/2);letendY=Math.floor(endEle.top+endEle.height/2);letfatherEle=document.querySelector(".container");letball=document.createElement("div");ball.classList.add("ball");ball.style.left=startX+"px";ball.style.top=startY+"px";// 贝塞尔曲线路径ball.style.offsetPath=`path('M${0}${0}C${100}${-100},${endX-startX}${endY-startY},${endX-startX}${endY-startY}')`;fatherEle.appendChild(ball);setTimeout(()=>{fatherEle.removeChild(ball);},Number(animationSpeed.value)-100);ball.animate(// 将偏移路径动画化{offsetDistance:[0,"100%"]},{duration:Number(animationSpeed.value),iterations:1,easing:"cubic-bezier(.667,0.01,.333,.99)",direction:"alternate",});};return{items,cart,balls,ballIndex,animationSpeed,addToCart,createBall,};},template:`<div class="container"> <h1>购物车小球动画</h1> <p class="description">点击商品将生成飞向购物车的小球动画,多个小球实例互不干扰</p> <div class="items-container"> <div v-for="item in items" :key="item.id" class="item" @click="addToCart(item, $event)" > <div class="item-img"></div> <div>{{ item.name }}</div> <div>¥{{ item.price }}</div> </div> </div> <div class="control-panel"> <h3>动画控制</h3> <div class="slider-container"> <label>动画速度: {{ animationSpeed }}ms</label> <input type="range" min="500" max="1000" v-model="animationSpeed" > <div class="value-display">调整小球飞行的速度</div> </div> </div> <div class="cart-container"> <div class="cart"> <span>购</span> <div class="cart-count">{{ cart.count }}</div> </div> </div>`,};constapp=createApp(App);app.mount("#app");</script></body></html>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/17 11:01:00

智慧文旅信创落地新标杆:四川省文旅厅完成MySQL 5.7平滑替换,筑牢省级管理平台自主可控底座

在国家全面推进数字政府与信息技术应用创新深度融合的背景下&#xff0c;对开源技术依赖的风险治理正成为关键领域安全体系建设的重要一环。2025年3月&#xff0c;四川省文化和旅游厅成功将其核心业务系统——“智游天府综合管理平台”的底层数据库&#xff0c;由原MySQL 5.7单…

作者头像 李华
网站建设 2025/12/17 14:53:54

7、Unix/Linux 网络监控与日志管理全解析

Unix/Linux 网络监控与日志管理全解析 1. 计算重传率 在网络监控中,计算 TCP 重传率是一项重要任务。可以通过解析 netstat 输出获取所需数据。以下是示例代码: @myrec = split(" ", $_); $tcpoutsegs = $myrec[0]; } if(/segments retransmited/) { @myrec …

作者头像 李华
网站建设 2025/12/17 14:53:53

11、数据备份与系统安装全攻略

数据备份与系统安装全攻略 在数据管理和系统运维的领域中,数据备份和系统安装是至关重要的环节。合理的数据备份策略能够确保数据的安全性和可用性,而正确的系统安装则为后续的系统运行和服务提供了坚实的基础。下面将详细介绍数据备份的多种方式以及系统安装的规划要点。 …

作者头像 李华
网站建设 2025/12/17 14:53:51

12、Unix/Linux 系统设置与生产准备全攻略

Unix/Linux 系统设置与生产准备全攻略 系统安装与优化 在进行系统安装时,若有必要,可多次重新运行安装程序,逐步达成适合预期用途的系统状态。较为简便的做法是先安装精简系统,仅包含基本组件,随后检查缺失的功能,持续安装直至满足需求。此方法能让你了解系统在修改时的…

作者头像 李华
网站建设 2025/12/17 14:53:49

5步掌握网页数据采集:零代码工具完全操作手册

5步掌握网页数据采集&#xff1a;零代码工具完全操作手册 【免费下载链接】web-scraper-chrome-extension Web data extraction tool implemented as chrome extension 项目地址: https://gitcode.com/gh_mirrors/we/web-scraper-chrome-extension 在数字化时代&#xf…

作者头像 李华
网站建设 2025/12/17 14:53:48

15、测试系统与“安全”系统

测试系统与“安全”系统 在生产系统上应用补丁、进行升级或部署新软件之前,测试是必不可少的环节。同时,系统管理员还应拥有所谓的“安全”系统,并且良好的文档记录也至关重要。下面将详细介绍测试系统、“安全”系统以及文档记录的相关内容。 测试系统 在向非系统管理专业…

作者头像 李华