vue實現(xiàn)移動端觸屏拖拽功能
vue實現(xiàn)移動端可拖拽浮球,供大家參考,具體內(nèi)容如下
1 首先創(chuàng)建一個div
<div class="floatball" id="floatball"
@mousedown="down" @touchstart.stop="down"
@mousemove="move" @touchmove.stop="move"
@mouseup="end" @touchend.stop="end" @click="showRewardDesc"
:style="{top:position.y+'px', left:position.x+'px'}">
獎勵規(guī)則
</div>
2 給 div 附上樣式
<style>
.floatball{
color:white;
height:50px;
width: 50px;
padding: 5px;
z-index: 990;
position: fixed;
top: 60px;
right: 320px;
border-radius: 50%;
background-color: rgba(29, 157, 237,0.8);
}
</style>
3 給 div 附上事件
準備四個變量
1)、屏幕長
var screenHeight = window.screen.height
2)、屏幕寬
var screenWidth = window.screen.width
3)、初始觸控點 距離 div 左上角的橫向距離 dx
4)、初始觸控點 距離 div 左上角的豎向距離 dy

在開始拖拽時,計算出鼠標點(初始觸控點)和 div左上角頂點的距離
down(event){
this.flags = true;
var touch ;
if(event.touches){
touch = event.touches[0];
}else {
touch = event;
}
console.log('鼠標點所在位置', touch.clientX,touch.clientY)
console.log('div左上角位置', event.target.offsetTop,event.target.offsetLeft)
dx = touch.clientX - event.target.offsetLeft
dy = touch.clientY - event.target.offsetTop
},
拖拽進行時,將觸控點的位置賦值給 div
// 定位滑塊的位置 this.position.x = touch.clientX - dx; this.position.y = touch.clientY - dy;
// 限制滑塊超出頁面
// console.log('屏幕大小', screenWidth, screenHeight)
if (this.position.x < 0) {
this.position.x = 0
} else if (this.position.x > screenWidth - touch.target.clientWidth) {
this.position.x = screenWidth - touch.target.clientWidth
}
if (this.position.y < 0) {
this.position.y = 0
} else if (this.position.y > screenHeight - touch.target.clientHeight) {
this.position.y = screenHeight - touch.target.clientHeight
}
拖拽結(jié)束
//鼠標釋放時候的函數(shù)
end(){
console.log('end')
this.flags = false;
},
全部代碼
<template>
<div class="floatball" id="floatball"
@mousedown="down" @touchstart.stop="down"
@mousemove="move" @touchmove.stop="move"
@mouseup="end" @touchend.stop="end"
:style="{top:position.y+'px', left:position.x+'px'}">
獎勵規(guī)則
</div>
</template>
<script>
// 鼠標位置和div的左上角位置 差值
var dx,dy
var screenWidth = window.screen.width
var screenHeight = window.screen.height
export default {
data() {
return {
flags: false,
position: {
x: 320,
y: 60
},
}
},
methods: {
// 實現(xiàn)移動端拖拽
down(event){
this.flags = true;
var touch ;
if(event.touches){
touch = event.touches[0];
}else {
touch = event;
}
console.log('鼠標點所在位置', touch.clientX,touch.clientY)
console.log('div左上角位置', event.target.offsetTop,event.target.offsetLeft)
dx = touch.clientX - event.target.offsetLeft
dy = touch.clientY - event.target.offsetTop
},
move() {
if (this.flags) {
var touch ;
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
// 定位滑塊的位置
this.position.x = touch.clientX - dx;
this.position.y = touch.clientY - dy;
// 限制滑塊超出頁面
// console.log('屏幕大小', screenWidth, screenHeight )
if (this.position.x < 0) {
this.position.x = 0
} else if (this.position.x > screenWidth - touch.target.clientWidth) {
this.position.x = screenWidth - touch.target.clientWidth
}
if (this.position.y < 0) {
this.position.y = 0
} else if (this.position.y > screenHeight - touch.target.clientHeight) {
this.position.y = screenHeight - touch.target.clientHeight
}
//阻止頁面的滑動默認事件
document.addEventListener("touchmove",function(){
event.preventDefault();
},false);
}
},
//鼠標釋放時候的函數(shù)
end(){
console.log('end')
this.flags = false;
},
}
}
</script>
<style>
.floatball{
color:white;
height:50px;
width: 50px;
padding: 5px;
z-index: 990;
position: fixed;
top: 60px;
right: 320px;
border-radius: 50%;
background-color: rgba(29, 157, 237,0.8);
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue配合Vant使用時area省市區(qū)選擇器的使用方式
這篇文章主要介紹了Vue配合Vant使用時area省市區(qū)選擇器的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
基于 Vue.js 之 iView UI 框架非工程化實踐記錄(推薦)
為了快速體驗 MVVM 模式,我選擇了非工程化方式來起步,并選擇使用 Vue.js,以及基于它構(gòu)建的 iView UI 框架。本文給大家分享基于 Vue.js 之 iView UI 框架非工程化實踐記錄,需要的朋友參考下吧2017-11-11
vue.js內(nèi)部自定義指令與全局自定義指令的實現(xiàn)詳解(利用directive)
這篇文章主要給大家介紹了關(guān)于vue.js內(nèi)部自定義指令與全局自定義指令的實現(xiàn)方法,vue.js中實現(xiàn)自定義指令的主要是利用directive,directive這個單詞是我們寫自定義指令的關(guān)鍵字,需要的朋友們下面跟著小編來一起學習學習吧。2017-07-07

