vue 悬浮图标_vue实现可拖拽移动悬浮球
拖拽移动悬浮球
需求拆解
1.元素悬浮于全屏
2.元素可拖拽
3.元素拖拽结束后会吸附贴壁
4.元素单击唤出菜单
5.菜单展开时点击空白处关闭菜单
6.菜单不可拖拽
7.元素拖拽时菜单不打开
8.元素拖拽至接近底部时,若剩余高度不足以展开菜单则自动吸附至底部。
实现思路
如果适配移动端,则需加注touch事件
1.鼠标按下时
1.1.如果此时已打开菜单,则不做响应
1.2.未打开菜单,记录 按下状态为true 记录x y轴坐标
2.按下移动时 动态计算坐标 设置拖拽元素 style 控制位置 ;
2.1.判断拖拽区域 溢出时 归位判断;
2.2.拖拽时 阻止页面滑动
2.3.增加拖拽计数
3.鼠标抬起
3.1.修改 按下状态 为false
3.2.根据元素位置和容器高宽动态计算元素最后应该吸附位置
4.元素点击事件
4.1.拖拽计数与历史计数差小于10则执行点击事件
4.2.反之不执行
实现过程
:class="{'zlevelTop':mouseDownState}"
style="position: absolute;top: 0;height: 100%;width: 100%">
style="position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 998">
ref="actionMgr" :style="position" @mousedown="demo_down">
export default {
name: "homeDragbtn",
props: {
// 通过position来设置初始定位
position: {
type: Object,
default: function() {
return {
top: "32.25rem",
left: "0"
}
}
}
},
data() {
return {
menuOpen:false, // 菜单展开状态
mouseDownState:false, // 鼠标点击状态
iX:0, iY:0,
dX:0, dY:500, // 初始定位
lastMoveIndex:0, // 拖拽计数
curMoveIndex:0, // 历史计数
}
},
methods: {
// 鼠标按下
demo_down(event){
// 如果打开了菜单,则不做响应
if(this.menuOpen){
this.mouseDownState = false;
return
}
console.log("demo_down",event);
/* 此处判断 pc 或 移动端 得到 event 事件 */
var touch;
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
// 鼠标点击 面向页面 的 x坐标 y坐标
let { clientX, clientY } = touch;
// 鼠标x坐标 - 拖拽按钮x坐标 得到鼠标 距离 拖拽按钮 的间距
this.iX = clientX - this.$refs.actionMgr.offsetLeft;
// 鼠标y坐标 - 拖拽按钮y坐标 得到鼠标 距离 拖拽按钮 的间距
this.iY = clientY - this.$refs.actionMgr.offsetTop;
// 设置当前 状态为 鼠标按下
this.mouseDownState = true;
},
// 鼠标拖拽
demo_move(event){
//鼠标按下 切移动中
if (this.mouseDownState) {
console.log("demo_move",event);
/* 此处判断 pc 或 移动端 得到 event 事件 */
var touch;
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
// 鼠标移动时 面向页面 的 x坐标 y坐标
let { clientX, clientY } = touch;
//当前页面全局容器 dom 元素 获取容器 宽高
let {
clientHeight: pageDivY,
clientWidth: pageDivX
} = this.$refs.pageDiv;
/* 鼠标坐标 - 鼠标与拖拽按钮的 间距坐标 得到 拖拽按钮的 左上角 x轴y轴坐标 */
let [x, y] = [clientX - this.iX, clientY - this.iY];
//拖拽按钮 dom 元素 获取 宽高 style 对象
let {
clientHeight: actionMgrY,
clientWidth: actionMgrX,
style: actionMgrStyle
} = this.$refs.actionMgr;
/* 此处判断 拖拽按钮 如果超出 屏幕宽高 或者 小于
设置 屏幕最大 x=全局容器x y=全局容器y 否则 设置 为 x=0 y=0
*/
if (x > pageDivX - actionMgrX) x = pageDivX - actionMgrX;
else if (x < 0) x = 0;
if (y > pageDivY - actionMgrY) y = pageDivY - actionMgrY;
else if (y < 0) y = 0;
this.dX =x;this.dY = y;
// 计算后坐标 设置 按钮位置
actionMgrStyle.left = `${x}px`;
actionMgrStyle.top = `${y}px`;
actionMgrStyle.bottom = "auto";
actionMgrStyle.right = "auto";
// move Index
this.lastMoveIndex++;
// 当按下键滑动时, 阻止屏幕滑动事件
event.preventDefault();
}
},
// 鼠标抬起
demo_up(event){
console.log("demo_up",event);
// 当前页面全局容器 dom 元素 获取容器 宽高
let {
clientHeight: windowHeight,
clientWidth: windowWidth
} = document.documentElement;
console.log('全局容器:',windowWidth,windowHeight);
// 拖拽按钮 dom 元素 获取 宽高 style 对象
let {
clientHeight: actionMgrY,
clientWidth: actionMgrX,
style: actionMgrStyle
} = this.$refs.actionMgr;
console.log('拖拽按钮',actionMgrY,actionMgrX,actionMgrStyle);
// 计算后坐标 设置 按钮位置
if(this.dY>0&&this.dY
if(this.dX<=(windowWidth/2)){ // left 小于等于屏幕一半
actionMgrStyle.left = 0;
actionMgrStyle.right = 'auto';
}else { // left 大于屏幕一半
actionMgrStyle.left = 'auto';
actionMgrStyle.right = 0;
}
if(this.dY>=(windowHeight/2)){ // 宽度大于1/2时,是将top改为auto,调整bottom
actionMgrStyle.top = 'auto';
actionMgrStyle.bottom = (windowHeight - this.dY - 50) + 'px';
}
}else {
if(this.dY===0){ // 在顶部
actionMgrStyle.top = 0;
actionMgrStyle.bottom = 'auto';
}else if(this.dY===(windowHeight-50)){
actionMgrStyle.bottom = 0;
actionMgrStyle.top = 'auto';
}
if(this.dX>=(windowWidth/2)){ // 右侧是将left改为auto,调整right
actionMgrStyle.left = 'auto';
actionMgrStyle.right = (windowWidth - this.dX - 50) + 'px';
}
}
this.mouseDownState = false;
},
// 单击事件
demo_click(){
console.log("demo_click|moveIndex:",this.lastMoveIndex,this.curMoveIndex);
// mouseup 后会激活click事件
// 如果上一次down事件到下一次click事件中经历10次以下move,则视为纯点击事件
if(this.lastMoveIndex-this.curMoveIndex<=10){
// 点击事件
this.menuOpen = !this.menuOpen;
if( this.menuOpen ){
// 打开菜单
}
}
this.curMoveIndex = this.lastMoveIndex
},
// 点击空白关闭菜单
closeOpenModal(){}
}
}
.zlevelTop{
z-index: 9999;
}
.more-tran-animate{
transition:0.5s;
}
.moreModal {
/* 如果碰到滑动问题,1.3 请检查 z-index。z-index需比web大一级*/
z-index: 9999;
position: fixed;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #337AB7;
line-height: 40px;
text-align: center;
color: #fff;
opacity: 0.6;
}
.moreModal:hover{
opacity: 1;
}
.six-more-modal-btn{
position: fixed;
z-index: 9999;
width: 14rem;
height: 14rem;
border-radius: 5px;
background: #1A1A1A;
color: #fff;
}
.imgMore{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
vue 悬浮图标_vue实现可拖拽移动悬浮球相关推荐
- vuedraggable能实现自由拖拽功能吗?_基于 vue.js 仿禅道主页拖拽效果
今天给大家分享一个超不错的Vue仿禅道首页拖拽布局VueDndKon. vue-dnd-kon 基于vuedraggable实现的仿禅道首页拖拽项目.支持模块上下及左右自由拖动布局. 主页分为左右两栏 ...
- Android自定义可拖拽的悬浮按钮---DragFloatingActionButton
悬浮按钮FloatingActionButton是Android 5.0系统添加的新控件,FloatingActionButton是继承至ImageView,所以FloatingActionButto ...
- Android进阶之路 - 可拖拽的悬浮按钮
类似文章在CSDN上有很多,但是几经查找之后原文其实产于简书的一位作者: 综合几篇文章,在原有基础上我会尽可能全面总结一下 效果图 实现思路 通过重写控件的onTouchEvent方法监听触摸效果 通 ...
- html移动小图标,html5 实现可拖拽移动的悬浮图标
h5 上经常会有这样的需求: 需要在页面上加上一个悬浮图标,大致是如下图的最终实现 但是往往按照设计稿是不会遮住主体区域的,但是实际上有时候偏偏会遮挡主体区域,但是为了更好的点击量,又不得不悬浮在页面 ...
- vue 悬浮图标_vue实现移动端悬浮窗效果
本文讲述,在使用VUE的移动端实现类似于iPhone的悬浮窗的效果. 相关知识点 touchstart当在屏幕上按下手指时触发 touchmove 当在屏幕上移动手指时触发 touchend 当在屏幕 ...
- vue——js实现图片/文件的拖拽上传(复制粘贴就能用,还有优化空间)
首先先创建元素容器 <template><div id="drop"><span v-show="isUpload" class= ...
- vue拖动改变模板_可视化拖拽 UI 布局之拖拽篇
前言:前段时间负责公司的运营管理后台项目,通过运营后台的PC端拖拽配置布局,达到App首页模板的动态UI界面配置,生成页面.趁着周末,整理一下当时所了解到的拖拽.文章会根据大家的反馈或者自己学习经验的 ...
- vue aplayer 进度条无法拖动_「最近项目小结」使用Vue实现一个简单的鼠标拖拽滚动效果插件...
演示事例 http://www.longstudy.club/vue-drag-scroll/index.html 最近在做一个新的项目,有个需求是这样的: 简单描述一下,就是鼠标拖动页面,整个页面会 ...
- java 不让滚动条随着拖拽滑动_「最近项目小结」使用Vue实现一个简单的鼠标拖拽滚动效果插件...
演示事例 http://www.longstudy.club/vue-drag-scroll/index.html 最近在做一个新的项目,有个需求是这样的: 简单描述一下,就是鼠标拖动页面,整个页面会 ...
最新文章
- 固态硬盘与QLC闪存
- java web面试题大全_Java经典面试题之Java web开发汇总(附答案)
- 密码技术--国密SM4分组密码算法及Go语言应用
- linux什么用户什么任务,Linux 用户
- wireshark抓包工具的使用及分析
- iOS 直播推流 - 搭建基于RTMP的本地Nginx服务器
- 第512章 河系量子计算机,第512章 河系量子计算机
- [译]LightSwitch 如何实现:在查询中创建和使用全局值(Eric Erhardt)
- clickhouse小结--数据类型及常见客户端类型
- ENVI学习总结(二)——基于自带定位信息的几何校正
- f4小组专用3306mysql抓鸡工具_【技术】3306端口手动入侵之mysql写入启动项提权
- 笔记本计算机内部部件,笔记本内部硬件构造有哪些
- c51单片机超声测距hcsr04在 rtos运行的 实验 基于陈明计先生的smallrtos
- 前端克隆数据 --JS 深浅拷贝
- 教你同时分析圆通快递多个单号的物流情况
- 双十一快件近40亿再创历史新高;疫情挑战下中国受访者对科学的信任度位居全球第一 | 美通企业日报...
- CMNET和CMWAP GPRS 连接
- 量化交易是什么意思?怎么理解?
- Arduino实现数码管动态显示
- ffmpeg添加字幕(包含srt文件)
热门文章
- 摩拜单车拉勾网php,Python拉勾网数据采集与可视化
- fs.readFile和fs.readFileSync的区别
- 案例精选 | 左耳朵耗子:如何写出让同事无法维护的代码?
- 如何使用 SAP Intelligent Robotic Process Automation 自动操作 Excel
- 192.168.0.1/27 表示什么
- MATLAB中对矩阵元素操作的for循环优化方法
- win10可以上网,但显示小地球的情况(未连接到网络)
- 【了凡四训-摘抄】修心养性
- 计算机ipad手机组成,如何为自适应手机,计算机和iPad制作网页的摘要
- JAVA 类名.class是什么意思?