vuejs移動端實現(xiàn)div拖拽移動
vue移動端實現(xiàn)div拖拽移動,供大家參考,具體內(nèi)容如下
本文講述,在使用VUE的移動端實現(xiàn)類似于iPhone的懸浮窗的效果。
相關(guān)知識點
- touchstart 當(dāng)在屏幕上按下手指時觸發(fā)
- touchmove 當(dāng)在屏幕上移動手指時觸發(fā)
- touchend 當(dāng)在屏幕上抬起手指時觸發(fā)
- mousedown mousemove mouseup對應(yīng)的是PC端的事件
- touchcancel 當(dāng)一些更高級別的事件發(fā)生的時候(如電話接入或者彈出信息)會取消當(dāng)前的touch操作,即觸發(fā)touchcancel。一般會在touchcancel時暫停游戲、存檔等操作。
效果圖

實現(xiàn)步驟
(一)html
總結(jié)了一下評論,好像發(fā)現(xiàn)大家都碰到了滑動的問題。就在這里提醒一下吧。可將該懸浮 DIV 同你的 scroller web 同級。 ---- (log: 2018-08-21)
html結(jié)構(gòu): <template> <div>你的web頁面</div> <div>懸浮DIV</div> </template>
<template>
<div id="webId">
<div>你的web頁面</div>
<!-- 1.1 如果碰到滑動問題,請檢查這里是否屬于同一點。 -->
<!-- 懸浮的HTML -->
<div v-if="!isShow" class="xuanfu" id="moveDiv"
@mousedown="down" @touchstart="down"
@mousemove="move" @touchmove="move"
@mouseup="end" @touchend="end"
>
<div class="yuanqiu">
{{pageInfo.totalPage}}
</div>
</div>
</div>
</template>
(二)JS
<script>
data() {
return {
flags: false,
position: { x: 0, y: 0 },
nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',
}
}
methods: {
// 實現(xiàn)移動端拖拽
down(){
this.flags = true;
var 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){
var 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;
moveDiv.style.left = this.xPum+"px";
moveDiv.style.top = this.yPum +"px";
//阻止頁面的滑動默認事件
document.addEventListener("touchmove",function(){ // 1.2 如果碰到滑動問題,請注意是否獲取到 touchmove
event.preventDefault();//jq 阻止冒泡事件
// event.stopPropagation(); // 如果沒有引入jq 就用 stopPropagation()
},false);
}
},
//鼠標(biāo)釋放時候的函數(shù)
end(){
this.flags = false;
},
}
</script>
(三)CSS
<style>
.xuanfu {
height: 4.5rem;
width: 4.5rem;
/*1.3 如果碰到滑動問題,請檢查 z-index。z-index需比web大一級*/
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>
實現(xiàn)好JS邏輯,基本上,問題不大。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue el-table字段點擊出現(xiàn)el-input輸入框,失焦保存方式
這篇文章主要介紹了vue el-table字段點擊出現(xiàn)el-input輸入框,失焦保存方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
vuex直接修改state、commit和dispatch修改state的用法及區(qū)別說明
這篇文章主要介紹了vuex直接修改state、commit和dispatch修改state的用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
element-ui動態(tài)添加表單項并實現(xiàn)事件觸發(fā)驗證代碼示例
這篇文章主要給大家介紹了關(guān)于element-ui動態(tài)添加表單項并實現(xiàn)事件觸發(fā)驗證的相關(guān)資料,其實就是利用了vue的v-for循環(huán)渲染,通過添加數(shù)組實現(xiàn)動態(tài)添加表單項,需要的朋友可以參考下2023-12-12
Vue3組合式API中使用forwardRef()函數(shù)
本文主要介紹了Vue3組合式API中使用forwardRef()函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
詳解vue-cli4 配置不同開發(fā)環(huán)境打包命令
這篇文章主要介紹了vue-cli4 配置不同開發(fā)環(huán)境打包命令,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

