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)點擊的位置與視圖窗口的距離
* e.target.offsetLeft offsetTop 鼠標(biāo)點擊的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)文章
詳解TypeScript+Vue 插件 vue-class-component的使用總結(jié)
這篇文章主要介紹了TypeScript+Vue 插件 vue-class-component的使用總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02
基于el-table封裝的可拖拽行列、選擇列組件的實現(xiàn)
本文主要介紹了基于el-table封裝的可拖拽行列、選擇列組件的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
解決vue項目,npm run build后,報路徑錯的問題
這篇文章主要介紹了解決vue項目,npm run build后,報路徑錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

