vue实现拖拽组件

代码如下:
注意:代码中使用的图片未上传
DragAndDrop组件:
<template>
<div class="drag" id="moveDiv"
@mousedown="start($event)" @touchstart="start($event)"
@mousemove="move($event)" @touchmove="move($event)"
@mouseup="end($event)" @touchend="end($event)">
<slot name="drag-cont"></slot>
<div id="optionBall"></div>
<div id="optionContent" v-show="optionContentShow">
<span class="imgWrap" @click="gatewayOption" v-bind:style="{backgroundColor : this.$parent.whichOption? '#ff5b45' : '#ffffff'}" >
<img v-bind:src="this.$parent.whichOption?require('../assets/img/SmartGateway_write.png'):require('../assets/img/SmartGateway.png')" alt="" width="24px" height="24px">
</span>
<span class="imgWrap" @click="alertOption" v-bind:style="{backgroundColor : !this.$parent.whichOption? '#ff5b45' : '#ffffff'}">
<img v-bind:src="!this.$parent.whichOption?require('../assets/img/alert_write.png'):require('../assets/img/alert.png')" alt="" width="24px" height="24px">
</span>
</div>
</div><!--E 拖动组件 -->
</template> <script>
export default {
name: "dragAndDrop",
data() {
return {
position: {x: 0,y: 0}, // 鼠标点击的x轴和y轴的距离
nx: '', // 鼠标当前距离元素的左侧距离
ny: '', // 鼠标当前距离元素的顶部距离
dx: '', // 元素距离左侧的距离
dy: '', // 元素距离顶部的距离
xPum: '', // 元素移动的x轴距离
yPum: '', // 元素移动的y轴距离
optionContentShow: true
};
},
methods: {
start(e){
// 如果touches存在就说明是移动端
// 否则为pc端直接获取事件源对象
let touch = e.touches? e.touches[0] : e;
this.position.x = touch.clientX;
this.position.y = touch.clientY;
this.dx = moveDiv.offsetLeft;
this.dy = moveDiv.offsetTop;
this.optionContentShow = true;
},
move(e){
this.isDrop = true;
if(this.isDrop){
let touch = e.touches? e.touches[0] : e;
this.nx = touch.clientX - this.position.x;
this.ny = touch.clientY - this.position.y;
this.xPum = this.dx+this.nx;
this.yPum = this.dy+this.ny;
moveDiv.style.left = this.xPum + "px";
moveDiv.style.top = this.yPum + "px";
document.addEventListener("touchmove",function(){
event.preventDefault();
},false);
if(e.preventDefault){
e.preventDefault();
}else{
window.event.returnValue == false;
}
} },
end(e){
let oWidth = moveDiv.offsetWidth; // Element Width
let oWrapWidth = moveDiv.parentNode.offsetWidth; // Parent Element Width
let oWrprapHeight = moveDiv.parentNode.offsetHeight; // Parent Element Height
let sumWidth = moveDiv.offsetLeft + oWidth; // Element Left + Element Width
let sumHeight = moveDiv.offsetTop + moveDiv.offsetHeight; // Element Top + Element Height
// The Limit Deal
if(moveDiv.offsetLeft < 0) {
moveDiv.style.left = 0;
this.optionContentShow = false;
moveDiv.style.left = "-30px";
} else if(sumWidth > oWrapWidth){
moveDiv.style.left = oWrapWidth - oWidth + 'px';
// console.log("到最右边了");
this.optionContentShow = false;
moveDiv.style.left = "-30px";
} else if(moveDiv.offsetTop < 0) {
moveDiv.style.top = 0;
} else if(sumHeight > oWrprapHeight) {
moveDiv.style.top = oWrprapHeight - moveDiv.offsetHeight + 'px';
}
document.onmousemove = null;
document.onmouseup = null;
},
gatewayOption: function () {
this.$parent.gatewayOption();
},
alertOption: function () {
this.$parent.alertOption();
},
}
};
</script> <style scoped>
.drag {
width: 160px;
height: 60px;
position: absolute;
left: 40px;
bottom: 60px;
z-index: 999;
} #optionBall {
width: 56px;
height: 56px;
position: absolute;
background-color: #ff5b45;
border-radius: 56px;
z-index: 20;
} #optionContent {
width: 130px;
height: 50px;
background-color: #ff956b;
position: absolute;
left: 20px;
margin-top: 3px;
border-radius: 50px;
z-index: 10;
padding: 6px 0 6px 34px;
box-sizing: border-box;
} .imgWrap {
display: inline-block;
width: 38px;
height: 38px;
background-color: #ffffff;
border-radius: 40px;
padding: 8px;
box-sizing: border-box;
} .checked {
background-color: #ff5b45;
}
</style> 父组件:subDevice.vue
<template>
<DragAndDrop></DragAndDrop>
</template>
<script>
import DragAndDrop from "./DragAndDrop";
export default {
name: "subDevice",
components: {DragAndDrop,InfiniteScroll},
data() {
return {
whichOption: true
}
},
methods:{
gatewayOption: function () {
this.whichOption = true;
},
alertOption: function () {
this.whichOption = false;
}
}
}
</script>
vue实现拖拽组件的更多相关文章
- Vue 可拖拽组件 Vue Smooth DnD 详解和应用演示
本文发布自 https://www.cnblogs.com/wenruo/p/15061907.html 转载请注明出处. 简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu ...
- vue列表拖拽组件 vue-dragging
安装 $ npm install awe-dnd --save 应用 在main.js中,通过Vue.use导入插件 import VueDND from 'awe-dnd' Vue.use(VueD ...
- Vue.Draggable:基于 Sortable.js 的 Vue 拖拽组件使用中遇到的问题
Sortable.js 介绍 https://segmentfault.com/a/1190000008209715 项目中遇到的问题: A - 我需要在项目的拖拽组件中,使用背景 1 - 想到的第一 ...
- vue拖拽组件开发
vue拖拽组件开发 创建临时vue项目 先查看node和npm版本,怎么安装就不多多bb了 再安装vue-cli npm install vue-cli -g //全局安装 vue-cli 检测是否安 ...
- vue自由拖拽、缩放组件
github地址:https://github.com/kirillmurashov/vue-drag-resize 安装: npm i -s vue-drag-resize 使用: <temp ...
- vue-slicksort拖拽组件
vue-slicksort拖拽组件 安装 通过npm安装 $ npm install vue-slicksort --save 通过yarn安装 $ yarn add vue-slicksort 插件 ...
- Vue-Grid-Layout分享一款好用的可拖拽组件
在使用Grafana的过程中,发现Grafana关于视图页面中每一个面板都可拖拽,可随意放大放小,体验非常棒,F12看了Grafana的代码,看打包后的代码很像react,进一步css,看到有grid ...
- React拖拽组件Dragact V0.1.7:教你优化React组件性能与手感
仓库地址:Dragact手感丝滑的拖拽布局组件 预览地址:支持手机端噢- 上回我们说到,Dragact组件已经进行了一系列的性能优化,然而面对大量数据的时候,依旧比较吃力,让我们来看看,优化之前的Dr ...
- Vue实现拖拽穿梭框功能四种方式
一.使用原生js实现拖拽 点击打开视频讲解更加详细 <html lang="en"> <head> <meta charset="UTF-8 ...
随机推荐
- jquery解析XML文件实现的省市联动
XML我是直接在网上下载的文件包 拿过来用的 jquery我用的是3.1的 前台页面 <form action="buy.html" method="get&quo ...
- [牛腩]如何关闭.net framework4.0的请求验证 标签: 发布 2015-07-31 09:27 887人阅读 评论(38)
敲牛腩的时候,点击运行提示:从客户端中检测到有潜在危险的 Request.Form 值,感觉自己代码敲的并没有问题,于是开始各种查,下面分享一下我对此进行的研究. 为什么会报这个错误? 在 Web 应 ...
- jQuery 加法计算 使用+号即强转类型
var value1 = $("#txt1").val(); var value2 = $("#txt2").val(); //数值前添加+号 number加号 ...
- 使用DECLARE定义条件和处理程序
定义条件和处理程序是事先定义程序执行过程中可能遇到的问题,并且可以在处理程序中定义解决这些问题的办法,可以简单理解 为异常处理,这种方式可以提前预测可能出现的问题,并提出解决办法,从而增强程序健壮性. ...
- django之请求方法
Http1.0定义了三种请求方法:GET,POST和HEAD方法 Http1.1新增了五种请求方式:OPTIONS,PUT,DELETE,TRACE和CONNECT方法 ----get ...
- hdu 1077 (圆交)
Problem - 1077 我们可以知道,当这个单位圆可以覆盖到最多的点的时候,必定最少有两个点位于这个圆的圆周上,于是就有网上众多的O(N^3)的枚举两个在圆上的点的暴搜做法. 然而这题是可以用圆 ...
- Streamy障碍一:大批量条目
- PAN-OS 6.1 Open Source Software (OSS) Listing
https://www.paloaltonetworks.com/documentation/oss-listings/oss-listings/pan-os-oss-listings/pan-os- ...
- iptables 详细使用
检查状态 先检查是否安装了iptables $ service iptables status 安装iptables $ yum install iptables 升级iptables $ yum u ...
- Android 动态设置控件获取焦点
之前写过一篇博客,简单的介绍了Android 隐藏EditText的焦点,之所以要隐藏EditText的焦点,是因为当应用在第一次进入某个Activity时,由于该页面中的EditText获取了焦点, ...