uniapp vue2 自定义悬浮按钮

组件代码

粘贴即用

javascript 复制代码
<template>
	<view>
		<movable-area class="movable-area">
			<movable-view class="movable-view" :x="x" :y="y" direction="all">
				<slot name="img"></slot>
			</movable-view>
		</movable-area>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				x: 350, //x坐标
				y: 600, //y坐标
			}
		}
	}
</script>

<style lang="scss">
	$all_width: 108rpx;
	$all_height: 108rpx;

	.movable-area {
		height: 90vh; 
		width: 750rpx;
		top: 0;
		position: fixed;
		pointer-events: none; //此处要加,鼠标事件可以渗透
		z-index: 99999999;

		.movable-view {

		
			width: $all_width;
			height: $all_height;
			pointer-events: auto; //恢复鼠标事件

			image {
				// width: $all_width;
				// height: $all_height;
			}
		}
	}
</style>

父组件使用

需求如不满足请自行修改

第一种方式使用

效果图

javascript 复制代码
<View @tap.stop="goMassage()">
			<floatButton>
                //通过插槽来将要显示的图片显示
				<template v-slot:img>
					<view class="butCustom flex jc ac">
                         //这里是显示的图片
						<image src="" mode=""></image>
					</view>
				</template>
			</floatButton>
</View>




js跳转等操作
	goMassage() {
				//要执行的操作
			},


css样式
//样式和背景色都可以在这设置
	.butCustom {
		width: 100rpx;
		height: 100rpx;
		background: #1D9673;
		border-radius: 999px;
	}

第二种使用方式

效果图

无消息

有消息

javascript 复制代码
		<View @tap.stop="goMassage()">
			<floatButton>
				<template v-slot:img>
					<view class="butCustom flex jc ac">
                        //消息的数量
						<view class="newmagTotal flex jc ac" v-if="newmagTotal>0">
							{{newmagTotal}}
						</view>
                         //展示消息图片
						<image src="" mode=""></image>
					</view>
					
				</template>
			</floatButton>
		</View>




js跳转等操作
	goMassage() {
				//要执行的操作
			},


css样式
//样式和背景色都可以在这设置
.butCustom {
		width: 100rpx;
		height: 100rpx;
		background: #1D9673;
		border-radius: 999px;
		position: relative;

		.newmagTotal {
			position: absolute;
			right: -5rpx;
			top: -5rpx;
			padding: 3px 10rpx;
			box-sizing: border-box;
			background-color: red;
			color: #fff;
			border-radius: 999px;
			font-size: 20rpx;
		}

	}
相关推荐
小徐_233318 小时前
2025,AI 编程元年,我用 TRAE 做了这些!
前端·程序员·trae
沛沛老爹18 小时前
Web开发者实战RAG评估:从指标到工程化验证体系
前端·人工智能·llm·agent·rag·评估
软件技术NINI18 小时前
JavaScript性能优化实战指南
前端·css·学习·html
前端小配角18 小时前
React难上手原因找到了,原来是因为坑太多了。。。
前端
是你的小橘呀18 小时前
零基础也能懂!React Hooks实战手册:useState/useEffect上手就会,告别类组件
前端·架构
xhxxx18 小时前
从样式到结构:TailwindCss + Fragment 如何让 React 代码更干净、更高效
前端·css·react.js
Maxkim19 小时前
「✍️JS原子笔记 」深入理解JS数据类型检测的4种核心方式
前端·javascript·面试
小高00719 小时前
Elips-Core:轻量级 Node.js Web 框架核心实现
前端·javascript·node.js
Focus_19 小时前
SSE+broadcastChannel
前端
zabr19 小时前
前端已死?我用 Trae + Gemini 零代码手搓 3D 塔罗牌,找到了新出路
前端·人工智能·aigc