uniapp vue3 小程序中 手写模仿京东淘宝加入购物车红点曲线飞入样式效果 简化版代码

uniapp vue3 小程序中 手写模仿京东淘宝加入购物车红点曲线飞入样式效果 简化版代码

图片实例点击列表中的购物车按钮 红点飞到下方的购物车组件中

样式布局如下

javascript 复制代码
//列表布局如下  :class="`cart-btn-${item.id}`"  这个是重点设置
<view class="goods-cell" v-for="(item,index) in goodsList" :key="index">
	<view class="img-box">
		<up-image mode="scaleToFill" width="100%" height="100%" :src="item.url" ></up-image>
	</view>
	<view class="cart-btn" :class="`cart-btn-${item.id}`" @click.stop="addCartFun(item)">
		<up-icon name="shopping-cart" color="#fff" size="18"></up-icon>
	</view>
</view>
//红点样式  我只做了红点 如果想加入图片 自己在view中添加
<view class="dot-box" :style="dotStyle"></view>

//css样式如下:
<style lang="scss" scoped>
.dot-box{
	position: absolute;
	top: 0;
	left: 0;
	z-index: 10;
	width: 25rpx;
	height: 25rpx;
	background-color: #f00;
	border-radius: 50%;
	//transition: transform 0.9s ease;//transition: transform 0.9s ease 0.1s; 是否等待0.1s执行
	transition: transform 0.9s cubic-bezier(0.35, -0.45, 0.58, 1);//0.1s  这个贝塞尔曲线 有个抛物的效果
}
</style>
//js如下
<script setup>
import { reactive, ref,watch,onMounted, onUnmounted } from 'vue';
// 商品列表信息
let goodsList = ref([
{id:12,ys:423,quantity:1,isCart:false,url:'http://img.alicdn.com/img/i4/5230503464/O1CN01W2h9No1bSZ1trp6po_!!4611686018427381288-0-saturn_solar.jpg',name:'安石坊沙藏酒老酒水家宴酒老百姓口粮酒原浆糯谷醇香纯粮酿造白酒',jg:90,xl:480},
{id:22,ys:303,quantity:1,isCart:false,url:'http://img.alicdn.com/img/i4/7962644201/O1CN01e1Y1PO1gu6yMIr7WZ_!!4611686018427383529-0-saturn_solar.jpg',name:'孔子白酒礼仪人家信整箱纯粮食浓香型高度6瓶礼盒装酒水送礼长辈',jg:58,xl:25480},
{id:32,ys:4223,quantity:1,isCart:false,url:'https://g-search2.alicdn.com/img/bao/uploaded/i4/i4/2216802875497/O1CN01SPyOZK1qTgEFjL6zY_!!2216802875497.jpg',name:'【整箱】12度中式精酿原浆茶啤酒1.5L/6桶装毛尖茉莉花茶西湖龙井',jg:39.1,xl:42480},
])
//初始化红点的位置信息
let dotStyle = ref({
	top:'-100rpx',//这是为了隐藏红点的位置 不显示在页面中
	left:'-100rpx',
	transform: `translate(${0}rpx, ${0}rpx)`
})

// 加入购物车
const sys = uni.getSystemInfoSync();//获取系统信息 屏幕高度和宽度
function addCartFun(e){
//根据点击的按钮判断是那个位置然后获取该位置的高度、偏移量等信息
uni.createSelectorQuery().select(`.cart-btn-${e.id}`).boundingClientRect(rect=>{
	//rect.left 和 rect.top 是相对于屏幕左上角的坐标
	//console.log(rect,sys)
	dotStyle.value.top = rect.top*2 +'rpx'//将红点的坐标位置移动到点击的按钮上
	dotStyle.value.left = rect.left*2 +'rpx'
	//设置红点曲线飞到的最终位置坐标信息  
	//其中偏移宽度为 - sys.windowWidth/2 - 40(40 这个数值根据购物车按钮大小和具体位置调整)  
	//偏移高度为(sys.windowHeight - rect.bottom)*2 + 50(50 是我页面设置的距离底部高度 可根据自己页面样式调整)
	//scale(0.5)  控制缩放样式
	dotStyle.value.transform = `translate(${- sys.windowWidth/2 - 40}rpx, ${(sys.windowHeight - rect.bottom)*2 + 50}rpx) scale(0.5) `
	//最后等待曲线执行完成 还原红点的位置信息
	setTimeout(()=>{
		dotStyle.value.top = '-100rpx'
		dotStyle.value.left = '-100rpx'
		dotStyle.value.transform = `translate(${0}rpx, ${0}rpx)`
	},900)//900 为上面红点过渡的时间 两方要同步设置 transition: transform 0.9s ease;
}).exec()

//下面是加入购物车逻辑  
//uni.showToast({ title: '商品已加入购物车',icon:'none' })

}

以上就完成了加入购物车 红点曲线的简化版样式效果实现

相关推荐
HashTang9 小时前
【AI 编程实战】第 7 篇:登录流程设计 - 多场景、多步骤的优雅实现
前端·uni-app·ai编程
计算机程序设计小李同学16 小时前
婚纱摄影集成管理系统小程序
java·vue.js·spring boot·后端·微信小程序·小程序
幽络源小助理19 小时前
SpringBoot+小程序高校素拓分管理系统源码 – 幽络源免费分享
spring boot·后端·小程序
Mr -老鬼20 小时前
移动端跨平台适配技术框架:从发展到展望
android·ios·小程序·uni-app
内存不泄露20 小时前
棋牌预约小程序系统论文
小程序
一颗小青松1 天前
uniapp app端显示天气详情
uni-app
Swift社区2 天前
H5 与 ArkTS 通信的完整设计模型
uni-app·harmonyos
说私域2 天前
短视频私域流量池的变现路径创新:基于AI智能名片链动2+1模式S2B2C商城小程序的实践研究
大数据·人工智能·小程序
小溪彼岸2 天前
uni-app小白从0开发一款鸿蒙Next应用到上线
uni-app·harmonyos
毕设源码-邱学长2 天前
【开题答辩全过程】以 基于微信小程序的松辽律所咨询系统的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序