vue實(shí)現(xiàn)移動(dòng)端觸屏拖拽功能
vue實(shí)現(xiàn)移動(dòng)端可拖拽浮球,供大家參考,具體內(nèi)容如下
1 首先創(chuàng)建一個(gè)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'}"> 獎(jiǎng)勵(lì)規(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 附上事件
準(zhǔn)備四個(gè)變量
1)、屏幕長(zhǎng)
var screenHeight = window.screen.height
2)、屏幕寬
var screenWidth = window.screen.width
3)、初始觸控點(diǎn) 距離 div 左上角的橫向距離 dx
4)、初始觸控點(diǎn) 距離 div 左上角的豎向距離 dy
在開始拖拽時(shí),計(jì)算出鼠標(biāo)點(diǎn)(初始觸控點(diǎn))和 div左上角頂點(diǎn)的距離
down(event){ this.flags = true; var touch ; if(event.touches){ touch = event.touches[0]; }else { touch = event; } console.log('鼠標(biāo)點(diǎn)所在位置', 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 },
拖拽進(jìn)行時(shí),將觸控點(diǎn)的位置賦值給 div
// 定位滑塊的位置 this.position.x = touch.clientX - dx; this.position.y = touch.clientY - dy;
// 限制滑塊超出頁(yè)面 // 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í)候的函數(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'}"> 獎(jiǎng)勵(lì)規(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: { // 實(shí)現(xiàn)移動(dòng)端拖拽 down(event){ this.flags = true; var touch ; if(event.touches){ touch = event.touches[0]; }else { touch = event; } console.log('鼠標(biāo)點(diǎn)所在位置', 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; // 限制滑塊超出頁(yè)面 // 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 } //阻止頁(yè)面的滑動(dòng)默認(rèn)事件 document.addEventListener("touchmove",function(){ event.preventDefault(); },false); } }, //鼠標(biāo)釋放時(shí)候的函數(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕(操作方法)
這篇文章主要介紹了vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個(gè)下載按鈕,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04element-ui復(fù)雜table表格動(dòng)態(tài)新增列、動(dòng)態(tài)調(diào)整行以及列順序詳解
這篇文章主要給大家介紹了關(guān)于element-ui復(fù)雜table表格動(dòng)態(tài)新增列、動(dòng)態(tài)調(diào)整行以及列順序的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法
這篇文章主要介紹了使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法,需要的朋友可以參考下2017-12-12