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
在開始拖拽時,計算出鼠標(biāo)點(初始觸控點)和 div左上角頂點的距離
down(event){ this.flags = true; var touch ; if(event.touches){ touch = event.touches[0]; }else { touch = event; } console.log('鼠標(biāo)點所在位置', 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é)束
//鼠標(biāo)釋放時候的函數(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> // 鼠標(biāo)位置和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('鼠標(biāo)點所在位置', 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); } }, //鼠標(biāo)釋放時候的函數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個下載按鈕(操作方法)
這篇文章主要介紹了vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個下載按鈕,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04element-ui復(fù)雜table表格動態(tài)新增列、動態(tài)調(diào)整行以及列順序詳解
這篇文章主要給大家介紹了關(guān)于element-ui復(fù)雜table表格動態(tài)新增列、動態(tài)調(diào)整行以及列順序的相關(guān)資料,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-08-08使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法
這篇文章主要介紹了使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法,需要的朋友可以參考下2017-12-12