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自动适应的图片弹窗实例
Jun 29 Javascript
Node.js文件操作详解
Aug 16 Javascript
jQuery控制TR显示隐藏的三种常用方法
Aug 21 Javascript
JavaScript 作用域链解析
Nov 13 Javascript
javascript递归回溯法解八皇后问题
Apr 22 Javascript
javascript模拟评分控件实现方法
May 13 Javascript
jQuery实现提示密码强度的代码
Jul 15 Javascript
ionic 上拉菜单(ActionSheet)实例代码
Jun 06 Javascript
Js+Ajax,Get和Post在使用上的区别小结
Jun 08 Javascript
原生Javascript插件开发实践
Jan 18 Javascript
Jquery中attr与prop的区别详解
May 27 jQuery
教你如何编写Vue.js的单元测试的方法
Oct 17 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中生成随机密码的自定义函数代码
2013/10/21 PHP
php使用curl获取header检测开启GZip压缩的方法
2018/08/15 PHP
Laravel用户授权系统的使用方法示例
2018/09/16 PHP
mysqli扩展无法在PHP7下升级问题的解决
2019/09/10 PHP
PHP8.0新功能之Match表达式的使用
2020/07/19 PHP
自己开发Dojo的建议框架
2008/09/24 Javascript
JavaScript入门教程(1) 什么是JS
2009/01/31 Javascript
JSON扫盲帖 JSON.as类教程
2009/02/16 Javascript
passwordStrength 基于jquery的密码强度检测代码使用介绍
2011/10/08 Javascript
js网页中的(运行代码)功能实现思路
2013/02/04 Javascript
JavaScript加强之自定义event事件
2013/09/21 Javascript
浅析jQuery事件之on()方法绑定多个选择器,多个事件
2016/04/27 Javascript
JS从数组中随机取出几个数组元素的方法
2016/08/02 Javascript
利用angularjs1.4制作的简易滑动门效果
2017/02/28 Javascript
详解关于html,css,js三者的加载顺序问题
2019/04/10 Javascript
Vue实现Header渐隐渐现效果的实例代码
2020/11/05 Javascript
你不知道的SpringBoot与Vue部署解决方案
2020/11/09 Javascript
对numpy中shape的深入理解
2018/06/15 Python
python制作填词游戏步骤详解
2019/05/05 Python
python logging日志模块原理及操作解析
2019/10/12 Python
python爬取2021猫眼票房字体加密实例
2021/02/19 Python
HTML5本地存储和本地数据库实例详解
2017/09/05 HTML / CSS
美国第一个网上卖鞋零售商:OnlineShoes.com
2017/09/24 全球购物
NBA欧洲商店(英国):NBA Europe Store UK
2018/07/27 全球购物
艺术系大学生毕业个人自我评价
2013/09/19 职场文书
精彩的推荐信范文
2013/11/26 职场文书
初中考试作弊检讨书
2014/02/01 职场文书
机关门卫制度
2014/02/01 职场文书
九年级政治教学反思
2014/02/06 职场文书
布达拉宫的导游词
2015/02/02 职场文书
单位计划生育责任书
2015/05/09 职场文书
导游词之南昌滕王阁
2019/11/29 职场文书
python tkinter模块的简单使用
2021/04/07 Python
详解Html5项目适配系统深色模式方案总结
2021/04/14 HTML / CSS
python中出现invalid syntax报错的几种原因分析
2022/02/12 Python
TV动画《史上最强大魔王转生为村民A》番宣CM公布
2022/04/01 日漫