相關(guān)知識點
touchstart 當在屏幕上按下手指時觸發(fā)touchmove 當在屏幕上移動手指時觸發(fā)
touchend 當在屏幕上抬起手指時觸發(fā)
mousedown mousemove mouseup對應(yīng)的是PC端的事件touchcancel 當一些更高級別的事件發(fā)生的時候(如電話接入或者彈出信息)會取消當前的touch操作屹耐,即觸發(fā)touchcancel钧唐。一般會在touchcancel時暫停游戲、存檔等操作踊餐。
html
<template>
<div id="webId">
<div>你的web頁面</div>
<!-- 如果碰到滑動問題军浆,1.1 請檢查這里是否屬于同一點夫植。 -->
<!-- 懸浮的HTML -->
<div v-if="!isShow" class="xuanfu" id="moveDiv"
@mousedown="down" @touchstart="down"
//..prevent 防止滑動時泊业,頁面也跟著滑動
@mousemove="move" @touchmove.prevent="move"
@mouseup="end" @touchend="end"
>
<div class="yuanqiu">
{{pageInfo.totalPage}}
</div>
</div>
</div>
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";
//阻止頁面的滑動默認事件且叁;如果碰到滑動問題都哭,1.2 請注意是否獲取到 touchmove
document.addEventListener("touchmove",function(){
event.preventDefault();
},false);
}
},
//鼠標釋放時候的函數(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>