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高级程序设计第二版第十二章事件要点总结(常用的跨浏览器检测方法)
Aug 22 Javascript
5个JavaScript经典面试题
Oct 13 Javascript
js读取并解析JSON类型数据的方法
Nov 14 Javascript
使用Object.defineProperty实现简单的js双向绑定
Apr 15 Javascript
javascript显示倒计时控制按钮的简单实现
Jun 07 Javascript
AngularJS基础 ng-hide 指令用法及示例代码
Aug 01 Javascript
canvas 绘制圆形时钟
Feb 22 Javascript
vue.js实现刷新当前页面的方法教程
Jul 05 Javascript
实时监控input框,实现输入框与下拉框联动的实例
Jan 23 Javascript
详解从Vue-router到html5的pushState
Jul 21 Javascript
详解在vue-cli中使用graphql即vue-apollo的用法
Sep 08 Javascript
详解Vue内部怎样处理props选项的多种写法
Nov 06 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+dbfile开发小型留言本
2006/10/09 PHP
php中文本数据翻页(留言本翻页)
2006/10/09 PHP
使用php4加速网络传输
2006/10/09 PHP
ZF等常用php框架中存在的问题
2008/01/10 PHP
PHP 文件类型判断代码
2009/03/13 PHP
PHP函数import_request_variables()用法分析
2016/04/02 PHP
Javascript 继承实现例子
2009/08/12 Javascript
javascript之AJAX框架使用说明
2010/04/24 Javascript
JavaScript 对象链式操作测试代码
2010/04/25 Javascript
javascript利用初始化数据装配模版的实现代码
2010/11/17 Javascript
javascript权威指南 学习笔记之变量作用域分享
2011/09/28 Javascript
Javascript base64编码实现代码
2011/12/02 Javascript
JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)
2015/10/10 Javascript
深入理解jQuery之防止冒泡事件
2016/05/24 Javascript
js智能获取浏览器版本UA信息的方法
2016/08/08 Javascript
Javascript中常用的检测方法小结
2016/10/08 Javascript
用nodeJS搭建本地文件服务器的几种方法小结
2017/03/16 NodeJs
javascript实现滑动解锁功能
2017/03/22 Javascript
react-navigation之动态修改title的内容
2018/09/26 Javascript
Vue3项目打包后部署到服务器 请求不到后台接口解决方法
2020/02/06 Javascript
Python日期操作学习笔记
2008/10/07 Python
详解Python的Django框架中inclusion_tag的使用
2015/07/21 Python
总结python爬虫抓站的实用技巧
2016/08/09 Python
用python写扫雷游戏实例代码分享
2018/05/27 Python
对python:print打印时加u的含义详解
2018/12/15 Python
python如何以表格形式打印输出的方法示例
2019/06/21 Python
推荐8款常用的Python GUI图形界面开发框架
2020/02/23 Python
TensorFlow keras卷积神经网络 添加L2正则化方式
2020/05/22 Python
tensorflow pb to tflite 精度下降详解
2020/05/25 Python
pycharm 复制代码出现空格的解决方式
2021/01/15 Python
Python爬虫+tkinter界面实现历史天气查询的思路详解
2021/02/22 Python
一款纯css3实现的响应式导航
2014/10/31 HTML / CSS
HTML5 Web存储方式的localStorage和sessionStorage进行数据本地存储案例应用
2012/12/09 HTML / CSS
html5 application cache遇到的严重问题
2012/12/26 HTML / CSS
爱国之歌(8首)
2019/09/29 职场文书
vue使用v-model进行跨组件绑定的基本实现方法
2021/04/28 Vue.js