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

	}
相关推荐
GISer_Jing2 分钟前
[总结篇]个人网站
前端·javascript
ss.li4 分钟前
TripGenie:畅游济南旅行规划助手:个人工作纪实(二十二)
javascript·人工智能·python
疯狂的沙粒24 分钟前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
小妖66628 分钟前
html 滚动条滚动过快会留下边框线
前端·html
heroboyluck42 分钟前
Svelte 核心语法详解:Vue/React 开发者如何快速上手?
前端·svelte
海的诗篇_43 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
琹箐1 小时前
ant-design4.xx实现数字输入框; 某些输入法数字需要连续输入两次才显示
前端·javascript·anti-design-vue
程序员-小李1 小时前
VuePress完美整合Toast消息提示
前端·javascript·vue.js
Uyker2 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
Dontla5 小时前
为什么React列表项需要key?(React key)(稳定的唯一标识key有助于React虚拟DOM优化重绘大型列表)
javascript·react.js·ecmascript