vue实现移动端拖动排序


Posted in Javascript onAugust 21, 2020

本文实例为大家分享了vue实现移动端拖动排序的具体代码,供大家参考,具体内容如下

效果

vue实现移动端拖动排序

代码:

<template>
 <div>
 <div class="mainDiv" id="columns">
 <div id="child"
  class="childDiv"
  v-for="(option,index) in options"
  :key="index"
 >
 {{option}}
 </div>

 <!-- <div id="test" class="test"
  @touchstart="down" @touchmove="move" @touchend="end"
 >什么都没有
 </div>-->
 </div>
 </div>
</template>
<script>
 export default {
 name: "touchMove",
 data() {
  return {
  options: ['选项1', '选项2', '选项3', '选项4'],
  columns: undefined,
  flags: false,
  position: {x: 0, y: 0},
  nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',
  }
 },
 mounted() {
  this.columns = document.querySelectorAll('#child');
  let num = 0;
  for (let i of this.columns) {
  i.style.top = (i.offsetHeight * num) + 'px';
  i.addEventListener('touchstart', this.down);
  i.addEventListener('touchmove', this.move);
  i.addEventListener('touchend', this.end);
  num ++;
  }
 },
 methods: {
  down(e) {
  e.preventDefault();
  this.flags = true;
  var touch;
  if (e.touches) {
   touch = e.touches[0];
  } else {
   touch = e;
  }
  /*touch.clientX clientY 鼠标点击的位置与视图窗口的距离
  * e.target.offsetLeft offsetTop 鼠标点击的div与父元
  * 素的边框距离,父元素必须为定位样式,不然认为父元素为body
  * */
  this.position.x = touch.clientX;
  this.position.y = touch.clientY;
  this.dx = e.target.offsetLeft;
  this.dy = e.target.offsetTop;
  },
  move(e) {
  if (this.flags) {
   var touch;
   if (e.touches) {
   touch = e.touches[0];
   } else {
   touch = 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;
   e.target.style.left = this.xPum + 'px';
   e.target.style.top = this.yPum + 'px';
  }
  },
  end(e) {
  //处理边界问题
  let right= e.target.offsetLeft + e.target.offsetWidth;
  let bottom = e.target.offsetTop + e.target.offsetHeight;
  if(e.target.offsetLeft <= 0 || right >= e.target.offsetParent.offsetWidth){
   e.target.style.left = 0 + 'px';
  }
  if(e.target.offsetTop <= 0 || bottom >= e.target.offsetParent.offsetHeight){
   e.target.style.top = 0 + 'px';
  }
  this.dataTransfer(e);
  this.flags = false;
  },
  dataTransfer(e){
  let eleTop = e.target.offsetTop + Math.round(e.target.offsetHeight / 2);//找到当前元素的中间位置
  let arr = Array.from(this.columns);//将nodelist转为array
  let index = arr.indexOf(e.target);//找到当前元素下标
  for(let i in arr){
   //如果当前元素进入另一个元素的位置,将他们的值互换,位置还原
   if(eleTop > arr[i].offsetTop && eleTop < (arr[i].offsetTop + arr[i].offsetHeight)){
   //值互换,位置还原(保证数组的序列数据不变)
   let temp = arr[index].innerText;
   arr[index].innerText = arr[i].innerText;
   arr[i].innerText = temp;
   }
  }
  let num = 0;
  for (let i of this.columns) {
   i.style.top = (i.offsetHeight * num) + 'px';
   num ++;
  }
  }
 }
 }
</script>
<style scoped>
 .mainDiv {
 position: absolute;
 height: 500px;
 width: 100%;
 border: 3px solid red;
 border-radius: 10px;
 margin: 10px;
 }

 .mainDiv > .childDiv {
 position: absolute;
 height: 50px;
 width: 90%;
 background-color: blue;
 border: 2px solid;
 border-radius: 10px;
 margin: 1px auto;
 padding: 10px;
 text-align: center;
 }


 .test {
 position: relative;
 height: 50px;
 width: auto;
 background-color: red;
 border: 2px solid;
 border-radius: 3px;
 margin: 1px 0 1px;
 padding: 10px;
 text-align: center;
 }

</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript中数组的排序、乱序和搜索实现代码
Nov 30 Javascript
解决jquery异步按一定的时间间隔刷新问题
Dec 10 Javascript
删除Javascript Object中间的key
Nov 18 Javascript
swtich/if...else的替代语句
Aug 16 Javascript
js 自带的 map() 方法全面了解
Aug 16 Javascript
最全面的百度地图JavaScript离线版开发
Sep 10 Javascript
最常见和最有用的字符串相关的方法详解
Feb 06 Javascript
Java中int与integer的区别(基本数据类型与引用数据类型)
Feb 19 Javascript
深入剖析JavaScript instanceof 运算符
Jun 14 Javascript
js判断一个对象是数组(函数)的方法实例
Dec 19 Javascript
Vue.js中使用Vuex实现组件数据共享案例
Jul 31 Javascript
webpack的移动端适配方案小结
Jul 25 Javascript
微信小程序实现聊天室
Aug 21 #Javascript
vue实现折线图 可按时间查询
Aug 21 #Javascript
Vue按时间段查询数据组件使用详解
Aug 21 #Javascript
js绘制一条直线并旋转45度
Aug 21 #Javascript
AJAX XMLHttpRequest对象创建使用详解
Aug 20 #Javascript
基于vue.js仿淘宝收货地址并设置默认地址的案例分析
Aug 20 #Javascript
微信小程序以7天为周期连续签到7天功能效果的示例代码
Aug 20 #Javascript
You might like
php将数据库导出成excel的方法
2010/05/07 PHP
php存储过程调用实例代码
2013/02/03 PHP
基于yaf框架和uploadify插件,做的一个导入excel文件,查看并保存数据的功能
2017/01/24 PHP
php workerman定时任务的实现代码
2018/12/23 PHP
Laravel自定义 封装便捷返回Json数据格式的引用方法
2019/09/29 PHP
PHP CURL实现模拟登陆并上传文件操作示例
2020/01/02 PHP
jquery 批量上传图片实现代码
2010/01/28 Javascript
js数据验证集合、js email验证、js url验证、js长度验证、js数字验证等简单封装
2010/05/15 Javascript
jquery中实现简单的tabs插件功能的代码
2011/03/02 Javascript
JavaScript实现自己的DOM选择器原理及代码
2013/03/04 Javascript
关于删除时的提示处理(确定删除吗)
2013/11/03 Javascript
js实现鼠标悬停图片上时滚动文字说明的方法
2015/02/17 Javascript
JavaScript中this详解
2015/09/01 Javascript
微信小程序 中wx.chooseAddress(OBJECT)实例详解
2017/03/31 Javascript
Vue2.0实现购物车功能
2017/06/05 Javascript
JavaScript内存泄漏的处理方式
2017/11/20 Javascript
JS实现为动态创建的元素添加事件操作示例
2018/03/17 Javascript
一文看懂如何简单实现节流函数和防抖函数
2019/09/05 Javascript
layui-table表复选框勾选的所有行数据获取的例子
2019/09/13 Javascript
javascript设计模式之装饰者模式
2020/01/30 Javascript
Vue状态模式实现窗口停靠功能(灵动、自由, 管理后台Admin界面)
2020/03/06 Javascript
python多重继承新算法C3介绍
2014/09/28 Python
Python 实现简单的电话本功能
2015/08/09 Python
Python连接数据库学习之DB-API详解
2017/02/07 Python
Python中使用多进程来实现并行处理的方法小结
2017/08/09 Python
Python动态导入模块的方法实例分析
2018/06/28 Python
Ubuntu下Python2与Python3的共存问题
2018/10/31 Python
Python面向对象之类和对象属性的增删改查操作示例
2018/12/14 Python
浅谈Python中的生成器和迭代器
2020/06/19 Python
简单掌握CSS3将文字描边及填充文字颜色的方法
2016/03/07 HTML / CSS
中专生学习生活的自我评价分享
2013/10/27 职场文书
高中自我鉴定范文
2013/11/03 职场文书
副科级后备干部考察材料
2014/05/15 职场文书
2014年幼儿园老师工作总结
2014/12/05 职场文书
2015年中学元旦晚会活动方案
2014/12/09 职场文书
海上钢琴师的观后感
2015/06/11 职场文书