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 相关文章推荐
JS遍历数组及打印数组实例分析
Jan 21 Javascript
js实现上传文件添加和删除文件选择框
Oct 24 Javascript
bootstrap的3级菜单样式,支持母版页保留打开状态实现方法
Nov 10 Javascript
微信小程序购物商城系统开发系列-工具篇的介绍
Nov 21 Javascript
js实现返回顶部效果
Mar 10 Javascript
浅谈Angular2 模块懒加载的方法
Oct 04 Javascript
vue初尝试--项目结构(推荐)
Jan 30 Javascript
Vue自定义指令实现checkbox全选功能的方法
Feb 28 Javascript
vue的token刷新处理的方法
Jul 17 Javascript
10行代码实现微信小程序滑动tab切换
Dec 28 Javascript
详解Vue项目引入CreateJS的方法(亲测可用)
May 30 Javascript
vue如何使用外部特殊字体的操作
Jul 30 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获取http请求的头信息实现步骤
2012/12/16 PHP
thinkphp区间查询、统计查询与SQL直接查询实例分析
2014/11/24 PHP
ThinkPHP打开验证码页面显示乱码的解决方法
2014/12/18 PHP
使用Zttp简化Guzzle 调用
2017/07/02 PHP
用Javscript实现表单复选框的全选功能
2007/05/25 Javascript
用javascript实现画板的代码
2007/09/05 Javascript
jQuery 无刷新分页实例代码
2013/11/12 Javascript
js与运算符和或运算符的妙用
2014/02/14 Javascript
JS动态显示表格上下frame的方法
2015/03/31 Javascript
javascript动态添加checkbox复选框的方法
2015/12/23 Javascript
javascript基本数据类型和转换
2017/03/17 Javascript
如何使用bootstrap框架 bootstrap入门必看!
2017/04/13 Javascript
jQuery复合事件用法示例
2017/06/10 jQuery
vue中使用v-model完成组件间的通信
2019/08/22 Javascript
JS实现提示效果弹出及延迟隐藏的功能
2019/08/26 Javascript
Python错误: SyntaxError: Non-ASCII character解决办法
2017/06/08 Python
Python算法之求n个节点不同二叉树个数
2017/10/27 Python
Python argparse模块使用方法解析
2020/02/20 Python
Python读取Excel一列并计算所有对象出现次数的方法
2020/09/04 Python
CSS3弹性盒模型开发笔记(二)
2016/04/26 HTML / CSS
美国宠物美容和宠物用品购物网站:Cherrybrook
2018/12/07 全球购物
印度电子产品购物网站:Vijay Sales
2021/02/16 全球购物
焊接专业毕业生求职信
2013/10/01 职场文书
团员个人的自我评价
2013/12/02 职场文书
医学专业五年以上个人求职信
2013/12/03 职场文书
教师师德反思材料
2014/02/15 职场文书
毕业生自荐信格式
2014/03/07 职场文书
我的老师教学反思
2014/05/01 职场文书
法律顾问服务方案
2014/05/15 职场文书
金融专业求职信
2014/08/05 职场文书
公司演讲稿开场白
2014/08/25 职场文书
试用期转正员工自我评价
2014/09/18 职场文书
2014年技术部工作总结
2014/12/12 职场文书
干部年终考核评语
2015/01/04 职场文书
四年级作文之植物
2019/09/20 职场文书