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

	}
相关推荐
西西学代码14 分钟前
Flutter---异步编程
开发语言·前端·javascript
拉不动的猪15 分钟前
CSS 像素≠物理像素:0.5px 效果的核心密码是什么?
前端·css·面试
前端市界20 分钟前
Copilot新模型GPT-5.1太强了!自动生成完美Axios封装,同事都看傻了
前端·前端框架·github
米欧21 分钟前
取消当前正在进行的所有接口请求
前端·javascript·axios
浪里行舟26 分钟前
告别“拼接”,迈入“原生”:文心5.0如何用「原生全模态」重塑AI天花板?
前端·javascript·后端
OpenTiny社区26 分钟前
救命!这个低代码工具太香了 ——TinyEngine 物料自动导入上手
前端·低代码·github
努力学前端Hang28 分钟前
移动端跨平台开发深度解析:UniApp、Taro、Flutter 与 React Native 对比
前端·javascript·react native·react.js
前端九哥39 分钟前
🚫循环里写return,浏览器当场沉默!
前端·javascript
亲爱的马哥1 小时前
填鸭表单!开箱即用的开源问卷调查系统!
java·前端·低代码·产品经理
米诺zuo1 小时前
nextjs文件路由、路由组
前端·next.js