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;
		}

	}
相关推荐
hqk2 小时前
鸿蒙零基础语法入门:开启你的开发之旅
android·前端·harmonyos
AAA阿giao2 小时前
大厂面试之反转字符串:深入解析与实战演练
前端·javascript·数据结构·面试·职场和发展·编程技巧
专业抄代码选手2 小时前
告别“屎山”:用 Husky + Prettier + ESLint 打造前端项目的代码基石
前端
想进字节冲啊冲2 小时前
Vibe Coding 实战指南:从“手写代码”到“意图设计”的前端范式转移
前端·ai编程
2501_915921433 小时前
抓包技术全面指南:原理、工具与应用场景
android·ios·小程序·https·uni-app·iphone·webview
离&染3 小时前
宝塔nginx一个域名部署两个前端和两个后端(VUE3)
前端·nginx
朱哈哈O_o3 小时前
前端通用包的作用——md5篇
前端
Lsx_3 小时前
🔍 React 有 useAntdTable,Vue3 怎么办?自封一个 useTable!
前端·javascript·vue.js
O***p6043 小时前
TypeScript类型守卫
前端·javascript·typescript
小希smallxi3 小时前
在 Spring Boot 项目中,如何在非 Web 层(如 AOP)中获取 Session 信息
前端·spring boot·后端