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

	}
相关推荐
海上彼尚12 分钟前
使用 npm-run-all2 简化你的 npm 脚本工作流
前端·npm·node.js
开发者小天1 小时前
为什么 /deep/ 现在不推荐使用?
前端·javascript·node.js
如白驹过隙1 小时前
cloudflare缓存配置
前端·缓存
excel1 小时前
JavaScript 异步编程全解析:Promise、Async/Await 与进阶技巧
前端
Jerry说前后端2 小时前
Android 组件封装实践:从解耦到架构演进
android·前端·架构
不如摸鱼去2 小时前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程
步行cgn2 小时前
在 HTML 表单中,name 和 value 属性在 GET 和 POST 请求中的对应关系如下:
前端·hive·html
hrrrrb2 小时前
【Java Web 快速入门】十一、Spring Boot 原理
java·前端·spring boot
找不到工作的菜鸟3 小时前
Three.js三大组件:场景(Scene)、相机(Camera)、渲染器(Renderer)
前端·javascript·html
定栓3 小时前
vue3入门-v-model、ref和reactive讲解
前端·javascript·vue.js