亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue實現(xiàn)移動端拖動排序

 更新時間:2020年08月21日 10:50:36   作者:搖曳de瘋丶  
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)移動端拖動排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)移動端拖動排序的具體代碼,供大家參考,具體內(nèi)容如下

效果

代碼:

<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 鼠標(biāo)點(diǎn)擊的位置與視圖窗口的距離
  * e.target.offsetLeft offsetTop 鼠標(biāo)點(diǎn)擊的div與父元
  * 素的邊框距離,父元素必須為定位樣式,不然認(rèn)為父元素為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);//找到當(dāng)前元素的中間位置
  let arr = Array.from(this.columns);//將nodelist轉(zhuǎn)為array
  let index = arr.indexOf(e.target);//找到當(dāng)前元素下標(biāo)
  for(let i in arr){
   //如果當(dāng)前元素進(jìn)入另一個元素的位置,將他們的值互換,位置還原
   if(eleTop > arr[i].offsetTop && eleTop < (arr[i].offsetTop + arr[i].offsetHeight)){
   //值互換,位置還原(保證數(shù)組的序列數(shù)據(jù)不變)
   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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue移動端使用appClound拉起支付寶支付的實現(xiàn)方法

    vue移動端使用appClound拉起支付寶支付的實現(xiàn)方法

    這篇文章主要介紹了vue移動端使用appClound拉起支付寶支付的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • vue項目關(guān)閉eslint校驗

    vue項目關(guān)閉eslint校驗

    eslint是一個JavaScript的校驗插件,通常用來校驗語法或代碼的書寫風(fēng)格。這篇文章主要介紹了vue項目關(guān)閉eslint校驗,需要的朋友可以參考下
    2018-03-03
  • checkbox在vue中的用法小結(jié)

    checkbox在vue中的用法小結(jié)

    之前對于vue中用到過的checkbox也只是別人寫好的組件,這次在自己實現(xiàn)時走了很多坑,特意寫這篇文章記錄到腳本之家平臺,供大家參考
    2018-11-11
  • 關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取

    關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取

    這篇文章主要介紹了關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue 指定文字高亮的實現(xiàn)示例

    vue 指定文字高亮的實現(xiàn)示例

    在做文字處理的項目時經(jīng)常會遇到搜索文字并高亮的需求,本文就來介紹vue 指定文字高亮的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • Vue3中其他的Composition?API詳解

    Vue3中其他的Composition?API詳解

    這篇文章主要介紹了Vue3中其他的Composition?API,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • 前端Vue項目部署到服務(wù)器的全過程以及踩坑記錄

    前端Vue項目部署到服務(wù)器的全過程以及踩坑記錄

    使用Vue做前后端分離項目時,通常前端是單獨(dú)部署,用戶訪問的也是前端項目地址,因此前端開發(fā)人員很有必要熟悉一下項目部署的流程,下面這篇文章主要給大家介紹了關(guān)于前端Vue項目部署到服務(wù)器的全過程以及踩坑記錄的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Vue設(shè)置keepAlive不生效問題及解決

    Vue設(shè)置keepAlive不生效問題及解決

    這篇文章主要介紹了Vue設(shè)置keepAlive不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue如何在新窗口打開頁面

    vue如何在新窗口打開頁面

    這篇文章主要介紹了vue如何在新窗口打開頁面問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue-cli中post請求發(fā)送Json格式數(shù)據(jù)方式

    Vue-cli中post請求發(fā)送Json格式數(shù)據(jù)方式

    這篇文章主要介紹了Vue-cli中post請求發(fā)送Json格式數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評論