手机端悬浮按钮包含个菜单 可拖动

新建组件 fab.vue  基于uni-ui框架
<template>
	<view>
		<!-- 悬浮按钮 多个菜单 -->
		<movable-area class="movable-area">
			<movable-view class="movable-view" :x="x" :y="y" direction="all" @touchmove.stop>
				<uni-fab class="fab" ref="fab" :pattern="pattern" :content="content" :horizontal="horizontal"
					:vertical="vertical"  :direction="direction" @trigger="trigger" @fabClick="fabClick"/>
			</movable-view>
		</movable-area>
	</view>
</template>
 
<script>
	export default {
		props:['horizontal','vertical','direction','pattern','content'],
		data() {
			return {
				x: 320,		//x坐标
				y: 480,		//y坐标
			}
		},
		mounted() {
			//根据屏幕高度设置y坐标
			let that = this
			uni.getSystemInfo({
				success: function(res) {
					// 计算组件的高度
					that.y = res.windowHeight -10
				}
			})
		},
		methods: {
            fabClick(){
                //点击事件
                this.$emit('fabClick') //回调父组件方法
            },
			trigger(e){
                //点击菜单
				this.$emit('clickSet',e) //回调父组件方法
			},
			closePress() {
				if (this.$refs.fab.isShow) {
					this.$refs.fab.close()
					return true
				}
				return false
			}
		}
	}
</script>
 
<style lang="scss">
	$all_width: 96rpx;
	$all_height:96rpx;
 
	.movable-area {
		height: 100vh;
		width: 750rpx;
		top: 0;
		position: fixed;
		z-index: 10;
		pointer-events: none;		//此处要加,鼠标事件可以渗透
		.movable-view {
			width: $all_width;
			height: $all_height;
			pointer-events: auto;	//恢复鼠标事件
			// image {
			// 	width: $all_width;
			// 	height: $all_height;
			// }
		}
	}
	::v-deep .uni-fab__content--other-platform {
	    box-shadow: 0 1px 5px 2px rgba(241, 233, 233, 0.1);
	}
	.imgView{
		width:55px;
		height:55px;
		border-radius: 100%;
		border:2px solid  #b7b4b4;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color:#007AFF;
		color:#fff;
		font-size: 45px;
	}
</style>

 在页面引用
 

<template>
    <view>
<!-- 悬浮按钮 -->
		<fab @clickSet="clickSet" @fabClick="fabClick" :horizontal='horizontal' :vertical='vertical' :pattern='pattern' :content='content'></fab>
	</view>
</template>



//基于uni-ui
import fab from "@/components/fab/fab.vue";

export default {
    data() {
			return {
				horizontal:'right',
				vertical:'bottom',
				content: [{
					iconPath: '/static/contractChange.png',
					selectedIconPath: '/static/contractChange-active.png',
					text: '标准合同',
					active: false
				}, {
					iconPath: '/static/contractChange.png',
					selectedIconPath: '/static/contractChange-active.png',
					text: '非标合同',
					active: false
				}],
				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#12c1dd',
					buttonColor: '#12c1dd',
					iconColor: '#fff',
					text: '合同对比',
				},
			},
        components: {
            fab
        },
        methods:{
            fabClick(){

            },
            clickSet(e){
                console.log(e.index)
            }
          }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现一个可拖动悬浮按钮,可以使用以下步骤: 1. 在 HTML 中添加一个悬浮按钮的元素,比如一个 `<div>` 标签。 ```html <div class="floating-button"></div> ``` 2. 使用 CSS 设置该元素的样式,比如设置它的位置、大小、背景色、圆角等。 ```css .floating-button { position: fixed; bottom: 20px; right: 20px; width: 50px; height: 50px; background-color: #007bff; border-radius: 50%; z-index: 9999; } ``` 3. 使用 JavaScript 实现拖动功能。可以监听该元素的 `mousedown`、`mousemove`、`mouseup` 事件,通过计算鼠标的位置和元素的位置差,实现元素的拖动。 ```javascript var floatingButton = document.querySelector('.floating-button'); var isDragging = false; var lastX, lastY; floatingButton.addEventListener('mousedown', function(e) { isDragging = true; lastX = e.clientX; lastY = e.clientY; }); floatingButton.addEventListener('mousemove', function(e) { if (isDragging) { var deltaX = e.clientX - lastX; var deltaY = e.clientY - lastY; var rect = floatingButton.getBoundingClientRect(); floatingButton.style.left = rect.left + deltaX + 'px'; floatingButton.style.top = rect.top + deltaY + 'px'; lastX = e.clientX; lastY = e.clientY; } }); floatingButton.addEventListener('mouseup', function() { isDragging = false; }); ``` 这样就实现了一个可拖动悬浮按钮。需要注意的是,对于移动,需要考虑触摸事件而非鼠标事件。同时,需要使用 CSS 设置 `touch-action: none;`,防止浏览器默认的滚动行为干扰拖动操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值