vue實(shí)現(xiàn)移動端拖動排序
更新時(shí)間:2020年08月21日 10:50:36 作者:搖曳de瘋丶
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動端拖動排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了vue實(shí)現(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: ['選項(xiàng)1', '選項(xiàng)2', '選項(xiàng)3', '選項(xiàng)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)入另一個(gè)元素的位置,將他們的值互換,位置還原
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拉起支付寶支付的實(shí)現(xiàn)方法
這篇文章主要介紹了vue移動端使用appClound拉起支付寶支付的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue項(xiàng)目關(guān)閉eslint校驗(yàn)
eslint是一個(gè)JavaScript的校驗(yàn)插件,通常用來校驗(yàn)語法或代碼的書寫風(fēng)格。這篇文章主要介紹了vue項(xiàng)目關(guān)閉eslint校驗(yàn),需要的朋友可以參考下2018-03-03
關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取
這篇文章主要介紹了關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
前端Vue項(xiàng)目部署到服務(wù)器的全過程以及踩坑記錄
使用Vue做前后端分離項(xiàng)目時(shí),通常前端是單獨(dú)部署,用戶訪問的也是前端項(xiàng)目地址,因此前端開發(fā)人員很有必要熟悉一下項(xiàng)目部署的流程,下面這篇文章主要給大家介紹了關(guān)于前端Vue項(xiàng)目部署到服務(wù)器的全過程以及踩坑記錄的相關(guān)資料,需要的朋友可以參考下2023-05-05
Vue-cli中post請求發(fā)送Json格式數(shù)據(jù)方式
這篇文章主要介紹了Vue-cli中post請求發(fā)送Json格式數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

