Vue.js如何实现悬浮窗拖动
2019-08-16 15:12
295 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39531576/article/details/99678371
内容1
内容2
js模块自己写一个自定义制定directives
drag: function(el) {
var that=this;
let dragBox = el; //获取当前元素
dragBox.onmousedown = e => {
sessionStorage.setItem(‘move’,‘startmove’)
//计算鼠标相对元素的位置
let disX = e.clientX - dragBox.offsetLeft;
let disY = e.clientY - dragBox.offsetTop;
document.onmousemove = e => {
sessionStorage.setItem(‘move’,‘move’)
document.getElementById(‘drag_box’).style.cursor=‘move’
//用鼠标的位置减去鼠标相对元素的位置,得到元素的位置2
let left = e.clientX - disX;
let top = e.clientY - disY;
if(top>document.body.scrollHeight-100){
top=document.body.scrollHeight-96
}
if(left>document.documentElement.clientWidth-231){
left=document.documentElement.clientWidth-218
}
if(left<0){
left=0
}
if(top<0){
top=0
}
//移动当前元素
dragBox.style.left = left + “px”;
dragBox.style.top = top + “px”;
};
document.onmouseup = e => {
document.getElementById(‘drag_box’).style.cursor=‘pointer’
//鼠标弹起来的时候不再移动
document.onmousemove = null;
//组织鼠标松开还会移动
document.onmouseup = null;
};
};
}
css模块
.drag_box {
z-index: 10000;
bottom: 0px;
right: 0px;
width: 216px;
height: 94px;
background: #FFFFFF;
position: absolute;
cursor: pointer;
box-sizing: border-box;
border-radius:12px;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 6px 12px 6px rgba(0,0,0,0.12);
h5 代码模块
内容1
内容2
js模块自己写一个自定义制定directives
drag: function(el) {
var that=this;
let dragBox = el; //获取当前元素
dragBox.onmousedown = e => {
sessionStorage.setItem(‘move’,‘startmove’)
//计算鼠标相对元素的位置
let disX = e.clientX - dragBox.offsetLeft;
let disY = e.clientY - dragBox.offsetTop;
document.onmousemove = e => {
sessionStorage.setItem(‘move’,‘move’)
document.getElementById(‘drag_box’).style.cursor=‘move’
//用鼠标的位置减去鼠标相对元素的位置,得到元素的位置2
let left = e.clientX - disX;
let top = e.clientY - disY;
if(top>document.body.scrollHeight-100){
top=document.body.scrollHeight-96
}
if(left>document.documentElement.clientWidth-231){
left=document.documentElement.clientWidth-218
}
if(left<0){
left=0
}
if(top<0){
top=0
}
//移动当前元素
dragBox.style.left = left + “px”;
dragBox.style.top = top + “px”;
};
document.onmouseup = e => {
document.getElementById(‘drag_box’).style.cursor=‘pointer’
//鼠标弹起来的时候不再移动
document.onmousemove = null;
//组织鼠标松开还会移动
document.onmouseup = null;
};
};
}
css模块
.drag_box {
z-index: 10000;
bottom: 0px;
right: 0px;
width: 216px;
height: 94px;
background: #FFFFFF;
position: absolute;
cursor: pointer;
box-sizing: border-box;
border-radius:12px;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 6px 12px 6px rgba(0,0,0,0.12);
}
.drag_box li{
height: 100%;
flex-grow: 1;
text-align: center;
list-style: none;
display: flex;
justify-content: center;
align-items: center;
}
.drag_box span{
position: relative;
top: 12px;
font-family: “microsoft yahei”;
font-size: 20px;
color: #666666;
letter-spacing: 0;
text-align: center;
line-height: 20px;
}
效果如下图,本人把图片去掉了,有需要的依样画葫芦即可,好了,差不多功能到这里,有不懂的可以下面留言
相关文章推荐
- Vue.js实现图片的随意拖动
- 说说如何在 Vue.js 中实现数字输入组件
- js鼠标事件解析——如何用js实现一个拖动但是不触发其点击事件
- 说说在 Vue.js 中如何实现组件间通信
- js实现悬浮窗效果(支持拖动)
- 说说如何在Vue.js中实现数字输入组件的方法
- Vue.js 是如何实现 MVVM 的?
- 浅谈在Vue.js中如何实现时间转换指令
- vue.js vue-router如何实现无效路由(404)的友好提示
- 如何在Vue.js中实现标签页组件详解
- 说说如何基于 Vue.js 实现表格组件
- Vue.js如何实现路由懒加载浅析
- 【111】Vue.js实现页面共用头部悬浮、共用底部跟随内容改变位置的例子。
- Vue.js实现图片的随意拖动方法
- vue.js + element UI实现表格、列表的拖动 推拽效果
- 如何在 vue.js 中实现点击标签进行排序
- 如何在Vue.js中实现标签页组件详解
- 浅谈Vue.js中如何实现自定义下拉菜单指令
- Vue 实现拖动滑块验证功能(只有css+js没有后台验证步骤)
- Vue.js自定义下拉列表,如何实现在下拉列表区域外点击即可关闭下拉列表的功能