vue移動(dòng)端寫的拖拽功能示例代碼
相關(guān)知識(shí)點(diǎn)
touchstart 當(dāng)在屏幕上按下手指時(shí)觸發(fā)
touchmove 當(dāng)在屏幕上移動(dòng)手指時(shí)觸發(fā)
touchend 當(dāng)在屏幕上抬起手指時(shí)觸發(fā)
mousedown mousemove mouseup對(duì)應(yīng)的是PC端的事件
touchcancel 當(dāng)一些更高級(jí)別的事件發(fā)生的時(shí)候(如電話接入或者彈出信息)會(huì)取消當(dāng)前的touch操作,即觸發(fā) touchcancel。一般會(huì)在touchcancel時(shí)暫停游戲、存檔等操作。
效果圖

實(shí)現(xiàn)步驟html
總結(jié)了一下評(píng)論,好像發(fā)現(xiàn)大家都碰到了滑動(dòng)的問題。就在這里提醒一下吧??蓪⒃搼腋?DIV 同你的 scroller web 同級(jí)。
<template>
<div id="webId">
<div>你的web頁面</div>
<!-- 1.1 如果碰到滑動(dòng)問題,請(qǐng)檢查這里是否屬于同一點(diǎn)。 -->
<!-- 懸浮的HTML -->
<div class="xuanfu" id="moveDiv"
@mousedown="down()" @touchstart="down()"
@mousemove.prevent.stop="move()"
@touchmove.prevent.stop="move()"
@mouseup="end()" @touchend="end()"
>
<div class="yuanqiu">11</div>
</div>
</div>
</template>
js
<script>
data() {
return {
flags: false,
position: { x: 0, y: 0 },
nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',
}
}
methods: {
// 實(shí)現(xiàn)移動(dòng)端拖拽
down(){
this.flags = true;
let touch;
if(event.touches){
touch = event.touches[0];
}else {
touch = event;
}
this.position.x = touch.clientX;
this.position.y = touch.clientY;
this.dx = moveDiv.offsetLeft;
this.dy = moveDiv.offsetTop;
},
move(){
if(this.flags){
let touch ;
if(event.touches){
touch = event.touches[0];
}else {
touch = event;
}
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;
//添加限制:只允許在屏幕內(nèi)拖動(dòng)
const maxWidth = document.body.clientWidth - 54;//屏幕寬度減去懸浮框?qū)捀?
const maxHeight = document.body.clientHeight - 54;
if (this.xPum < 0) { //屏幕x限制
this.xPum = 0;
} else if (this.xPum>maxWidth) {
this.xPum = maxWidth;
}
if (this.yPum < 0) { //屏幕y限制
this.yPum = 0;
} else if (this.yPum>maxHeight) {
this.yPum = maxHeight;
}
moveDiv.style.left = this.xPum+"px";
moveDiv.style.top = this.yPum +"px";
//阻止頁面的滑動(dòng)默認(rèn)事件
document.addEventListener("touchmove",function(){ // 1.2 如果碰到滑動(dòng)問題,請(qǐng)注意是否獲取到 touchmove
event.preventDefault();//jq 阻止冒泡事件
// event.stopPropagation(); // 如果沒有引入jq 就用 stopPropagation()
},false);
}
},
//鼠標(biāo)釋放時(shí)候的函數(shù)
end(){
this.flags = false;
},
}
</script>
css
<style>
/*css樣式可自定義 僅提供參考*/
#webId { position: relative; }
.xuanfu {
height: 54px; /* rem = 12px */
width: 54px;
/*1.3 如果碰到滑動(dòng)問題,請(qǐng)檢查 z-index。z-index需比web大一級(jí)*/
z-index: 999;
position: fixed;
top: 4.2rem;
right: 3.2rem;
border-radius: 0.8rem;
background-color: rgba(0, 0, 0, 0.55);
}
.yuanqiu {
height: 2.7rem;
width: 2.7rem;
border: 0.3rem solid rgba(140, 136, 136, 0.5);
margin: 0.65rem auto;
color: #000000;
font-size: 1.6rem;
line-height: 2.7rem;
text-align: center;
border-radius: 100%;
background-color: #ffffff;
}
</style>
總結(jié)
到此這篇關(guān)于vue移動(dòng)端寫的拖拽功能示例代碼的文章就介紹到這了,更多相關(guān)vue移動(dòng)端拖拽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ElementUI中利用table表格自定義表頭Tooltip文字提示
這篇文章主要介紹了ElementUI中利用table表格自定義表頭Tooltip文字提示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
vue.js計(jì)算屬性computed用法實(shí)例分析
這篇文章主要介紹了vue.js計(jì)算屬性computed用法,結(jié)合實(shí)例形式分析了vue.js使用computed方式進(jìn)行屬性計(jì)算的相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
Vue中textarea自適應(yīng)高度方案的實(shí)現(xiàn)
本文主要介紹了Vue中textarea自適應(yīng)高度方案的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
vue+moment實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了vue+moment實(shí)現(xiàn)倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
淺談Vue使用Elementui修改默認(rèn)的最快方法
這篇文章主要介紹了淺談Vue使用Elementui修改默認(rèn)的最快方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
vue中實(shí)現(xiàn)展示與隱藏側(cè)邊欄功能
這篇文章主要介紹了vue中實(shí)現(xiàn)展示與隱藏側(cè)邊欄功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue管理系統(tǒng)項(xiàng)目中的一些核心技能匯總
Vue是當(dāng)今增長(zhǎng)最快的前端框架,Vue 平易近人、用途廣泛且性能卓越,它的語法非常直觀,并且具有友好的學(xué)習(xí)曲線,是開發(fā)人員最想學(xué)習(xí)的頂級(jí)前端庫之一,下面這篇文章主要給大家介紹了關(guān)于vue管理系統(tǒng)項(xiàng)目中的一些核心技能,需要的朋友可以參考下2022-05-05

