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实现浏览器菜单命令
Sep 05 Javascript
jquery下操作HTML控件的实现代码
Jan 12 Javascript
JS动态添加option和删除option(附实例代码)
Apr 01 Javascript
使用apply方法实现javascript中的对象继承
Dec 16 Javascript
javascript setinterval 的正确语法如何书写
Jun 17 Javascript
javascript修改图片src的方法
Jan 27 Javascript
JavaScript实现的伸展收缩型菜单代码
Oct 14 Javascript
jquery仿微信聊天界面
May 06 jQuery
详谈AngularJs 控制器、数据绑定、作用域
Jul 09 Javascript
原生js封装的ajax方法示例
Aug 02 Javascript
js实现图片粘贴到网页
Dec 06 Javascript
JS继承实现方法及优缺点详解
Sep 02 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 curl 登录163邮箱并抓取邮箱好友列表的代码(经测试)
2011/04/07 PHP
PHP中全面阻止SQL注入式攻击分析小结
2012/01/30 PHP
PHP中__get()和__set()的用法实例详解
2013/06/04 PHP
laravel 5 实现模板主题功能(续)
2015/03/02 PHP
PHP面向对象学习之parent::关键字
2017/01/18 PHP
详细分析PHP 命名空间(namespace)
2020/06/30 PHP
javascript 自动填写表单的实现方法
2010/04/09 Javascript
通过length属性判断jquery对象是否存在
2013/10/18 Javascript
javascript通过className来获取元素的简单示例代码
2014/01/10 Javascript
jquery插件NProgress.js制作网页加载进度条
2015/06/05 Javascript
Bootstrap基本组件学习笔记之面板(14)
2016/12/08 Javascript
微信小程序实现图片轮播及文件上传
2017/04/07 Javascript
详解webpack 配合babel 将es6转成es5 超简单实例
2017/05/02 Javascript
JavaScript中防止微信浏览器被整体拖动的方法
2017/08/25 Javascript
JavaScript实现微信号随机切换代码
2018/03/09 Javascript
vue组件横向树实现代码
2018/08/02 Javascript
vue中实现点击按钮滚动到页面对应位置的方法(使用c3平滑属性实现)
2019/12/29 Javascript
jquery绑定事件 bind和on的用法与区别分析
2020/05/22 jQuery
ES6 Symbol在对象中的作用实例分析
2020/06/06 Javascript
Javascript var变量删除原理及实现
2020/08/26 Javascript
[35:26]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第三局
2016/02/26 DOTA
从零学Python之入门(四)运算
2014/05/27 Python
python获取目录下所有文件的方法
2015/06/01 Python
从CentOS安装完成到生成词云python的实例
2017/12/01 Python
Python DataFrame设置/更改列表字段/元素类型的方法
2018/06/09 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
使用CSS实现阅读进度条
2017/02/27 HTML / CSS
html5通过postMessage进行跨域通信的方法
2017/12/04 HTML / CSS
Weekendesk意大利:探索多种引人入胜的周末主题
2016/10/14 全球购物
佛罗里达州印第安河新鲜水果:Hale Groves
2017/02/20 全球购物
苏格兰在线威士忌商店:The Whisky Barrel
2019/05/07 全球购物
大学总结自我鉴定
2014/01/18 职场文书
租房协议书
2014/04/10 职场文书
羽毛球比赛策划方案
2014/06/13 职场文书
催款通知书范文
2015/04/17 职场文书
详解PHP Swoole与TCP三次握手
2021/05/27 PHP