vue實(shí)現(xiàn)移動(dòng)端懸浮窗效果
本文講述,在使用VUE的移動(dòng)端實(shí)現(xiàn)類(lèi)似于iPhone的懸浮窗的效果。
相關(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)步驟
1.html
總結(jié)了一下評(píng)論,好像發(fā)現(xiàn)大家都碰到了滑動(dòng)的問(wèn)題。就在這里提醒一下吧??蓪⒃搼腋?DIV 同你的 scroller web 同級(jí)。 —- (log: 2018-08-21)
html結(jié)構(gòu): <template> <div>你的web頁(yè)面</div> <div>懸浮DIV</div> </template>
<template>
<div id="webId">
...
<div>你的web頁(yè)面</div>
<!-- 如果碰到滑動(dòng)問(wèn)題,1.1 請(qǐng)檢查這里是否屬于同一點(diǎn)。 -->
<!-- 懸浮的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>
2.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;
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";
//阻止頁(yè)面的滑動(dòng)默認(rèn)事件;如果碰到滑動(dòng)問(wèn)題,1.2 請(qǐng)注意是否獲取到 touchmove
document.addEventListener("touchmove",function(){
event.preventDefault();
},false);
}
},
//鼠標(biāo)釋放時(shí)候的函數(shù)
end(){
this.flags = false;
},
}
</script>
3.CSS
<style>
.xuanfu {
height: 4.5rem;
width: 4.5rem;
/* 如果碰到滑動(dòng)問(wèn)題,1.3 請(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>
實(shí)現(xiàn)好JS邏輯,基本上,問(wèn)題不大。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue @click.stop阻止事件向祖先元素傳遞方式(事件冒泡)
在Vue中,使用@click.stop修飾符可以阻止事件向父級(jí)元素傳遞,從而實(shí)現(xiàn)單擊父級(jí)元素執(zhí)行函數(shù),而單擊子元素不執(zhí)行函數(shù)的需求2025-02-02
動(dòng)態(tài)加載權(quán)限管理模塊中的Vue組件
本篇文章給大家詳細(xì)講解了如何在權(quán)限管理模塊中動(dòng)態(tài)的加載VUE組件的過(guò)程,有這方面需求的朋友跟著學(xué)習(xí)下吧。2018-01-01
vue實(shí)現(xiàn)小球滑動(dòng)交叉效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)小球滑動(dòng)交叉,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Nuxt3+ElementPlus構(gòu)建打包部署全過(guò)程
網(wǎng)上大部分關(guān)于Nuxt打包部署教程可謂是可以用五花八門(mén)來(lái)形容,這對(duì)于第一次接觸的朋友簡(jiǎn)直是無(wú)從下手,這篇文章主要給大家介紹了關(guān)于Nuxt3+ElementPlus構(gòu)建打包部署的相關(guān)資料,需要的朋友可以參考下2023-01-01
解決vue的router組件component在import時(shí)不能使用變量問(wèn)題
這篇文章主要介紹了解決vue的router組件component在import時(shí)不能使用變量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue Getters和mapGetters的原理及使用示例詳解
Vuex的核心概念包括state、mutations、actions、getters和modules,今天,我們要深入探討其中一個(gè)關(guān)鍵部分:getters,以及它的相關(guān)輔助函數(shù)mapGetters,感興趣的朋友跟隨小編一起看看吧2024-08-08

